概述:主要是做App客戶端相關自動化操作,實作自動化用例,通過pytest和allure框架完成自動化測驗并輸出測驗報告,在客戶端較穩定的情況下,可多次重復實作自動化回歸,
一、環境搭建
作業系統:Mac 11.5.2
1、Pycharm //官網下載
2、Anaconda(Python3.8)//python --version
3、brew //brew --version
4、Android-sdk //adb --version
5、Appium //官網下載
6、Xcode //App store下載
其他包:
pip install Appium-Python-Client
pip install selenium
pip install -U pytest
pip install allure-pytest
brew install allure
pytest執行全部用例:
py.test test/ --alluredir ./result/
pytest執行部分用例:
py.test test/ --allure_features='購物車功能' --allure_stories='加入購物車'
allure生成報告:
allure generate ./result/ -o ./report/ --clean
二、構建Appium自動化服務器
概述:Appium原理類似Selenium,通過Appium服務器,以代碼驅動去操作客戶端軟體
1、進入Appium
2、port:4723是安卓服務器,點擊start Server

3、點擊右上角放大鏡(Start Inspector Session)

4、添加Desired Capabilities的引數,可參考:
{
"platformName": "Android", //平臺
"deviceName": "nova 5", //設備名
"appPackage": "com.xxx.xxx", //要自動化的App包名
"appActivity": "ui.activity.SplashActivity", //框架,可不修改
"noReset": true,
"unicodeKeyboard": false, //是否使用unicode打字法
"resetKeyboard": true //是否重制打字法
}
5、點擊右下角save
6、連接手機至電腦,點擊Start Session,在手機安卓Appium和Android-sdk插件,
三、撰寫自動化腳本
Start session后,連接成功的話,Appium會需要在手機上安裝2個自動化測驗相關軟體,
Appium服務器會顯示成這樣,
手機點擊某一個app時,點擊右上角重繪,Select Elements選擇元素,右邊Selector可獲取元素的抓取方式,可通過xpath或id獲取,

獲取到元素ID或者Xpath路徑,就可以開始撰寫Python自動化測驗代碼了,
我使用的是Pytest+Allure+Appium框架,
例如:
import allure
import pytest
from appium import webdriver
desired_caps = {
'platformName': 'Android', # 被測手機是安卓
'platformVersion': '10', # 手機安卓版本
'deviceName': 'JPFDU19425xxxx', # 設備名,安卓手機可以隨意填寫
'appPackage': 'com.xxx.xxxx', # 啟動APP Package名稱
'appActivity': '.ui.activity.SplashActivity', # 啟動Activity名稱
# 'unicodeKeyboard': True, # 使用自帶輸入法,輸入中文時填True
'resetKeyboard': True, # 執行完程式恢復原來輸入法
'noReset': True,
'newCommandTimeout': 6000,
'automationName' : 'UiAutomator2'
# 'app': r'd:\apk\bili.apk',
}
class TestGloweAuton(object):
def setup_method(self): //用例初始化加載的方法
self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)//獲取driver
print('用例開始執行')
def teardown_method(self): //用例結束時加載的方法
print('用例執行完畢')
def test_xxxx(self): //測驗用例,以test為開頭
driver = self.driver
driver.find_element_by_id("com.xxx.xxx").click()//通過id獲取元素
driver.implicitly_wait(5) //隱式等待獲取新的元素
四、測驗報告
pycharm點擊下面terminal

pytest執行全部用例命令:
py.test test/ --alluredir ./result/
allure生成報告:
allure generate ./result/ -o ./report/ --clean
通過chrome打開index.html,觀察測驗報告結果

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/302882.html
標籤:其他
上一篇:它為什么是人臉識別中的 “不定時炸彈”?丨獨家公開課實錄
下一篇:Git操作手冊(二)
