window自动化分类:1.新增选择窗口(inspect),支持高亮指定元素,并查看元素的名称、类型、句柄、xpath等各项属性及所在窗口各项属性 2.新增界面自动化,支持通过xpath、name、id等条件在对指定元素执行点击、设置值、模拟输入、高亮等操作

This commit is contained in:
fofolee
2025-01-17 18:51:11 +08:00
parent 61d711765b
commit 10b67c919a
11 changed files with 2203 additions and 2110 deletions

View File

@@ -1,271 +1,98 @@
const { runCsharpFeature } = require("../../csharp");
/**
* 窗口置顶,只操作第一个找到的窗口
* @param {string} method 查找方式:"title"|"handle"|"process"|"class"|"active"
* @param {string} value 窗口标题、句柄、进程名、类名
* @param {Object} result 结果
* @param {boolean} result.success 是否成功
* @param {Object} result.window 被置顶的窗口信息
* 执行窗口操作
* @param {string} type - 操作类型, 可选值: "topmost"|"opacity"|"rect"|"state"|"close"|"focus"|"border"|"clickthrough"|"info"
* @param {Object} params - 参数对象
* @param {string} params.method - 查找方式:"title"|"handle"|"process"|"class"|"active"
* @param {string} params.window - 窗口标题、句柄、进程名、类名
* @param {*} params.value - 操作的值,不同操作类型对应不同的值类型
* @returns {Promise<Object>} - 操作结果
*/
async function setTopMost(method, value, isTopMost) {
const args = [
"-type",
"topmost",
"-method",
method,
"-window",
value,
"-value",
isTopMost.toString(),
];
async function runWindow(type, params = {}) {
const args = ["-type", type];
const { method = "active", window, value } = params;
// 通用参数
args.push("-method", method);
if (window) {
args.push("-window", window);
}
// 特定命令的参数处理
switch (type) {
case "topmost":
case "border":
case "clickthrough":
if (value !== undefined) {
args.push("-value", value.toString());
}
break;
case "opacity":
if (value !== undefined) {
args.push("-value", value.toString());
}
break;
case "rect":
if (value && typeof value === "object") {
const { x = 0, y = 0, width = 0, height = 0 } = value;
args.push("-value", `${x},${y},${width},${height}`);
}
break;
case "state":
if (value) {
args.push("-value", value);
}
break;
}
let error;
try {
const windowInfo = await runCsharpFeature("window", args);
if (windowInfo) {
return {
success: true,
window: JSON.parse(windowInfo),
};
const result = await runCsharpFeature("window", args);
if (result) {
const jsonResult = JSON.parse(result);
if (type === "info") {
return jsonResult;
}
return { success: true, window: jsonResult };
}
} catch (err) {
error = err.toString();
error = err
.toString()
.replace(/^Error: /, "")
.trim();
console.log(error);
}
return { success: false, error };
}
/**
* 设置窗口透明度,只操作第一个找到的窗口
* @param {string} method 查找方式:"title"|"handle"|"process"|"class"|"active"
* @param {string} value 窗口标题、句柄、进程名、类名
* @param {number} opacity 透明度 0-100
* @param {Object} result 结果
* @param {boolean} result.success 是否成功
* @param {Object} result.window 被设置透明度的窗口信息
*/
async function setOpacity(method, value, opacity) {
const args = [
"-type",
"opacity",
"-method",
method,
"-window",
value,
"-value",
opacity.toString(),
];
let error;
try {
const windowInfo = await runCsharpFeature("window", args);
if (windowInfo) {
return { success: true, window: JSON.parse(windowInfo) };
}
} catch (err) {
error = err.toString();
}
if (type === "info") return [];
return { success: false, error };
}
/**
* 设置窗口位置和大小,只操作第一个找到的窗口
* @param {string} method 查找方式:"title"|"handle"|"process"|"class"|"active"
* @param {string} value 窗口标题、句柄、进程名、类名
* @param {number} x X坐标
* @param {number} y Y坐标
* @param {number} width 宽度
* @param {number} height 高度
* @param {Object} result 结果
* @param {boolean} result.success 是否成功
* @param {Object} result.window 被设置位置和大小的窗口信息
*/
async function setWindowRect(method, value, x, y, width, height) {
const args = [
"-type",
"rect",
"-method",
method,
"-window",
value,
"-value",
`${x},${y},${width},${height}`,
];
let error;
try {
const windowInfo = await runCsharpFeature("window", args);
if (windowInfo) {
return { success: true, window: JSON.parse(windowInfo) };
}
} catch (err) {
error = err.toString();
}
return { success: false, error };
}
/**
* 设置窗口状态,只操作第一个找到的窗口
* @param {string} method 查找方式:"title"|"handle"|"process"|"class"|"active"
* @param {string} value 窗口标题、句柄、进程名、类名
* @param {string} state 状态:"normal"|"maximize"|"minimize"
* @param {Object} result 结果
* @param {boolean} result.success 是否成功
* @param {Object} result.window 被设置状态的窗口信息
*/
async function setWindowState(method, value, state) {
const args = [
"-type",
"state",
"-method",
method,
"-window",
value,
"-value",
state,
];
let error;
try {
const windowInfo = await runCsharpFeature("window", args);
if (windowInfo) {
return { success: true, window: JSON.parse(windowInfo) };
}
} catch (err) {
error = err.toString();
}
return { success: false, error };
}
/**
* 关闭窗口,只操作第一个找到的窗口
* @param {string} method 查找方式:"title"|"handle"|"process"|"class"|"active"
* @param {string} value 窗口标题、句柄、进程名、类名
* @param {Object} result 结果
* @param {boolean} result.success 是否成功
* @param {Object} result.window 被关闭的窗口信息
*/
async function closeWindow(method, value) {
const args = ["-type", "close", "-method", method, "-window", value];
let error;
try {
const windowInfo = await runCsharpFeature("window", args);
if (windowInfo) {
return { success: true, window: JSON.parse(windowInfo) };
}
} catch (err) {
error = err.toString();
}
return { success: false, error };
}
/**
* 设置窗口焦点,只操作第一个找到的窗口
* @param {string} method 查找方式:"title"|"handle"|"process"|"class"|"active"
* @param {string} value 窗口标题、句柄、进程名、类名
* @param {Object} result 结果
* @param {boolean} result.success 是否成功
* @param {Object} result.window 被设置焦点的窗口信息
*/
async function setFocus(method, value) {
const args = ["-type", "focus", "-method", method, "-window", value];
let error;
try {
const windowInfo = await runCsharpFeature("window", args);
if (windowInfo) {
return { success: true, window: JSON.parse(windowInfo) };
}
} catch (err) {
error = err.toString();
}
return { success: false, error };
}
/**
* 设置窗口边框,只操作第一个找到的窗口
* @param {string} method 查找方式:"title"|"handle"|"process"|"class"|"active"
* @param {string} value 窗口标题、句柄、进程名、类名
* @param {boolean} hasBorder 是否显示边框
* @param {Object} result 结果
* @param {boolean} result.success 是否成功
* @param {Object} result.window 被设置边框的窗口信息
*/
async function setBorder(method, value, hasBorder) {
const args = [
"-type",
"border",
"-method",
method,
"-window",
value,
"-value",
hasBorder.toString(),
];
let error;
try {
const windowInfo = await runCsharpFeature("window", args);
if (windowInfo) {
return { success: true, window: JSON.parse(windowInfo) };
}
} catch (err) {
error = err.toString();
}
return { success: false, error };
}
/**
* 设置窗口点击穿透,只操作第一个找到的窗口
* @param {string} method 查找方式:"title"|"handle"|"process"|"class"|"active"
* @param {string} value 窗口标题、句柄、进程名、类名
* @param {boolean} isTransparent 是否点击穿透
* @param {Object} result 结果
* @param {boolean} result.success 是否成功
* @param {Object} result.window 被设置点击穿透的窗口信息
*/
async function setClickThrough(method, value, isTransparent) {
const args = [
"-type",
"clickthrough",
"-method",
method,
"-window",
value,
"-value",
isTransparent.toString(),
];
let error;
try {
const windowInfo = await runCsharpFeature("window", args);
if (windowInfo) {
return { success: true, window: JSON.parse(windowInfo) };
}
} catch (err) {
error = err.toString();
}
return { success: false, error };
}
/**
* 获取窗口信息,返回所有匹配的窗口信息
* @param {string} method 查找方式:"title"|"handle"|"process"|"class"|"active"
* @param {string} value 窗口标题、句柄、进程名、类名
* @returns {Object} 所有匹配的窗口信息
*/
async function getWindowInfo(method, value) {
const args = ["-type", "info", "-method", method, "-window", value];
try {
const windowInfo = await runCsharpFeature("window", args);
if (windowInfo) return JSON.parse(windowInfo);
} catch (err) {
console.log(err);
}
return [];
}
module.exports = {
setTopMost,
setOpacity,
setWindowRect,
setWindowState,
closeWindow,
setFocus,
setBorder,
setClickThrough,
getWindowInfo,
setTopMost: (method, window, isTopMost) =>
runWindow("topmost", { method, window, value: isTopMost }),
setOpacity: (method, window, opacity) =>
runWindow("opacity", { method, window, value: opacity }),
setWindowRect: (method, window, x, y, width, height) =>
runWindow("rect", { method, window, value: { x, y, width, height } }),
setWindowState: (method, window, state) =>
runWindow("state", { method, window, value: state }),
closeWindow: (method, window) => runWindow("close", { method, window }),
setFocus: (method, window) => runWindow("focus", { method, window }),
setBorder: (method, window, hasBorder) =>
runWindow("border", { method, window, value: hasBorder }),
setClickThrough: (method, window, isTransparent) =>
runWindow("clickthrough", { method, window, value: isTransparent }),
getWindowInfo: (method, window) => runWindow("info", { method, window }),
};