命令行运行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来,如果不用截图的化就要给出元素的坐标位置,显然更麻烦,不知道有没有其他方法。


版权声明:本文为QuanWaiRen0原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。