移動端自動化openatx開源項目介紹,pytest並發測試框架結合


開頭

相信不少用過appium的同學,對於使用appium的一些體會與感受是否與我相似

1. appium啟動服務和app程序非常慢

2. appium多線程並發需要啟動多個服務

3. appium必須連接usb線進行自動化測試

所以在Testerhome這個共享氛圍很好,也有不少大牛的論壇上爬文章,發現了openatx這個開源項目,直接截圖github的README介紹一下項目情況

github地址:https://github.com/openatx

有興趣的同學可以去為開源項目添磚加瓦

總結一下atx:

1. 快速啟動和操作手機端

2. 基於atx-agent的http協議,通過接口調用手機上的二進制atx程序去進行手機操作,實現WiFi無線連接

3. codeskyblue還搭建了atx-server集群管理,目前在用Python重寫atxserver2,將支持跨網域連接設備和一些結構優化

4. 因無需啟動多appium服務,只是通過http接口發送命令,支持大量終端同步並發操作

 

ATX介紹

atx架構圖

安裝

適用范圍
Android手機 4.3+(sdk 18)

命令行CMD或Pycharm自己的venv環境中 

pip install -U --pre uiautomator2

然后等待安裝成功

初始化設備

手機連接上usb,運行初始化命令

python -m uiautomator2 init

如有atx-server設備集群管理,則可

python -m uiautomator2 init --server ip:port

ip和port為atx-server相應ip和端口

等待初始化成功即可。

基本操作

連接設備和操作

# coding: utf-8
import uiautomator2 as u2

u = u2.connect_usb()
或
u = u2.connect(ip)
driver = u.session("cn.vsx.vc")

driver(className="android.widget.Button", resourceId="cn.vsx.vc:id/ptt").long_click(duration=2, timeout=10)
assert driver(resourceId="cn.vsx.vc:id/ptt", text="按住 說話").exists

剩下的控制操作可參考官方文檔: https://github.com/openatx/uiautomator2

 

結合Pytest單元測試框架與並發

思路:

之前的文章介紹過pytest單元測試框架如何進行並發,此處可以重復一下:

通過CMD命令啟動pytest的時候,代入並發所需要的參數即可:

pytest.main(["../TestCases/", f"--cmdopt={Phone['ip']}", "--alluredir"])

在測試用例目錄下的conftest加入cmdopt參數代入方法

def pytest_addoption(parser):  # 定義命令行傳參參數
    parser.addoption("--cmdopt", action="store", default="device", help="None")


@pytest.fixture(scope="session")  # 命令行參數傳遞給pytest
def cmdopt(request):
    return request.config.getoption("--cmdopt")


@pytest.fixture(scope="session")  # 初始化開始連接設備
def connectDevice(cmdopt):
    address = cmdopt
    d = u2.connect(addr=address)
    d.set_fastinput_ime(True)
    driver = d.session("cn.vsx.vc")
    yield driver
    print("driver finished")
    driver.close()

 

然后通過concurrent.future的多線程ProcessPoolExcutor 去並發啟動包含pytest.main的方法

def runnerPool(deviceIP_list):  # 啟動多進程運行測試
    with ProcessPoolExecutor(len(getDevices())) as pool:
        pool.map(runPytest, deviceIP_list)

自此,整體的設計思路完成。 pytest的框架功能以及插件非常豐富,可以自己查找相關資料使用

如果有需要可仔細查看完整代碼

地址:https://github.com/Grandlulu/atx-pytest

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM