Airtest的Api介绍和应用(全)

Airtest的Api介绍和应用(全)

#!/usr/bin/env python 
# encoding: utf-8 
"""
@author: 九九的金金子
@file: test_airtest.py
@time: 2021/3/4 17:19
"""
# 初始化环境
from airtest.core.api import *
from poco.drivers.android.uiautomation import AndroidUiautomationPoco
import pytest


class TestAirtest:
    """
    airtest.core.api介绍
    """

    def setup(self):
        auto_setup(__file__)  # 自动安装运行env,并尝试连接android设备,如果没有连接设备。
        poco = AndroidUiautomationPoco(use_airtest_input=True, screenshot_each_action=False)

    def test_init_device(self):
        # 如果还没有初始化设备,并设置为当前设备。
        init_device(platform="Android", uuid="12344321", cap_method="JAVACAP")

    def test_connect_device(self):
        # 用uri初始化设备,并设置为当前设备。
        connect_device("Android:///12344321?cap_method=javacap&touch_method=adb")

    # 返回当前活动的设备。
    def test_device(self):
        dev = device()
        dev.touch((100, 100))

    # 设置“当前活动设备”。
    def test_set_current(self):
        set_current(0)  # 切换到当前连接的第一部手机

    # 在目标设备中启动remote shell并执行命令
    def test_shell(self):
        print(shell("ls"))

    # 在设备上启动目标应用程序
    def test_start_app(self):
        start_app("com.xxx.xxxx") #包名

    # 停止设备上的目标应用程序
    def test_stop_app(self):
        stop_app("com.xxx.xxxx")

    # 清除设备上目标应用的数据
    def test_clear_app(self):
        clear_app("com.xxx.xxxx")

    # 在设备上安装应用程序
    def test_install_app(self):
        install(r"E:\\sit_debug_2022847.apk")

    # 卸载设备上的应用程序
    def test_uninstall_app(self):
        uninstall("com.xxxt")

    # 获取目标设备的屏幕截图并将其保存到文件中。
    def test_snapshot(self):
        snapshot(filename="test.png", msg="test")

    # 唤醒并解锁目标设备
    def test_wake(self):
        wake()

    # 返回目标设备主界面。
    def test_home(self):
        home()

    # 在设备屏幕上执行触摸操作
    def test_touch(self):
        touch((100, 100))  # 单击绝对坐标
        touch((100, 100), times=2)  # 点击2次
        touch(Template(r"E:\测试\airtest\untitled.air\tpl1614843297836.png", record_pos=(-0.003, 0.309),
                       resolution=(1080, 1920)))  # 点击图片中心(模板对象)

    # 执行双击
    def test_double_click(self):
        double_click((100, 100))
        double_click(Template(r"E:\测试\airtest\untitled.air\tpl1614843297836.png"))

    def test_swipe(self):
        # 在设备界面上执行滑动操作。
        swipe((100, 100), (200, 200))
        swipe(Template(r"E:\测试\airtest\untitled.air\tpl1614844771023.png", record_pos=(0.473, -0.035),
                       resolution=(1080.0, 1920.0)),
              vector=[-0.933, -0.2038])

    def test_keyevent(self):
        # 在设备上执行设备控件事件
        keyevent("HOME") # hone键
        keyevent("BACK") # 返回键

    def test_test(self):
        # 在目标设备上输入文本。文本输入框是激活的状态。这个动作只做了输入的动作,没做点击输入框的动作。
        text("test")
        text("test", search=True)  # Android,有时候你需要在输入后点击搜索按钮

    def test_sleep(self):
        # 设置的等待时间。它将被记录在报告中
        sleep(2)

    def test_wait(self):
        # 在设备屏幕上等待与模板匹配
        def notfound():
            print("No target found")
        wait(Template(r"E:\测试\airtest\untitled.air\tpl1614843297836.png"), intervalfunc=notfound)

    def test_wait(self):
        # 检查设备屏幕上是否存在给定的目标
        # if exists(Template(r"E:\测试\airtest\untitled.air\tpl1614843297836.png")):
        #     touch(Template(r"E:\测试\airtest\untitled.air\tpl1614843297836.png"))

        pos = exists(Template(r"E:\测试\airtest\untitled.air\tpl1614843297836.png")) # 因为' exists() ' '将返回坐标,我们可以直接点击这个返回值来减少一次图像搜索
        if pos:
            touch(pos)

    def test_find_all(self):
        # 在设备屏幕上找到所有出现的目标并返回它们的坐标
        result = find_all(Template(r"E:\测试\airtest\untitled.air\tpl1614843297836.png"))
        print(result)

    def test_assert_exists(self):
        # 断言目标存在于设备屏幕上
        assert_exists(Template(r"E:\测试\airtest\untitled.air\tpl1614843297836.png"), "assert exists")

    def test_assert_not_exists(self):
        # 断言目标在设备屏幕上不存在
        assert_not_exists(Template(r"E:\测试\airtest\untitled.air\tpl1614843297836.png"), "assert not exists")

    def test_assert_equal(self):
        # 断言两个值相等
        assert_equal(1, 1, msg="assert 1==1")

    def test_assert_not_equal(self):
        # 断言两个值不相等
        assert_not_equal(1, 2, msg="assert 1!=2")
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Airtest和Appium是两个常用的移动端自动化测试框架,它们各自有一些优点和缺点。 Airtest的优点: 1. 跨平台支持:Airtest可以同时支持Android和iOS平台的自动化测试,开发者可以使用同一套代码来实现跨平台的自动化测试。 2. 易于上手:Airtest提供了简洁易懂的API和可视化操作界面,使得初学者可以快速上手和编写测试脚本。 3. 强大的图像识别功能:Airtest内置了强大的图像识别功能,可以用于处理一些无法通过控件定位的情况,提高了测试脚本的灵活性和稳定性。 Airtest的缺点: 1. 对于复杂的应用可能存在兼容性问题:由于Airtest使用了图像识别技术进行控件定位,对于一些复杂的应用界面可能无法准确识别,导致测试失败。 2. 缺乏社区支持:相比Appium,Airtest的社区用户相对较少,因此在遇到问题时可能难以找到及时有效的解决方案。 Appium的优点: 1. 广泛支持多种编程语言:Appium支持多种编程语言,如Java、Python、Ruby等,开发者可以根据自己的喜好和熟悉程度选择合适的语言进行自动化测试。 2. 大型社区支持:Appium拥有庞大的社区用户群体,开发者可以很容易地找到解决方案、文档和教程,减少学习和使用的难度。 3. 支持多种定位方式:Appium支持多种定位方式,包括基于控件属性、XPath、CSS选择器等,可以灵活地定位和操作应用界面上的控件。 Appium的缺点: 1. 对于跨平台支持相对较弱:Appium在跨平台支持上相对较弱,需要针对Android和iOS平台分别进行配置和使用。 2. 配置和环境搭建较为复杂:相比Airtest,Appium的配置和环境搭建较为复杂,需要安装和配置多个依赖组件,对于初学者来说可能会有一定的难度。 综上所述,Airtest适合对于简单应用的自动化测试,特别是对图像识别有较高需求的场景;而Appium适合对跨平台支持和社区支持有要求的场景,尤其是需要使用不同编程语言进行测试的情况。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值