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