使用 ATX+pytest+allure-pytest 進行 IOS 的 UI 自動化測試

Lydia發表於2020-06-02

最近學習了pytest框架,學習過程中練習的程式碼想分享給大家,僅供參考。
參考精華帖https://testerhome.com/topics/17292
本文主要講解使用ATX+pytest+allure-pytest, 進行IOS的UI自動化測試。

ATX官網:https://github.com/openatx/facebook-wda
Allure官網:https://docs.qameta.io/allure/#_get_started
Pytest官網:https://docs.pytest.org/en/latest/reference.html

環境:

macOS mojave 版本10.14.5
Xcode: Version 11.3
WebDriverAgent :官網下載的最新版,解壓後進入Xcode進行配置,具體配置方法後期新建文件詳細說明
Pytest: 安裝命令

pip install pytest==3.7
ATX:安裝命令 pip install -U facebook-wda
Allure: 安裝命令 brew install allure
allure-pytest: 安裝命令 pip install allure-pytest

需要安裝的命令列工具:

libimobiledevice (安裝命令:brew install libimobiledevice)
ideviceinstaller (安裝命令:brew install ideviceinstaller)
ios-deply (安裝命令:brew install ios-deply)
usbmuxd (安裝命令:brew install usbmuxd)

安裝完成後,開啟Xcode配置WebDriverAgent中的project,連線裝置,build成功後Test

執行以下命令獲取需要的資訊:
ios-deploy -c # 獲取已連線的裝置

ideviceinstaller -l # 獲取app列表和資訊

iproxy 8100 8100 # 把當前連線裝置的8100埠(SSH埠)對映到電腦的8100埠

此時進入瀏覽器輸入地址:http://localhost:8100/status,效果如圖,說明環境配置成功:


專案例項:

專案目錄:

說明:
config.py檔案中配置了全域性變數

conftest.py檔案為自定義的fixture,實現了setup,teardown,元素資料的傳遞,以及截圖功能

import pytest
import yaml
import time
from driver import Driver
from config import *
from tools.loggers import JFMlogging
logger = JFMlogging().getloger()


@pytest.fixture()
def ios_driver_setup():
logger.info("自動化測試開始!")
request = Driver().init_ios_driver()
# print(request.app_current())
logger.info("建立session")
s = request.session("com.tencent.xin")
# print(s.orientation)
# ios_allow(request)
# 進入微信小程式
s(name='發現').tap()
s(name='小程式').tap()
s(name='魔比UAT').tap()
s.implicitly_wait(wait_timeout)
yield s
logger.info("自動化測試結束!")
ios_screen_shot()
s.close()
logger.info("關閉session")

def ios_screen_shot():
"""
截圖操作
pic_name:截圖名稱
:return:
"""

fail_time = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))
os.chdir(img_dir)
a = os.system("idevicescreenshot")
if a == 0:
logger.info('截圖成功')
else:
logger.info(fail_time, "截圖失敗")

@pytest.fixture()
def imyaml():
file = open(yaml_dir, "r")
f = yaml.safe_load(file)
logger.info("匯入yaml資料成功")
return f

logs存放生成的日誌

tools目錄下loggers.py自定義日誌

htmlreport存放html測試報告

reports 為allure生成的json檔案存放位置

data目錄下data.yaml檔案配置了控制元件元素

testcases目錄下為測試用例

@allure.feature("測試XXX")
def test_post(ios_driver_setup, imyaml):
s = ios_driver_setup
# 定位元素
f = imyaml
keyvalues = []
for i in f.keys():
keyvalues.append(i)
name1 = f[keyvalues[1]][0]
name2 = f[keyvalues[1]][1]
name3 = f[keyvalues[1]][2]
xpath1 = f[keyvalues[0]][0]
xpath2 = f[keyvalues[0]][1]
xpath3 = f[keyvalues[0]][2]
xpath4 = f[keyvalues[0]][3]
# 開始
s(name=name1).tap()
s.implicitly_wait(30)
s(name=name2).tap()
s.swipe(0.741, 0.792, 0.741, 0.48)
s.implicitly_wait(30.0)
# 選擇時間
s(xpath=xpath1).tap()
s(xpath=xpath2).tap()
#選擇XX
s(xpath=xpath3).click_exists(timeout=5.0)
# 提交
s(xpath=xpath4).tap()
s.implicitly_wait(30.0)
try:
s(name='取消').tap()
assert s(name=name3).exists
logger.info("成功!")
"""if s(name=name3).exists is True:
logger.info("
成功")
else:
logger.info("
失敗...")"""
except Exception as e:
logger.info("異常資訊:".format(e))
s.implicitly_wait(30.0)

driver.py進行了裝置初始化

import wda


class Driver():

def init_ios_driver(self):
"""
ios裝置初始化
:return:
"""

try:
c = wda.Client('http://localhost:8100')
c.healthcheck()
logger.info("連線裝置:{}".format(ios_device_udid))
c.wait_ready(timeout=wait_timeout)
return c
except Exception as e:
logger.info("初始化ios端driver異常!{}".format(e))

run.py 進行環境初始化,執行指定用例,生成測試報告

import pytest
from config import *


def init_env():
cmd = "iproxy 8100 8100"
os.popen(cmd)


def init_report():
cmd = "allure generate reports -o htmlreport --clean"
os.popen(cmd)
# cmd = "allure open htmlreport"


if __name__ == '__main__':
init_env()
pytest.main(["-s", case_dir, "--setup-show", "--alluredir=reports", "--emoji"])
init_report()

相關文章