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,168 +1,144 @@
const { runCsharpFeature } = require("../../csharp");
/**
* 列出所有元素
* @param {string} method 窗口类型:"title"|"handle"|"process"|"class"|"active"
* @param {string} window 窗口标题、句柄、进程名、类名
* @param {object} options 选项
* @param {string} options.filter 过滤条件
* @returns {object[]} 元素列表
* 执行自动化操作
* @param {string} type - 操作类型, 可选值: "inspect"|"click"|"setvalue"|"getvalue"|"select"|"expand"|"scroll"|"wait"|"focus"|"highlight"
* @param {Object} params - 参数对象
* @param {string} params.by - 查找方式:"xpath"|"id"|"name"|"condition"
* @param {string} params.searchValue - 搜索值
* @param {string} params.window - 窗口标题、句柄、进程名、类名
* @param {Object} params.options - 附加选项
* @returns {Promise<Object>} - 操作结果
*/
const listElements = async function (method, window, options = {}) {
const { filter, scope } = options;
const args = ["-type", "list"];
async function runAutomation(
type,
window = "",
by = "xpath",
searchValue = "",
params = {}
) {
const args = [];
args.push("-method", method);
if (method !== "active" && window) args.push("-window", window);
if (filter) args.push("-filter", filter);
if (scope) args.push("-scope", scope);
try {
const result = await runCsharpFeature("automation", args);
console.log(result);
if (result) return JSON.parse(result);
} catch (err) {
console.log(err);
if (window) {
args.push("-window", window);
}
return [];
};
/**
* 点击元素
* @param {string} method 窗口类型:"title"|"handle"|"process"|"class"|"active"
* @param {string} window 窗口标题、句柄、进程名、类名
* @param {string} by 查找方式:"name"|"class"|"type"|"automationid"
* @param {string} value 查找值
* @param {object} options 选项
* @param {string} options.pattern 点击模式:"invoke"|"toggle"
* @param {boolean} options.background 是否后台操作
* @returns {Object} 操作结果
* @property {boolean} success 是否成功
* @property {Object} element 操作的元素信息
* @property {Object} element.window 操作的元素所在的窗口信息
*/
const clickElement = async function (method, window, by, value, options = {}) {
const { pattern = "invoke", background = false } = options;
const args = ["-type", "click", "-by", by, "-value", value];
args.push("-type", type);
// 通用参数处理
if (type !== "inspect") {
switch (by) {
case "xpath":
args.push("-xpath", searchValue);
break;
case "id":
args.push("-id", searchValue);
break;
case "name":
args.push("-name", searchValue);
break;
case "condition":
args.push("-condition", searchValue);
break;
}
}
// 特定命令的参数处理
switch (type) {
case "inspect":
if (params) {
args.push("-position");
if (params.x && params.y) {
args.push(`${params.x},${params.y}`);
}
}
break;
case "setvalue":
if (params.newValue !== undefined) {
args.push("-value", params.newValue);
}
if (params.sendenter) {
args.push("-sendenter");
}
break;
case "select":
if (params.item) {
args.push("-item", params.item);
}
break;
case "expand":
if (params.expand !== undefined) {
args.push("-expand", params.expand);
}
break;
case "scroll":
if (params.direction) {
args.push("-direction", params.direction);
}
if (params.amount !== undefined) {
args.push("-amount", params.amount);
}
break;
case "wait":
if (params.condition) {
args.push("-condition", params.condition);
}
if (params.timeout !== undefined) {
args.push("-timeout", params.timeout);
}
break;
case "highlight":
if (params.duration !== undefined) {
args.push("-duration", params.duration);
}
break;
case "sendkeys":
if (params.keys) {
args.push("-keys", params.keys);
}
break;
}
args.push("-method", method);
if (method !== "active" && window) args.push("-window", window);
if (pattern) args.push("-pattern", pattern);
if (background) args.push("-background");
let error;
try {
const result = await runCsharpFeature("automation", args);
if (result) {
return { success: true, element: JSON.parse(result) };
const resultStr = result.toString().trim();
if (type === "inspect") return JSON.parse(resultStr);
if (resultStr === "true") return { success: true };
try {
return { success: true, data: JSON.parse(resultStr) };
} catch (err) {
return { success: true, data: resultStr };
}
}
} catch (err) {
error = err.toString();
error = err.toString().trim();
}
return { success: true, error };
};
/**
* 设置元素值
* @param {string} method 窗口类型:"title"|"handle"|"process"|"class"|"active"
* @param {string} window 窗口标题、句柄、进程名、类名
* @param {string} by 查找方式:"name"|"class"|"type"|"automationid"
* @param {string} value 查找值
* @param {string} newValue 要设置的值
* @param {object} options 选项
* @param {boolean} options.background 是否后台操作
* @returns {Object} 操作结果
* @property {boolean} success 是否成功
* @property {Object} element 操作的元素信息
* @property {Object} element.window 操作的元素所在的窗口信息
*/
const setElementValue = async function (
method,
window,
by,
value,
newValue,
options = {}
) {
const { background = false } = options;
const args = [
"-type",
"setvalue",
"-by",
by,
"-value",
value,
"-newvalue",
newValue,
];
args.push("-method", method);
if (method !== "active" && window) args.push("-window", window);
if (background) args.push("-background");
let error;
try {
const result = await runCsharpFeature("automation", args);
if (result) return { success: true, element: JSON.parse(result) };
} catch (err) {
error = err.toString();
}
if (type === "inspect") return { error };
return { success: false, error };
};
/**
* 获取元素值
* @param {string} method 窗口类型:"title"|"handle"|"process"|"class"|"active"
* @param {string} window 窗口标题、句柄、进程名、类名
* @param {string} by 查找方式:"name"|"class"|"type"|"automationid"
* @param {string} value 查找值
* @param {object} options 选项
* @param {boolean} options.background 是否后台操作
* @returns {object} 元素值
*/
const getElementValue = async function (
method,
window,
by,
value,
options = {}
) {
const { background = false } = options;
const args = ["-type", "getvalue", "-by", by, "-value", value];
args.push("-method", method);
if (method !== "active" && window) args.push("-window", window);
if (background) args.push("-background");
let error;
try {
const result = await runCsharpFeature("automation", args);
if (result) return JSON.parse(result);
} catch (err) {
error = err.toString();
}
return { success: false, error };
};
/**
* 检查元素
* @param {object} options 选项
* @param {number} options.timeout 超时时间(秒)
* @returns {object} 元素信息
*/
const inspectElement = async function () {
const args = ["-type", "inspect"];
try {
const result = await runCsharpFeature("automation", args);
if (result) return JSON.parse(result);
} catch (err) {
console.log(err);
}
return [];
};
}
module.exports = {
listElements,
clickElement,
setElementValue,
getElementValue,
inspectElement,
inspect: () => runAutomation("inspect"),
inspectPosition: (position) =>
runAutomation("inspect", null, null, null, position || {}),
click: (...args) => runAutomation("click", ...args),
setvalue: (...args) => runAutomation("setvalue", ...args),
getvalue: (...args) => runAutomation("getvalue", ...args),
select: (...args) => runAutomation("select", ...args),
expand: (...args) => runAutomation("expand", ...args),
scroll: (...args) => runAutomation("scroll", ...args),
wait: (...args) => runAutomation("wait", ...args),
focus: (...args) => runAutomation("focus", ...args),
highlight: (...args) => runAutomation("highlight", ...args),
sendkeys: (...args) => runAutomation("sendkeys", ...args),
};

View File

@@ -1,140 +1,114 @@
const { runCsharpFeature } = require("../../csharp");
/**
* 键盘操作
* @param {string} method 窗口类型:"title"|"handle"|"process"|"class"|"active"
* @param {string} window 窗口标题、句柄、进程名、类名
* @param {string} keys 键盘按键
* @param {object} options 选项
* @param {string} options.control 控件类名
* @param {boolean} options.background 是否后台操作
* @returns {Object} 操作结果
* @property {boolean} success 是否成功
* @property {Object} control 控件信息
* @property {string} control.window 控件所在窗口的信息
* 执行消息发送操作
* @param {string} type - 操作类型, 可选值: "keyboard"|"mouse"|"inspect"
* @param {Object} params - 参数对象
* @param {string} params.method - 查找方式:"title"|"handle"|"process"|"class"|"active"
* @param {string} params.window - 窗口标题、句柄、进程名、类名
* @param {string} params.action - 动作类型,如 "keys"、"text"、"click" 等
* @param {string} params.value - 操作的值,如按键、文本等
* @param {Object} params.options - 附加选项
* @returns {Promise<Object>} - 操作结果
*/
const sendKeys = async function (method, window, keys, options = {}) {
const { control, background = false } = options;
const args = ["-type", "keyboard", "-action", "keys", "-value", keys];
async function runSendMessage(type, params = {}) {
const args = ["-type", type];
const { method = "active", window, action, value, options = {} } = params;
const { control, text, pos, filter, background = false } = options;
// 通用参数
args.push("-method", method);
if (method !== "active" && window) args.push("-window", window);
if (control) args.push("-control", control);
args.push("-background", background.toString());
let error;
try {
const result = await runCsharpFeature("sendmessage", args);
if (result) {
return {
success: true,
control: JSON.parse(result),
};
}
} catch (err) {
error = err.toString();
if (method !== "active" && window) {
args.push("-window", window);
}
return { success: false, error };
};
/**
* 发送文本
* @param {string} method 窗口类型:"title"|"handle"|"process"|"class"|"active"
* @param {string} window 窗口标题、句柄、进程名、类名
* @param {string} text 文本
* @param {object} options 选项
* @param {string} options.control 控件类名
* @param {boolean} options.background 是否后台操作
* @returns {Object} 操作结果
* @property {boolean} success 是否成功
* @property {Object} control 控件信息
* @property {string} control.window 控件所在窗口的信息
*/
const sendText = async function (method, window, text, options = {}) {
const { control, background = false } = options;
const args = ["-type", "keyboard", "-action", "text", "-value", text];
// 特定命令的参数处理
switch (type) {
case "keyboard":
args.push("-action", action);
if (value) {
args.push("-value", value);
}
if (control) {
args.push("-control", control);
}
break;
args.push("-method", method);
if (method !== "active" && window) args.push("-window", window);
if (control) args.push("-control", control);
args.push("-background", background.toString());
let error;
try {
const result = await runCsharpFeature("sendmessage", args);
if (result) {
return { success: true, control: JSON.parse(result) };
}
} catch (err) {
error = err.toString();
case "mouse":
args.push("-action", action);
if (control) {
args.push("-control", control);
}
if (text) {
args.push("-text", text);
}
if (pos) {
args.push("-pos", pos);
}
break;
case "inspect":
if (filter) {
args.push("-filter", filter);
}
break;
}
return { success: false, error };
};
/**
* 鼠标点击
* @param {string} method 窗口类型:"title"|"handle"|"process"|"class"|"active"
* @param {string} window 窗口标题、句柄、进程名、类名
* @param {string} action 动作:"click"|"doubleClick"|"rightClick"
* @param {object} options 选项
* @param {string} options.control 控件类名
* @param {string} options.text 控件文本
* @param {string} options.pos 点击位置x,y
* @param {boolean} options.background 是否后台操作
* @returns {Object} 操作结果
* @property {boolean} success 是否成功
* @property {Object} control 控件信息
* @property {string} control.window 控件所在窗口的信息
*/
const click = async function (method, window, action = "click", options = {}) {
const { control, text, pos, background = false } = options;
const args = ["-type", "mouse", "-action", action];
args.push("-method", method);
if (method !== "active" && window) args.push("-window", window);
if (control) args.push("-control", control);
if (text) args.push("-text", text);
if (pos) args.push("-pos", pos);
// 后台操作参数
args.push("-background", background.toString());
let error;
try {
const result = await runCsharpFeature("sendmessage", args);
if (result) {
return { success: true, control: JSON.parse(result) };
const jsonResult = JSON.parse(result);
if (type === "inspect") {
return jsonResult;
}
return { success: true, control: jsonResult };
}
} catch (err) {
error = err.toString();
error = err
.toString()
.replace(/^Error: /, "")
.trim();
}
if (type === "inspect") return [];
return { success: false, error };
};
/**
* 获取窗口控件树
* @param {string} method 窗口类型:"title"|"handle"|"process"|"class"|"active"
* @param {string} window 窗口标题、句柄、进程名、类名
* @param {object} options 选项
* @param {string} options.filter 过滤条件
* @param {boolean} options.background 是否后台操作
* @returns {object} 控件树
*/
const inspectWindow = async function (method, window, options = {}) {
const { filter, background = false } = options;
const args = ["-type", "inspect"];
args.push("-method", method);
if (method !== "active" && window) args.push("-window", window);
if (filter) args.push("-filter", filter);
args.push("-background", background.toString());
try {
const result = await runCsharpFeature("sendmessage", args);
if (result) return JSON.parse(result);
} catch (error) {
console.log(error);
}
return [];
};
}
module.exports = {
sendKeys,
sendText,
click,
inspectWindow,
sendKeys: (method, window, keys, options = {}) =>
runSendMessage("keyboard", {
method,
window,
action: "keys",
value: keys,
options,
}),
sendText: (method, window, text, options = {}) =>
runSendMessage("keyboard", {
method,
window,
action: "text",
value: text,
options,
}),
click: (method, window, action = "click", options = {}) =>
runSendMessage("mouse", {
method,
window,
action,
options,
}),
inspectWindow: (method, window, options = {}) =>
runSendMessage("inspect", {
method,
window,
options,
}),
};

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 }),
};