Documentation in <https://github.com/openatx/uiautomator2>
一、环境
(1)安装依赖
pip install atx
pip install --pre --upgrade uiautomator2
pip install --pre --upgrade weditor
(2)手机链接电脑后,初始化:
python -m uiautomator2 init
(3)打开ATX:
python -m weditor
选择设备,输入设备号(或设备链接的WIFI地址,需要和电脑同一个WIFI),点击Connect
二、测试脚本
简易脚本1:
#encoding utf-8
import uiautomator2 as u2
from time import sleep
'''
(1)wifi连接:d=u2.connect('172.21.1.89')
usb 链接的device:3ac86305
(2)测试的手机管家APP包名:com.coloros.phonemanager
(3)
ResourceId定位:
d(resourceId="com.coloros.phonemanager:id/item_entry_img")
Text定位:
d(text="清理存储")
Description定位:
d(description=" ")
ClassName定位:
d(className="android.widget.RelativeLayout")
'''
#USB方式连接设备
d=u2.connect('3ac86305')
# 启动手机管家App
d.app_start("com.coloros.phonemanager")
# 点击清理缓存
d(resourceId="com.coloros.phonemanager:id/item_entry_img").click()
# 点击一键清理
d(resourceId="com.coloros.phonemanager:id/clear_advice_preference_button_layout").click()
sleep(5)
#退出手机管家App
d.app_stop("com.coloros.phonemanager")
脚本2:用unnitest执行用例,包含测试过程记录报告
#encoding utf-8
import uiautomator2 as u2
from time import sleep
from uiautomator2.ext.htmlreport import HTMLReport
import unittest
'''
(1)连接:
wifi连接:
d=u2.connect('192.168.1.89')#手机和电脑同一个WIFI,此为手机IP地址
usb连接:
u2.connect('3ac86305')#device:3ac86305
(2)包名:
手机管家APP包名:com.coloros.phonemanager
(3)定位:
ResourceId定位:
d(resourceId="com.coloros.phonemanager:id/item_entry_img")
Text定位:
d(text="清理存储")
Description定位:
d(description=" ")
ClassName定位:
d(className="android.widget.RelativeLayout")
(4)报告:
from uiautomator2.ext.htmlreport import HTMLReport
html_report=HTMLReport(self.d,'report')
html_report.patch_click()
(5)执行:
规范unnitest:
class名:首字母大写,包含Test
初始化环境:setUp(),U大写
清理环境:tearDown(),D大写
测试用例方法:test开头,加上和测试模块相关的字母缩写(自定义发挥,能看懂测撒)
'''
class PhmTest(unittest.TestCase):
def setUp(self):
self.d=u2.connect('3ac86305')
html_report=HTMLReport(self.d,'report')
html_report.patch_click()
self.d.app_start("com.coloros.phonemanager")
pass
def testclear(self):
# 点击清理缓存
self.d(resourceId="com.coloros.phonemanager:id/item_entry_img").click()
# 点击一键清理
self.d(resourceId="com.coloros.phonemanager:id/clear_advice_preference_button_layout").click()
sleep(5)
def tearDown(self):
self.d.app_stop("com.coloros.phonemanager")
pass
if __name__=='__main__':
unittest.main()
执行后,报告如下:
3-pytest的脚本:
#encoding utf-8 import uiautomator2 as u2 from time import sleep from uiautomator2.ext.htmlreport import HTMLReport import pytest import allure import os from Tools import logger ''' (1)连接: wifi连接: d=u2.connect('192.168.1.89')#手机和电脑同一个WIFI,此为手机IP地址 usb连接: u2.connect('3ac86305')#device:3ac86305 (2)包名: 手机管家APP包名:com.coloros.phonemanager (3)定位: ResourceId定位: d(resourceId="com.coloros.phonemanager:id/item_entry_img") Text定位: d(text="清理存储") Description定位: d(description=" ") ClassName定位: d(className="android.widget.RelativeLayout") (4)报告: from uiautomator2.ext.htmlreport import HTMLReport html_report=HTMLReport(self.d,'report') html_report.patch_click() (5)执行: 规范pytest: class名:首字母大写,包含Test 初始化环境:setup(),u小写 清理环境:teardown(),d小写 测试用例方法:test开头,加上和测试模块相关的字母缩写(自定义发挥,能看懂测撒) ''' LOG = logger.Logger("TestPhm").getlog() class TestPhm(): def setup(self): self.log = LOG self.log.debug("连接设备") self.d=u2.connect('3ac86305')
#self.d.healthcheck() # 解锁屏幕并启动uiautomator服务html_report=HTMLReport(self.d,'report') html_report.patch_click() self.log.debug("启动手机管家APP") self.d.app_start("com.coloros.phonemanager") pass @allure.MASTER_HELPER.step("清理缓存") def test_clear(self): # 点击清理缓存 self.log.debug("点击清理缓存") self.d(resourceId="com.coloros.phonemanager:id/item_entry_img").click() # 点击一键清理 self.log.debug("点击一键清理") onece_clear="com.coloros.phonemanager:id/clear_advice_preference_button_layout" self.d(resourceId=onece_clear).click() #等待清理完成 sleep(1) self.log.debug("清理完成") assert self.d(resourceId=onece_clear).exists()==False def teardown(self): self.log.debug("退出APP") self.d.app_stop("com.coloros.phonemanager") pass if __name__ == '__main__': ''' cmd生成HTML报告 allure generate <xml路径> -o <html路径> --clean cmd查看HTML报告 allure open -h 127.0.0.1 -p 8083 <html路径> xml、html的报告路径 ''' path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) file_result = path + "/py_result/xml" file_report=path+"/py_result/html" pytest.main(['-s', '-q', '--alluredir', file_result]) 执行后,控制台输出日志: allure报告长这样:
三、资料:
GitHub地址:https://github.com/openatx/uiautomator2
https://www.cnblogs.com/fnng/p/8486863.html
https://blog.csdn.net/ricky_yangrui/article/details/81460848
https://blog.csdn.net/xinjing2018/article/details/80260529
ATX 安卓设备集群管理 atx-server https://testerhome.com/topics/11546