1. weditor 简介
在使用AccessibilityService
开发自动化工具或自动化测试脚本时需要知道控件属性才能自动化逻辑。weditor
是一个用于 Android UI 自动化测试的可视化工具,它提供一个 Web 界面,方便查看 Android 设备 UI 层级、获取控件属性。
2. 安装前的准备
在安装 weditor
之前,需要先安装以下依赖环境:
2.1 安装 Python
weditor
依赖 Python 运行环境,建议使用 Python 3.6 及以上版本。
-
在 Windows 上,可以从 Python 官网 下载并安装。
-
在 macOS/Linux 上,可以使用包管理工具安装,如:
bashsudo apt install python3 # Ubuntu/Debian brew install python3 # macOS
3. 安装 weditor
使用 pip
命令安装 weditor
:
pip install -U weditor
安装完成后,可以通过以下命令检查是否安装成功:
bash
weditor --help
如果输出帮助信息,则说明安装成功。
4. 连接 Android 设备
4.1 启用 USB 调试模式
在手机上开启 开发者模式
并启用 USB 调试
。
4.2 连接设备
通过 USB 连接手机,并运行以下命令查看设备是否被识别:
adb devices
如果设备列表中显示了设备编号,则表示连接成功。
5. 启动 weditor
在终端执行以下命令启动 weditor
:
weditor
默认情况下,它会在 localhost:17310
启动一个 Web 服务器。在浏览器中访问:
arduino
http://localhost:17310
即可打开 weditor
界面,并且显示手机当前的界面即可查看节点元素信息,如下:
6. 使用Assists
实现简单自动点击和输入
Assists是一个基于AccessibilityService
封装的开发框架,主要是为了简化自动化脚本开发、提供增强能力、提高代码易维护性的一个框架。
6.1集成Assists
项目根目录build.gradle添加
以下是Groovy DSL写法,Kotlin DSL的可GPT问下
rust
allprojects {
repositories {
//添加jitpack仓库
maven { url 'https://jitpack.io' }
}
}
主模块build.gradle添加
arduino
dependencies {
//按需添加
//基础库(必须)
implementation "com.github.ven-coder.Assists:assists-base:v3.2.11"
//屏幕录制相关(可选)
implementation "com.github.ven-coder.Assists:assists-mp:v3.2.11"
//opencv相关(可选)
implementation "com.github.ven-coder.Assists:assists-opcv:v3.2.11"
}
主模块AndroidManifest.xml中注册服务
xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.ven.assists.simple">
<application
android:name="com.ven.assists.simple.App"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:requestLegacyExternalStorage="true"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:usesCleartextTraffic="true">
<!-- 添加代码 ↓-->
<service
android:name="com.ven.assists.service.AssistsService"
android:enabled="true"
android:exported="true"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
<!--android:priority="10000" 可提高服务在设置中的权重,排在前面-->
<intent-filter android:priority="10000">
<action android:name="android.accessibilityservice.AccessibilityService" />
</intent-filter>
<meta-data
android:name="android.accessibilityservice"
android:resource="@xml/assists_service" />
</service>
<!-- 添加代码 ↑-->
</application>
</manifest>
6.2 实现点击微信搜索并输入搜索内容
6.2.1 获取微信搜索节点元素id
注意:
选中的搜索节点的clickable
需要是true
才行,代表该节点是可点击的
6.2.2 实现点击
kotlin
class SimpleStep: StepImpl() {
override fun onImpl(collector: StepCollector) {
collector.next(stepTag = 1){
//第1步
"点击搜索".overlayToast()
delay(1000)
AssistsCore.findById("com.tencent.mm:id/jha").firstOrNull()?.click()
//执行第2步
return@next Step.get(2, delay = 2000)
}
}
}
6.2.3 查找输入框并输入文字
除了可以通过id获取节点,还可以通过节点类型获取
6.2.4 实现输入
kotlin
class SimpleStep: StepImpl() {
override fun onImpl(collector: StepCollector) {
collector.next(stepTag = 1){
//第1步
"点击搜索".overlayToast()
delay(1000)
AssistsCore.findById("com.tencent.mm:id/jha").firstOrNull()?.click()
//执行第2步
return@next Step.get(2, delay = 2000)
}.next(stepTag = 2){
//第2步
"输入搜索内容".overlayToast()
delay(1000)
AssistsCore.findByTags("android.widget.EditText").firstOrNull()?.setNodeText("自动输入的搜索内容")
//结束执行
return@next Step.none
}
}
}