赞
踩
原创:George555 公众号: 测试江湖路
如果这些内容对你有帮助,也可以打开微信扫一扫,加关注:
注:本篇只针对代码层面的使用,关于AirtestIDE的使用请去官网查看。
一、环境要求:
操作系统
Windows
MacOS X
Linux
Python2.7 & Python3+
1.安装方法1:pip install -U airtest
2.安装方法2:从git下载:
git clone https://github.com/AirtestProject/Airtest.git
pip install -e airtest
备注:因为Airtest还在快速开发中,这里使用 -e 来安装源码。以后你就可以直接使用 git pull 更新代码目录来升级Airtest了。
三、连接设备
---Android设备连接----
connect_device("android:///") #连接本地android设备
connect_device("android://adbhost:adbport/1234566?cap_method=javacap&
touch_method=adb") #连接远端android设备
示例:
#A.连接本地夜神模拟器:
connect_device('Android://127.0.0.1:5037/127.0.0.1:62001')
注意:夜神模拟器端口是62001,127.0.0.1:5037是对应的本机的adbhost和adbport
#B.连接本机usb连接的真机: (连接华为真机)
connect_device("android:///Y2J5T17815005122")
----windows应用连接----
connect_device("Windows:///") #连接windows桌面
connect_device("Windows:///?title_re=百度.*") #连接windows应用
示例:
根据title正则匹配窗口:connect_device("Windows:///?title_re=百度一下.*")
根据窗口句柄匹配窗口:connect_device('Windows:///3410242')
----IOS设备连接----
connect_device("ios:///") #连接本机ios设备,即在mac上
connect_device("ios:///http://192.168.2.127:8100") #连接远端ios设备(比如在windows上调用脚本,操作mac上连接的ios设备)
注意:http://192.168.2.127:8100是在mac的xcode上运行WebdriverAgent(iOS-Tagent)成功后的服务端地址和端口
四、常用模拟输入
touch
swipe
text
keyevent
snapshot
wait
五、常用断言
assert_exists
assert_not_exists
assert_equal
assert_not_equal
备注:当断言失败,会抛出 AssertsionError。所有断言都会在html报告中显示。
六、获取脚本信息
# 如果代码中定义了包含: author, title, desc,则会在用例信息中输出author, title, desc。如:{"author": ..., "title": ..., "desc": ...}
使用命令:python -m airtest info "path to your .air dir"
七、导入其他air脚本
示例:
from airtest.core.api import using
using("common.air")
from common import common_function
common_function()
示例:
脚本目录结构:
airTestProject
com.air
com.py
test.air
test.py
com.py代码:
# -*- encoding=utf8 -*-
__author__ = "George"
from airtest.core.api import *
import os
def getScreenWidthHigh(SerialNo):
'''使用adb命令,获取移动设备屏幕的宽和高
@param:SerialNo:设备序列号
@return:high、width
调用示例:
width,high=getScreenWidthHigh("127.0.0.1:62001")
print (width)
print (high)
'''
os.system("adb connect "+SerialNo)
size=os.popen("adb shell wm size").
read().split(':',1)[1].strip()
#size是Physical size: 720x1280处理后的720x1280
#print (size)
high=size.split('x',1)[1] #用x分离获取high值
width=size.split('x',1)[0]#用x分离获取width值
os.system("adb disconnect "+SerialNo)
return width,high
test.py代码: (---调用com.py里的方法)
# -*- encoding=utf8 -*-
__author__ = "George"
from airtest.core.api import *
from airtest.core.api import using
using("com.air")
from com import getScreenWidthHigh
a,b=getScreenWidthHigh("127.0.0.1:62001")
print (a)
print (b)
#####欢迎大家加群交流####
QQ:464314378
微信群请先加群主微信,群主会拉各位小伙伴进群。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。