AirtestProject 1.1.8 版本 Airtest 新增的 iOS 接口究竟有多香?!今天告诉你

fishfish-yu · 2021年02月25日 · 最后由 哲豪 回复于 2021年03月08日 · 3314 次阅读

此文章来源于项目官方公众号:“AirtestProject”
版权声明:允许转载,但转载必须保留原链接;请勿用作商业或者非法用途

前言

年前我们更新了 1.1.8 版本的 Airtest,这个版本主要是针对我们的 iOS 做了一些新的支持和功能更新。

其中,我们新增了不少接口用于处理各种 iOS 事件,今天我们就一起来看看这些 新增 iOS 接口详细的示例 把!

PS:如需使用下文中 iOS 的新接口,需要把 IDE 升级为 1.2.7 版本或者 1.2.8 版本,使用本地 python 环境的同学需要把 airtest 版本升级为 1.1.8 版本。

1.keyevent 接口增加音量控制事件

for i in range(3):
    keyevent("volumeUp") # 增加音量
    sleep(1.0)

sleep(1.0)

keyevent("volumeDown") # 减少音量

2.新增 app_state 接口返回包体状态

其中 value 对应的含义为:

  • 1: not running
  • 2: running in background
  • 3:running in foreground
  • 4: running
>>>start_app("com.apple.mobilesafari")
>>>sleep(2.0)

>>>print("---------------------------")
>>>print("此时的包体状态为:"+str(ios.app_state("com.apple.mobilesafari")["value"]))

>>>home()
>>>sleep(2.0)

>>>print("---------------------------")
>>>print("此时的包体状态为:"+str(ios.app_state("com.apple.mobilesafari")["value"]))

>>>stop_app("com.apple.mobilesafari")
>>>sleep(2.0)

>>>print("---------------------------")
>>>print("此时的包体状态为:"+str(ios.app_state("com.apple.mobilesafari")["value"]))

---------------------------
此时的包体状态为:4
---------------------------
此时的包体状态为:3
---------------------------
此时的包体状态为:1

3.新增 app_current 接口返回当前运行应用

可用于查看或者获取当前打开应用的包名和其它详细信息。

>>>start_app("com.apple.mobilesafari")
>>>print("---------------------------")
>>>print(ios.app_current())

>>>keyevent("HOME")
>>>sleep(1.0)

>>>print("---------------------------")
>>>print(ios.app_current())

---------------------------
AttrDict({'processArguments': {'env': {}, 'args': []}, 'name': 'AX error -25205', 'pid': 226, 'bundleId': 'com.apple.mobilesafari'})
---------------------------
AttrDict({'processArguments': {'env': {}, 'args': []}, 'name': 'AX error -25205', 'pid': 58, 'bundleId': 'com.apple.springboard'})

4.新增加锁的相关接口

1)判断设备当前是否上锁:is_locked
from airtest.core.ios.ios import IOS, wda
ios = IOS("http://localhost:8100/")

ios.is_locked()
2)解锁设备:unlock
from airtest.core.ios.ios import IOS, wda
ios = IOS("http://localhost:8100/")

ios.unlock()
3)给设备上锁:lock
from airtest.core.ios.ios import IOS, wda
ios = IOS("http://localhost:8100/")

ios.lock()
4)给设备上锁解锁的示例

>>>print("当前设备是否上锁:"+str(ios.is_locked()))

>>>ios.lock()
>>>print("当前设备是否上锁:"+str(ios.is_locked()))

>>>sleep(1.0)

>>>ios.unlock()

当前设备是否上锁:False
当前设备是否上锁:True

5.新增弹窗的相关接口

1)判断弹窗是否存在:alert_exists

>>>start_app("com.apple.mobilesafari")

>>>sleep(1.0)

>>>print("是否出现弹窗:"+str(ios.alert_exists()))

是否出现弹窗:True
2)返回弹窗上面的描述文字:alert.text

>>>print(ios.driver.alert.text)

关闭飞行模式或者使用无线局域网来访问数据
3)以列表形式返回弹窗的按钮文字:alert_buttons

>>>print(ios.alert_buttons())

('设置', '好')
4)点击弹窗左边的按钮:alert_dismiss

主要适用于拥有 2 个按钮的 iOS 弹窗。

ios.alert_dismiss()
5)点击弹窗右边的按钮:alert_accept

同样主要适用于拥有 2 个按钮的 iOS 弹窗。

ios.alert_accept()
6)点击弹窗上的指定按钮:alert_click

ios.alert_click(['设置'])

按传入的列表顺序查找指定按钮并进行点击操作,可用于处理弹窗的类似选项,比如指定点击弹窗与同意相关的选项:

ios.alert_click(['好','同意','允许'])
7)监控弹窗出现并且点击指定按钮:alert_watch_and_click

图示案例的监控时长为 5s。并且如未在接口内传入指定的按钮,则默认情况下监控此类弹窗: ["使用App时允许", "好", "稍后", "稍后提醒", "确定", "允许", "以后"]

start_app("com.apple.mobilesafari")
sleep(1.0)

with ios.alert_watch_and_click():
    sleep(5)

如传入指定按钮,则按传入的按钮需要来进行监控。

start_app("com.apple.mobilesafari")
sleep(1.0)

with ios.alert_watch_and_click(["设置"]):
    sleep(5)

6.新增 device_info 接口返回设备信息

>>>print("----------------------")
>>>print(ios.device_info())

----------------------
AttrDict({'timeZone': 'GMT+0800', 'currentLocale': 'zh_CN', 'model': 'iPhone', 'uuid': '8816D79F-1F6A-47B5-xxxx-xxxxxxxxx', 'userInterfaceIdiom': 0, 'userInterfaceStyle': 'light', 'name': 'iPhone', 'isSimulator': False})

7.新增 home_interface 接口返回是否是 home 页

>>>print("此时是否是HOME页:"+str(ios.home_interface()))

>>>keyevent("HOME")
>>>sleep(1.0)

>>>print("此时是否是HOME页:"+str(ios.home_interface()))

此时是否是HOME页:False
此时是否是HOME页:True

Airtest 官网http://airtest.netease.com/
Airtest 教程官网https://airtest.doc.io.netease.com/
搭建企业私有云服务https://airlab.163.com/b2b

官方答疑 Q 群:654700783

呀~这么认真都看到这里啦,帮忙点击左下角的爱心,给我点个赞支持一下把,灰常感谢~

共收到 6 条回复 时间 点赞

锁屏下可以启动 wda 后,再解锁吗😀

哲豪 回复

记得不能,都要先启动,然后再来玩锁屏解锁的接口

fishfish-yu 回复

嗯,目前在研究 tidevice 是怎么启动 wda 的,我试过锁屏下也可以启动,再调用 Home 解锁

不就是对 wda 封装了一层吗,有啥大惊小怪😂

哲豪 回复

因为没有被测应用要启动,所以启动 WDA 的时候不需要解锁

codeskyblue 回复

是想做 ios 一键维护,从 重启到上线,之前 wda 是没办法在锁屏下启动,不能调用接口解锁,需要人工到机房维护,很麻烦。

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册