接口: RootAutomator2
root_automator.RootAutomator2
RootAutomator2 用于基于 root 或者 adb 权限,模拟点击、手势、长按等操作。相比起基于无障碍的操作,RootAutomator 支持多点触控和动态改变手势;相比 RootAutomator,RootAutomator2 的兼容性更佳。
提示
从 Pro 9.3 开始推荐使用 RootAutomator2 代替 RootAutomator,相比 RootAutomator,它有更好的设备兼容性。
目录
方法
方法
tap
▸ tap(x, y): Promise<void>
点击位置 (x, y),时长为 5 毫秒。
示例
"nodejs";
const { createRootAutomator2 } = require("root_automator");
async function main() {
const ra = await createRootAutomator2({ root: true });
// 点击 (200, 200) 的位置
await ra.tap(200, 200);
await ra.exit();
}
main();参数
| 名称 | 类型 | 描述 |
|---|---|---|
x | number | X 坐标 |
y | number | Y 坐标 |
返回值
Promise<void>
返回一个 Promise 对象,当操作完成时,Promise 对象会被 resolve。
swipe
▸ swipe(x1, y1, x2, y2, duration): Promise<void>
在给定的 duration 时长从 (x1, y1) 位置滑动到 (x2, y2) 位置。
示例
"nodejs";
const { createRootAutomator2 } = require("root_automator");
async function main() {
const ra = await createRootAutomator2({ root: true });
// 从 (1000, 50) 滑动到 (1000, 1000) 的位置,滑动时长 300ms
await ra.swipe(1000, 50, 1000, 1000, 300);
await ra.exit();
}
main();参数
| 名称 | 类型 | 描述 |
|---|---|---|
x1 | number | 起始 X 坐标 |
y1 | number | 起始 Y 坐标 |
x2 | number | 结束 X 坐标 |
y2 | number | 结束 Y 坐标 |
duration | number | 滑动时长,单位毫秒 |
返回值
Promise<void>
返回一个 Promise 对象,当操作完成时,Promise 对象会被 resolve。
press
▸ press(x, y, duration): Promise<void>
按下 (x, y) 位置持续 duration 时长,然后抬起手指。
示例
"nodejs";
const { createRootAutomator2 } = require("root_automator");
async function main() {
const ra = await createRootAutomator2({ root: true });
// 按下 (1000, 500) 位置持续 2 秒
await ra.press(1000, 500, 2000);
await ra.exit();
}
main();参数
| 名称 | 类型 | 描述 |
|---|---|---|
x | number | X 坐标 |
y | number | Y 坐标 |
duration | number | 按下时长,单位毫秒 |
返回值
Promise<void>
返回一个 Promise 对象,当操作完成时,Promise 对象会被 resolve。
longPress
▸ longPress(x, y): Promise<void>
长按 (x, y) 位置。长按的时长为 ViewConfiguration.getLongPressTimeout()+100 毫秒。
示例
"nodejs";
const { createRootAutomator2 } = require("root_automator");
async function main() {
const ra = await createRootAutomator2({ root: true });
// 长按 (1000, 500) 的位置
await ra.longPress(1000, 500);
await ra.exit();
}
main();参数
| 名称 | 类型 | 描述 |
|---|---|---|
x | number | X 坐标 |
y | number | Y 坐标 |
返回值
Promise<void>
返回一个 Promise 对象,当操作完成时,Promise 对象会被 resolve。
touchDown
▸ touchDown(x, y, id?): Promise<void>
按下 (x, y) 位置。若对应 id 的手指之前已经是按下状态,则会模拟手指移动 (touchMove) 事件。
示例
"nodejs";
const { createRootAutomator2 } = require("root_automator");
async function main() {
const ra = await createRootAutomator2({ root: true });
// 按下 (100, 100) 位置
await ra.touchDown(100, 100);
await ra.exit();
}
main();参数
| 名称 | 类型 | 默认值 | 描述 |
|---|---|---|---|
x | number | undefined | X 坐标 |
y | number | undefined | Y 坐标 |
id | number | 0 | 手指 ID,默认为 0 |
返回值
Promise<void>
touchDown
▸ touchDown(pointers): Promise<void>
模拟一个手指按下事件,使用数组描述触摸的位置和相应的手指 id。例如 ra.touchDown([{x: 100, y: 100, id: 0}, {x: 200, y: 200, id: 1}]) 会使用手指 0 按下位置 (100, 100),使用手指 1 按下位置 (200, 200)。
示例
"nodejs";
const { createRootAutomator2 } = require("root_automator");
async function main() {
const ra = await createRootAutomator2({ root: true });
// 使用两个手指同时按下
await ra.touchDown([
{ x: 100, y: 100, id: 0 },
{ x: 200, y: 200, id: 1 },
]);
await ra.touchUp();
await ra.exit();
}
main();参数
| 名称 | 类型 | 描述 |
|---|---|---|
pointers | Pointer[] | 指针数组,描述多个触摸点的位置和对应的手指 id |
返回值
Promise<void>
touchUp
▸ touchUp(id?): Promise<void>
抬起手指。
参数
| 名称 | 类型 | 默认值 | 描述 |
|---|---|---|---|
id | number | undefined | 手指 ID,若不指定则抬起所有手指 |
返回值
Promise<void>
touchUp
▸ touchUp(pointers): Promise<void>
模拟一个手指抬起事件,使用数组描述触摸的位置和相应的手指 id。例如 ra.touchUp([{x: 100, y: 100, id: 0}, {x: 200, y: 200, id: 1}]) 会使用手指 0 和手指 1 抬起,其中的坐标位置为手指抬起时的位置。
示例
"nodejs";
const { createRootAutomator2 } = require("root_automator");
async function main() {
const ra = await createRootAutomator2({ root: true });
// 按下两个手指
await ra.touchDown([
{ x: 100, y: 100, id: 0 },
{ x: 200, y: 200, id: 1 },
]);
// 抬起两个手指
await ra.touchUp([
{ x: 150, y: 150, id: 0 },
{ x: 250, y: 250, id: 1 },
]);
await ra.exit();
}
main();参数
| 名称 | 类型 | 描述 |
|---|---|---|
pointers | Pointer[] | 指针数组,描述多个触摸点的位置和对应的手指 id |
返回值
Promise<void>
touchMove
▸ touchMove(x, y, id?): void
将手指移动到 (x, y) 位置。若对应 id 的手指之前并非按下状态,则会模拟手指按下 (touchDown) 事件。
示例
"nodejs";
const { createRootAutomator2 } = require("root_automator");
async function main() {
const ra = await createRootAutomator2({ root: true });
// 按下
await ra.touchDown(100, 100);
// 移动手指
ra.touchMove(200, 200);
ra.touchMove(300, 300);
// 抬起
await ra.touchUp();
await ra.flush();
await ra.exit();
}
main();参数
| 名称 | 类型 | 默认值 | 描述 |
|---|---|---|---|
x | number | undefined | X 坐标 |
y | number | undefined | Y 坐标 |
id | number | 0 | 手指 ID,默认为 0 |
返回值
void
touchMove
▸ touchMove(pointers): Promise<void>
模拟一个手指移动事件,使用数组描述触摸的位置和相应的手指 id。例如 ra.touchMove([{x: 100, y: 100, id: 0}, {x: 200, y: 200, id: 1}]) 会将手指 0 移动到位置 (100, 100),将手指 1 移动到位置 (200, 200)。
示例
"nodejs";
const { createRootAutomator2 } = require("root_automator");
async function main() {
const ra = await createRootAutomator2({ root: true });
// 按下两个手指
await ra.touchDown([
{ x: 100, y: 100, id: 0 },
{ x: 200, y: 200, id: 1 },
]);
// 移动两个手指
await ra.touchMove([
{ x: 150, y: 150, id: 0 },
{ x: 250, y: 250, id: 1 },
]);
await ra.touchUp();
await ra.flush();
await ra.exit();
}
main();参数
| 名称 | 类型 | 描述 |
|---|---|---|
pointers | Pointer[] | 指针数组,描述多个触摸点的位置和对应的手指 id |
返回值
Promise<void>
flush
▸ flush(): Promise<void>
等待所有操作完成。例如我们使用 touchDown、touchMove、touchUp 完成了一系列手势,需要等待这些手势完成后继续下一步时,使用 await ra.flush() 来异步等待。
示例
"nodejs";
const { createRootAutomator2 } = require("root_automator");
async function main() {
const ra = await createRootAutomator2({ root: true });
// 执行一系列手势操作
await ra.touchDown(100, 100);
ra.touchMove(200, 200);
ra.touchMove(300, 300);
await ra.touchUp();
// 等待所有操作完成
await ra.flush();
console.log("所有手势操作已完成");
await ra.exit();
}
main();返回值
Promise<void>
返回一个 Promise 对象,当所有待处理的操作完成时 resolve。
exit
▸ exit(forced?): Promise<void>
退出 RootAutomator2。
参数
| 名称 | 类型 | 默认值 | 描述 |
|---|---|---|---|
forced | boolean | false | 如果为 true,将不等待未完成的操作,而是尽可能快地退出;如果为 false,在所有未完成的操作结束后退出进程 |
返回值
Promise<void>
返回一个 Promise 对象,在退出完成后 resolve。
