命令行运行airtest的python脚本

这篇博客介绍了如何从零开始使用Airtest进行UI自动化测试,包括环境搭建、IDE使用,以及如何在不使用IDE的情况下执行测试脚本。文章还展示了如何在脚本中设置设备参数,以及结合pytest实现测试类的编写。最后提到了在测试过程中遇到的截图问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一些废话

因为自家厂里目前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来,如果不用截图的化就要给出元素的坐标位置,显然更麻烦,不知道有没有其他方法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值