跳到主要内容

🚀 快速入门

快速入门

说明

5 分钟学会使用 ui 模块进行自动化操作。

待实现

该模块正在开发中。


App 控件自动化

说明

1. 查找控件

local node = require("node")

-- 通过 ID 查找
local n = node.selector():id("com.example:id/btn_login"):find()

-- 通过文本查找
local n = node.selector():text("登录"):find()

-- 组合条件
local n = node.selector()
:type("Button")
:text("确定")
:clickable(true)
:find()

2. 等待控件出现

local node = require("node")

-- 循环等待控件出现
local function waitFor(selector, timeout)
timeout = timeout or 10
local startTime = os.time()
while os.time() - startTime < timeout do
local n = selector:find()
if node then
return node
end
sys.msleep(500)
end
return nil
end

-- 使用
local node = waitFor(node.selector():text("登录"), 5)
if node then
node:click()
end

3. 点击和输入

local node = require("node")

-- 点击控件
local btn = node.selector():text("登录"):find()
if btn then
btn:click()
end

-- 输入文本
local input = node.selector():type("EditText"):find()
if input then
input:input("hello world")
end

4. 滑动列表

local node = require("node")

-- 找到可滑动控件
local list = node.selector():type("RecyclerView"):find()
if list then
list:slide(1) -- 向前滑动
list:slide(-1) -- 向后滑动
end

5. 遍历子控件

local node = require("node")

local container = node.selector():id("com.example:id/list"):find()
if container then
-- 获取所有子控件
local children = container:child()
if children then
for i, child in ipairs(children) do
print(i, child.text)
end
end
end

网页控件自动化

说明

1. 创建 Web 窗口

local node = require("node")
local node = require("node")

local window = node.web_window("http://www.example.com")

2. 查找网页元素

-- 通过文本查找
local n = node.web_selector(window):text("登录"):find()

-- 通过标签查找
local divs = node.web_selector(window):tag("div"):find_all()

-- 通过属性查找
local btn = node.web_selector(window):attr("class", "btn-primary"):find()

3. 表单操作

local node = require("node")
local node = require("node")

local window = node.web_window("http://www.example.com/login")

-- 输入用户名
local username = node.web_selector(window):attr("name", "username"):find()
if username then
username:input("admin")
end

-- 输入密码
local password = node.web_selector(window):attr("name", "password"):find()
if password then
password:input("123456")
end

-- 点击登录
local submit = node.web_selector(window):text("登录"):find()
if submit then
submit:click()
end

常见问题

说明

Q: 找不到控件怎么办?

  1. 检查控件是否可见 - 使用 visible(true) 约束
  2. 等待页面加载 - 添加适当的延时或循环等待
  3. 检查约束条件 - 使用 Selector.dump() 查看控件树

Q: 点击无效怎么办?

  1. 检查 clickable 属性 - 控件可能不可点击
  2. 尝试父控件 - 使用 parent() 获取父控件再点击
  3. 使用坐标点击 - 获取控件位置后使用 touch 模块

Q: 如何调试?

-- 打印控件树
local json = node.dump()
print(json)

-- 打印控件属性
local n = node.selector():text("xxx"):find()
if node then
print("text:", node.text)
print("type:", node.type)
print("rect:", node.rect.left, node.rect.top)
end