(1)使用Appium Desktop中Appium-Inspector辅助进行元素定位
早期版本集成在Appium Desktop中,最新版本已分开。
下载地址:Releases · appium/appium-inspector · GitHub
打开配置页面,输入对应信息即可启动start session。本文以雪球app为例。
【注意】启动前需要首先启动Appium Desktop,否则会报以下错误。
通过Appium Desktop中select elements进行元素识别定位。
更详细可参考:Appium入门:借助Appium Desktop工具辅助元素定位,录制测试用例 - Jayo - 博客园
(2)使用uiautomatorviewer辅助进行元素定位
双击运行uiautomatorviewer.bat,打开UI Automator Viewer,点“Device Screenshot(uiaotomator dump)”
【注意】点“Device Screenshot(uiaotomator dump)”报错。
解决方法:cmd命令下运行adb root,并停止运行Appium Inspector。
再次点“Device Screenshot(uiaotomator dump)”又报以下错误
通过网络搜索查找原因,说UI Automator Viewer仅支持Android 6.0及以下。重新创建Android 6.0模拟器,再次执行,可正常使用。
参考:Error while obtaining UI hierarchy XML file: com.android.ddmlib.SyncExceptio解决办法 - uniquefu - 博客园
附: UI Automator Viewer 无法在Android 8.0以上模拟器直接使用的问题解决方法
android8.0以后sdk tools自带的uiautomator直接打开,截取不到模拟器中机器界面信息。可以使用以下方法手动截取。
【注意】真机UI Automator Viewer可以正常使用,Appium Server正在运行时不能使用。
首先模拟器上操作APP,并定位到要分析的界面,使用adb命令。
adb shell uiautomator dump /sdcard/screen.uix
adb pull /sdcard/screen.uix D:/ui/screen.uix
【注意】如果下载到本地照片直接写磁盘根目录(如D:/),要报下图错,新建路径即可(如D:/ui)。
adb: error: cannot create file/directory 'D:/screen.uix': No such file or directory
注明:新手机地址改成:/data/local/tmp/**.uix
adb shell screencap -p /sdcard/screen.png
adb pull /sdcard/screen.png D:/ui/screen.png
打开sdk tools目录下的uiautomatorviewer.bat,导入从以上步骤获取的截图和资源文件。
鼠标移动到对应元素,右面自动显示当前元素相关信息。如移动到搜索框。
参考:android 8.0 以后 uiautomator 无法直接使用的问题 - Gleaner - 博客园
通过在Appium中设置相关信息也可以(我当前使用的Appium Desktop V1.22.0中未找到设置,暂未尝试),详细可参考:adb SDK 中uiautomatorviewer.bat报错Unexpected error while obtaining UI hierarchy 的解决办法_WAIT Y 的博客-CSDN博客
(3)使用使用Weditor(uiautomator2)替换uiautomatorviewer抓取Android控件
详细参考:浅谈自动化测试工具 python-uiautomator2 · TesterHome
通过pycharm中file-settings-Project-Python Interpreter中,安装uiautomator2
2)设备上安装atx-agent
github下载地址:GitHub - openatx/atx-agent: HTTP Server runs on android devices
首先设备连接到 PC,并能够adb devices发现该设备。
# 从github下载atx-agent文件,并推送到手机。在手机上安装包名为`com.github.uiautomator`的apk $ python -m uiautomator2 init
最后提示 Successfully init AdbDevice(serial=emulator-5554),代表 atx-agent 初始化成功。
安装完成后,cmd命令下运行:python -m weditor
运行后默认打开http://localhost:17310/
选择Andriod,右侧文本框输入设备id,这里输入模拟器设备名称:emulator-5554,之后点connect,如果连接正常则connect旁边出现一个绿色的树叶。如果看不到模拟器界面信息,可以点下右侧Dump Hierarchy就可以看到最新页面了。
- ResourceId 定位: d(resourceId="com.smartisanos.clock:id/text_stopwatch").click()
- Text 定位 d(text="秒表").click()
- Description 定位 d(description="..").click()
- ClassName 定位 d(className="android.widget.TextView").click()
以上仅主要是本人实际安装操作步骤,更详细请参考:浅谈自动化测试工具 python-uiautomator2 · TesterHome