docs: 更新包文档,匹配 ADB 隧道架构和当前 API
This commit is contained in:
@@ -27,11 +27,10 @@ type Device struct {
|
||||
serial string // 设备序列号
|
||||
|
||||
// UIAutomator2 服务配置
|
||||
serverPort int // 设备端服务端口(默认 9008)
|
||||
debug bool // 调试模式
|
||||
jarPath string // 本地 u2.jar 路径(空字符串使用默认路径)
|
||||
serverPort int // 设备端服务端口(默认 9008)
|
||||
debug bool // 调试模式
|
||||
|
||||
// 设备连接接口(通过 adb forward 连接)
|
||||
// 设备连接接口(通过 ADB 隧道直连)
|
||||
dev AdbDevice
|
||||
|
||||
// JSON-RPC 调用器
|
||||
@@ -49,16 +48,15 @@ type Device struct {
|
||||
}
|
||||
|
||||
// NewDevice 创建一个新的 Device 客户端并启动 UIAutomator2 服务
|
||||
// 使用 adb forward 端口转发,与 Python uiautomator2 相同的方案
|
||||
// 通过 ADB 隧道直连设备,与 Python uiautomator2 完全一致
|
||||
//
|
||||
// serial: 设备序列号(如 "emulator-5554")
|
||||
// addr: 可选,ADB 服务器地址,不传则使用默认值 "127.0.0.1:5037"
|
||||
//
|
||||
// 创建后会自动执行:
|
||||
// 1. 设置 adb forward 端口转发
|
||||
// 2. 推送 u2.jar 到设备(如果尚未存在)
|
||||
// 3. 启动 UIAutomator2 服务
|
||||
// 4. 等待服务就绪
|
||||
// 1. 推送内嵌的 u2.jar 到设备(如果尚未存在)
|
||||
// 2. 启动 UIAutomator2 服务
|
||||
// 3. 等待服务就绪
|
||||
func NewDevice(serial string, addr ...string) (*Device, error) {
|
||||
a := DefaultADBAddr
|
||||
if len(addr) > 0 && addr[0] != "" {
|
||||
@@ -80,8 +78,8 @@ func NewDevice(serial string, addr ...string) (*Device, error) {
|
||||
return d.jsonrpcCall(method, params, timeout)
|
||||
})
|
||||
|
||||
// 推送 u2.jar 到设备(仅在文件不存在时推送)
|
||||
if err := services.InstallServiceJar(a, serial, d.jarPath, false); err != nil {
|
||||
// 推送内嵌的 u2.jar 到设备(仅在文件不存在时推送)
|
||||
if err := services.InstallServiceJar(a, serial, false); err != nil {
|
||||
return nil, fmt.Errorf("安装 u2.jar 失败: %w", err)
|
||||
}
|
||||
|
||||
@@ -200,12 +198,6 @@ func (d *Device) checkAlive() bool {
|
||||
return string(resp.Content) == "pong"
|
||||
}
|
||||
|
||||
// SetJarPath 设置本地 u2.jar 路径
|
||||
// 传空字符串则使用默认路径 (assets/u2.jar)
|
||||
func (d *Device) SetJarPath(path string) {
|
||||
d.jarPath = path
|
||||
}
|
||||
|
||||
// launchAndWait 启动 UIAutomator2 进程并等待就绪
|
||||
func (d *Device) launchAndWait() error {
|
||||
// 通过 ADB shell 启动 UIAutomator2
|
||||
|
||||
51
libs/doc.go
51
libs/doc.go
@@ -1,45 +1,50 @@
|
||||
// Package libs 提供了完整的 Android UIAutomator2 自动化框架。
|
||||
//
|
||||
// 本包是 Python uiautomator2 的 Go 语言实现,通过 ADB 协议与运行在 Android 设备上的
|
||||
// UIAutomator2 HTTP 服务通信,提供设备控制、UI 操作、文本输入等功能。
|
||||
// 本包是 Python uiautomator2 的 Go 语言实现,通过 ADB 隧道协议与运行在 Android 设备上的
|
||||
// UIAutomator2 HTTP 服务直连,提供设备控制、UI 操作、文本输入等功能。
|
||||
// 零外部依赖,仅使用 Go 标准库。
|
||||
//
|
||||
// 核心组件:
|
||||
// - Device:设备客户端,管理 UIAutomator2 服务生命周期,提供所有设备操作
|
||||
// - UiObject:UI 控件对象,支持点击、输入、滑动、等待等操作
|
||||
// - Selector:UI 元素选择器,支持文本、类名、资源 ID 等多种查询条件
|
||||
// - JsonRpcWrapper:JSON-RPC 2.0 调用封装
|
||||
// - InputMethod:通过 AdbKeyboard 输入法实现快速文本输入
|
||||
// - UiObject:UI 控件对象,支持点击、输入、滑动、等待、滚动等操作
|
||||
// - Selector:UI 元素选择器,支持文本、类名、资源 ID、描述等多种查询条件
|
||||
// - JsonRpcWrapper:JSON-RPC 2.0 调用封装,自动错误映射和重试
|
||||
// - InputMethod:通过 AdbKeyboard 输入法实现快速文本输入(支持中文)
|
||||
// - SwipeExt:扩展滑动操作(按方向、比例滑动)
|
||||
// - WatchContext/Watcher:弹窗/对话框自动监控和处理
|
||||
// - Session:应用会话管理,自动检测应用状态
|
||||
// - Settings:设备配置管理(等待超时、操作延迟等)
|
||||
// - WatchContext:弹窗/对话框自动监控和处理
|
||||
// - Session:应用会话管理,自动检测应用存活状态
|
||||
// - Settings:线程安全的设备配置管理(等待超时、操作延迟等)
|
||||
//
|
||||
// 通信层:
|
||||
// - AdbHTTPConnection:通过 ADB 隧道发送 HTTP 请求
|
||||
// - AdbTunnelDevice:通过 ADB 隧道直连设备,与 Python uiautomator2 方案完全一致
|
||||
// - http.Transport:自定义 DialContext 将 HTTP 连接替换为 ADB 隧道
|
||||
// - HttpRequest:高层 HTTP 请求封装
|
||||
// - JsonRpcCall:JSON-RPC 2.0 请求/响应处理
|
||||
//
|
||||
// 使用示例:
|
||||
//
|
||||
// // 创建设备连接
|
||||
// device, err := libs.NewDevice("emulator-5554")
|
||||
// // 创建设备连接(自动推送 u2.jar、启动服务)
|
||||
// d, err := libs.NewDevice("emulator-5554")
|
||||
// // 自定义 ADB 地址: libs.NewDevice("emulator-5554", "192.168.1.100:5037")
|
||||
// if err != nil {
|
||||
// log.Fatal(err)
|
||||
// }
|
||||
// defer device.StopUiautomator()
|
||||
// defer d.Close()
|
||||
//
|
||||
// // 查找并点击按钮
|
||||
// btn, _ := device.FindElement(map[string]interface{}{"text": "登录"})
|
||||
// btn.Click()
|
||||
// // 便捷选择器,链式调用
|
||||
// d.ByText("登录").Click()
|
||||
// d.ByResourceId("com.example:id/input").SetText("admin")
|
||||
//
|
||||
// // 输入文本
|
||||
// input, _ := device.FindElement(map[string]interface{}{"resourceId": "com.example:id/username"})
|
||||
// input.SetText("admin")
|
||||
// // 多条件组合查找
|
||||
// d.By(libs.P{"className": "android.widget.Button", "text": "确定"}).Click()
|
||||
//
|
||||
// // 使用 AdbKeyboard 输入中文
|
||||
// im := libs.NewInputMethod(d)
|
||||
// im.SendKeys("你好世界")
|
||||
//
|
||||
// // 使用 Watcher 自动处理弹窗
|
||||
// watcher := libs.NewWatcher(device)
|
||||
// watcher.WhenText("同意").Click()
|
||||
// watcher.Start(2.0)
|
||||
// defer watcher.Stop()
|
||||
// w := libs.NewWatchContext(d, true)
|
||||
// w.WhenText("允许").Click()
|
||||
// w.Start()
|
||||
// defer w.Stop()
|
||||
package libs
|
||||
|
||||
Reference in New Issue
Block a user