1. 程式人生 >實用技巧 >其它 >命令列執行airtest的python指令碼

命令列執行airtest的python指令碼


一些廢話

因為自家廠裡目前UI測試工具用的是win32gui總是會出現找不到元素的情況,聽說airtest的影象識別成功率還是不錯的,所以嘗試下airtest

環境搭建和使用IDE

https://www.cnblogs.com/WCQ-berly/p/11981466.html

不用IDE執行指令碼

通過python -m airtest help可以檢視命令
命令列的方式是: python -m airtest run 指令碼路徑 --device 引數
windows裝置引數要有視窗控制代碼,用這種命令就要在執行前拿到控制代碼,感覺有點麻煩,要是可以在指令碼中再設定裝置引數就比較好。

指令碼中設定裝置引數

auto_setup(basedir=file, devices=[‘Windows:///{}’.format(handle)])
通過devices引數設定裝置連線引數,這樣直接像普通的python指令碼一樣用 python 指令碼路徑 去執行

demo

用網易雲音樂來試一把

import time
import win32gui
import pyautogui
from airtest.core.api import *

def getHandle(class_name):
    return win32gui.FindWindowEx(0, 0, class_name, None)

def sendKey(key_name):
    pyautogui.press(key_name)

def test():
    handle = getHandle("OrpheusBrowserHost")
    auto_setup(basedir=__file__, devices=['Windows:///{}'.format(handle)])
    touch(Template(r"tpl1634895083685.png", record_pos=(-0.213, -0.27), resolution=(1126, 837)))
    wait(Template(r"tpl1634895098533.png", record_pos=(-0.207, -0.267), resolution=(1126, 837)))
    touch(Template(r"tpl1634895162818.png", record_pos=(-0.082, -0.339), resolution=(1126, 837)))
    text("五月天")
    sendKey('Enter')

if __name__ == "__main__":
    time.sleep(3)
    test()

結合pytest和airtest

import time
import win32gui
import pyautogui
import pytest
from airtest.core.api import *

def getHandle(class_name):
    return win32gui.FindWindowEx(0, 0, class_name, None)

def sendKey(key_name):
    pyautogui.press(key_name)

class TestAir(object):
    def setup(self):
        handle = getHandle("OrpheusBrowserHost")
        auto_setup(basedir=__file__, devices=['Windows:///{}'.format(handle)])
    
    def test_custom(self):
        touch(Template(r"tpl1634895083685.png", record_pos=(-0.213, -0.27), resolution=(1126, 837)))
        assert wait(Template(r"tpl1634895098533.png", record_pos=(-0.207, -0.267), resolution=(1126, 837)))
    
    def test_search(self):
        touch(Template(r"tpl1634895162818.png", record_pos=(-0.082, -0.339), resolution=(1126, 837)))
        text("五月天")
        sendKey('Enter')

if __name__ == "__main__":
    time.sleep(3)
    pytest.main(['-s', __file__])

最後

截圖還是要靠IDE來,如果不用截圖的化就要給出元素的座標位置,顯然更麻煩,不知道有沒有其他方法。