锐单电子商城 , 一站式电子元器件采购平台!
  • 电话:400-990-0325

python-自动化Airtest-2代码篇 Airtest包

时间:2023-11-18 19:07:02 三极管sje13003

python-自动化Airtest-2代码篇 Airtest包

代码篇 Airtest包

  • 一: connect_device 代码
    • 1> auto_setup
    • 2> Template
    • 3> other airtest.core.api
      • 1. init_device
      • 2. device&connect_device&set_current
        • A: device
        • B: connect_device(uri)
        • C: set_current
        • D: auto_setup
  • 二:Other commands
    • 1> shell(cmd)
    • 2> app operation
      • 1. start_app(package, activity=None)
      • 2. stop_app(package)
      • 3. clear_app(package)
      • 4. install(filepath, **kwargs)
      • 5. uninstall(package)
    • 3> snapshot(filename=None, msg='', quality=None, max_size=None)
    • 4> wake()
    • 5> home()
    • 6> find_all(v)
    • 7> 其他命令keyevent、text、wait、wait、assert_exists、assert_not_exists、assert_equal、assert_not_equal、sleep
  • 三: screen operation
    • 1> touch(v, times=1, **kwargs)
    • 2> click(v, times=1, **kwargs)
    • 3> double_click(v)
    • 4> swipe(v1, v2=None, vector=None, **kwargs)
    • 5> pinch(in_or_out='in', center=None, percent=0.5)
  • 四:airtest poco

一: connect_device 代码

更完备的 airtest.core.api
https://airtest.readthedocs.io/en/latest/all_module/airtest.core.api.html

1> auto_setup

auto_setup(__file__, logdir=True, devices=["platform://host:host/uuid?cap_method=value&&ori_method=value",]) 

platform: Android/iOS/Windows…
host: Android平台是adb host,iOS下是iproxy host,请留空其他平台
port: Android下是adb port,iOS下填写iproxy port,请留空其他平台
uuid: 目标设备的uuid,例如Android下面是序列号,windows下面是窗口句柄,iOS是uuid
param: 设备的初始配置字段,如cap_method/ori_method/…
value: 设备初始化字段的值。

2> Template

将图片转换为代码

Template(r"tpl1532588127987.png", record_pos=(0.779, 0.382), resolution=(407, 264), threshold=0.6, target_pos=5, rgb=False) 

参数分别为:截取的图片名, record_pos记录坐标, resolution 分辨率,threshold阈值,target_pos点击图像位置
截取的图片名称添加绝对路径和相对路径
record_pos:记录坐标
resolution :分辨率
threshold:浮点型
target_pos:图像点击位置,向上节IDE介绍1-9 位置;默认为5

3> other airtest.core.api

1. init_device

如果没有初始设备,则设置为当前设备

init_device(platform='Android', uuid=None, **kwargs) 

参数:
platform :平台– Android, IOS or Windows
uuid :设备号
kwargs :其他特定关键字参数可选平台

例如:
init_device(platform=“Android”,uuid=“SJE5T17B17”, cap_method=“JAVACAP”)

2. device&connect_device&set_current

A: device

a = device() print(a) ==================返回当前设备实例,即以下可调用实例  a.touch((256, 872))  ==================  

B: connect_device(uri)

链接设备,URI即链接设备参数;同样返回设备实例,用法相同

a = connect_device("android://127.0.0.1:5037/27QGL20828000064?cap_method=MINICAP&&ori_method=MINICAPORI&&touch_method=MINITOUCH") print(a) ===========================  ========================================== a.touch((256, 872)) 

C: set_current

set_current(idx)

set_current(0) set_current("27QGL208280000641") 

Parameters:idx:初始化设备实例Uuid或索引;即链接当前设备的第0个设备,或设备号为27QGL208280000641 设备

Raises: 如果传参正确,不报错,找不到设备,则报错

IndexError: device idx not found in: ['27QGL208280000641'] or [0] 

Returns:
Platforms: 适平台Android, iOS, Windows

D: auto_setup

如上已做讲解,需要注意是其中都有参数参数,可做传参选择性访问执行,例如:设备-27QGL2082800064年,传输操作更加灵活;

二:Other commands

a id="1_shellcmd_86">1> shell(cmd)

Parameters: cmd – command to be run on device, e.g. “ls /data/local/tmp”
Platforms: 适用平台 Android

window_size = shell("wm size")
print(window_size)
或
a = set_current(0)
a.shell("wm size")
或
a = connect_device("Android:///device1")
a.shell("wm size")
========================
Physical size: 720x1440

2> app operation

1. start_app(package, activity=None)

启动apk
package:启动的包的名称,而非activity 名
activity :启动的活动,默认为None,这意味着主活动
Platforms: 支持平台 Android, iOS

start_app('com.huawei.appmarket',activity=None)
start_app('com.huawei.appmarket')

2. stop_app(package)

参数同start_app

stop_app('com.huawei.appmarket')

3. clear_app(package)

Parameters: 包的名称,参见start app
Platforms: 适用平台 Android

4. install(filepath, **kwargs)

安装apk
filepath: 文件安装在目标设备上的路径
Platforms: 适用平台Android

install(r"D:\demo\test.apk")
adb install -r -t D:\demo\test.apk
install(r"D:\demo\test.apk", install_options=["-r", "-t"])

5. uninstall(package)

卸载设备上apk
Parameters: 包的名称,参见start app
Platforms: 适用平台Android

uninstall('com.huawei.appmarket')

3> snapshot(filename=None, msg=‘’, quality=None, max_size=None)

获取目标设备的截图并将其保存到文件中
Parameters: Filename -保存截图的文件名。 如果提供了相对路径,则默认位置为ST.LOG_DIR
msg: 对截图的简短描述,它将被记录在报告中
quality :图像质量,范围为[1,99]的整数,默认为10
Max_size:图片的最大大小,例如1200
Platforms: 适用平台Android, iOS, Windows

snapshot(filename=r'C:\Users\CHH_PC\Desktop\test\1.png', msg='ceshi', quality=70, max_size=None)

4> wake()

唤醒或解当前设备
Platforms: 适用平台Android

wake() #此处由于屏锁其他原因可能无效操作需要确认

5> home()

返回目标设备主界面。
Platforms: 适用平台 Android, iOS

 home()

6> find_all(v)

在设备屏幕上找到所有出现的目标,并返回它们的坐标
Parameters: v-目标图标
Returns:
Platforms: 适用平台 Android, Windows, iOS

在这里插入图片描述

{'result': (93, 1047), 'rectangle': ((41, 1018), (41, 1076), (145, 1076), (145, 1018)), 'confidence': 1.0}, {'result': (397, 1047), 'rectangle': ((345, 1018), (345, 1076), (449, 1076), (449, 1018)), 'confidence': 1.0}, {'result': (549, 1047), 'rectangle': ((497, 1018), (497, 1076), (601, 1076), (601, 1018)), 'confidence': 0.9994440674781799}, {'result': (245, 1047), 'rectangle': ((193, 1018), (193, 1076), (297, 1076), (297, 1018)), 'confidence': 0.9882984161376953}]
============================
print(len(re))
==================找到4个结果,并以格式返回
4

7> 其他命令keyevent、text、wait、wait、assert_exists、assert_not_exists、assert_equal、assert_not_equal、sleep

此处和上节ide 界面操作介绍相对应
python-自动化Airtest-1Airtest IDE篇
https://blog.csdn.net/weixin_42914706/article/details/125084340
主要是图片操作对代码间的转换,本质都是一样的命令,这里不做重复讲解

三: screen operation

1> touch(v, times=1, **kwargs)

v – 可为坐标或者图片
Times -要执行多少次触摸
Platforms: 适用平台 Android, Windows, iOS
Returns:返回坐标最终点击
android 平台

touch((256, 872)) # 点击1次坐标
touch(Template(r"tpl1655545420944.png", record_pos=(-0.119, 0.196), resolution=(720, 1440))) # 图标转为代码点击一次
touch(Template(r"tpl1655545498543.png", record_pos=(0.222, 0.942), resolution=(720, 1440)),duration=3)# 3秒内点击,实际效果是点住图标三秒释放
touch(Template(r"tpl1655545498543.png", record_pos=(0.222, 0.942), resolution=(720, 1440)),times=2,duration=2)# 2秒内点击图标2次

Windows 平台

touch((100, 100), right_click=True) #右击

2> click(v, times=1, **kwargs)

和touch 用法一致

3> double_click(v)

双击
v : 要触摸的目标,可以是模板实例,也可以是绝对坐标(x, y)
Returns: 最终返回坐标并点击

double_click((100, 100))
a = touch(Template(r"tpl1655546545772.png", record_pos=(0.133, 0.025), resolution=(720, 1440)))
print(a)
====================
(456, 738)

4> swipe(v1, v2=None, vector=None, **kwargs)

手机左上角坐标为(0,0),右下角坐标为(1,1)
v1 to v2: 可为(x, y),(x, y) 坐标,可为矢量:滑动动作的矢量坐标,也可以是屏幕百分比,例如(0.5,0.5)
Raises: 当没有提供足够的参数来执行交换操作时,一般的异常
Returns: 原点位置和目标位置
Platforms: 适用平台 Android, Windows, iOS

swipe((100, 100), (200, 200), duration=1, steps=6)
swipe(Template(r"tpl1655546048479.png", record_pos=(0.125, 0.197), resolution=(720, 1440)), vector=[-0.49, 0.0151])
其为“- ”表示滑动逆向原坐标

5> pinch(in_or_out=‘in’, center=None, percent=0.5)

设备屏幕上捏动作
Parameters:in_or_out, [“in”, “out”]
center : 缩放动作的中心,默认为None,即屏幕的中心
percent: 屏幕上缩放动作的百分比,默认为0.5
Returns: None
Platforms: 适用平台Android

用两根手指在屏幕中央捏一下
pinch()
以(100,100)为中心,用两指滑出
pinch('out', center=(100, 100))# 图片的缩放可以使用此命令

四:airtest poco

python-自动化Airtest-3IDE poco介绍
https://blog.csdn.net/weixin_42914706/article/details/125350806

锐单商城拥有海量元器件数据手册IC替代型号,打造电子元器件IC百科大全!

相关文章