安卓自動化測試:uiautomator2+pytest+allure
本文主要講解使用uiautomator2+pytest+allure進行Android的UI自動化測試,也可以通過寫一些實戰的指令碼來更深入學習pytest框架。
學習地址:uiautomator2+pytest+allure測試框架介紹
踩坑記錄:https://www.jianshu.com/p/e9c542632f2c
基礎開發環境:
macOS 10.14
Python 3.7.2
環境搭建
mac電腦自帶python2.7, 所以想用python3.X來安裝相關庫時,建議使用pip3 install ...
pytest
最新版本出到4.0了,但是實際使用4.0和allure有些不相容.
pip3 install pytest==3.7
uiautomator2
uiautomator2也是python的一個類庫,用pip安裝即可.
pip3 install uiautomator2
allure
brew install allure
pip3 install pytest-allure-adaptor
有了測試框架、自動化框架、測試報告,基本上就能coding了。
pytest外掛
pytest外掛可以實現失敗重試、列印進度、指定順序
pip install pytest-sugar # 列印進度 pip install pytest-rerunfailures # 失敗重試 pip install pytest-ordering # 執行順序
當然外掛還有很多,這裡就不一一介紹了。
uiautomator2基本操作
啟動服務
執行如下命令:
python3 -m uiautomator2 init
會在手機上安裝atx-agent.apk並且會在手機上啟動服務:
手機上也會自動訪問以下地址:
監聽的是手機上的ip+預設7921.
檢視元素
安裝
需要安裝weditor庫
pip3 install weditor
啟動工具
python3 -m weditor
會在自動開啟瀏覽器並且展示元素,相當於web版本的uiautomatorviewer,使用起來比較方便:
無線執行
上邊提到的手機ip,有了這個手機ip就可以進行無線執行指令碼
把connect中的方法替換成手機ip就可以了:
# d = ut2.connect(device_name)
d = ut2.connect("192.168.129.93")
demo程式碼
原作者的demo,需要安裝下58同城最新版apk.
程式碼地址:https://github.com/xinxi1990/atxdemo.git
我的測試程式碼,本公司測試小夥伴可以去gitlab 獲取。
報告
allure報告,附幾張跑完用例的截圖:
需要說明的是,檢視報告時,請使用Firefox瀏覽器,或在IDE(如PyCharm)用任意瀏覽器開啟,如直接使用Chrome開啟,報告內容為空。
PS:詳細的uiautomator2使用方法可以移步如下地址:
https://github.com/openatx/uiautomator2
本文為轉發分享!如有侵權請聯絡作者!