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")
版权声明:本文为daxiangaifashi原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。