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

1,336 阅读3分钟

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

前言

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

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

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

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

image

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

keyevent("volumeDown") # 减少音量

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

image

其中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接口返回当前运行应用

image

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

>>>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)给设备上锁解锁的示例

image

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

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

>>>sleep(1.0)

>>>ios.unlock()

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

5.新增弹窗的相关接口

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

image

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

>>>sleep(1.0)

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

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

image

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

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

image

>>>print(ios.alert_buttons())

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

image

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

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

image

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

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

image

ios.alert_click(['设置'])

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

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

image

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

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

with ios.alert_watch_and_click():
    sleep(5)

image

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

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

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

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

image

>>>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页

image

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

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

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

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

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

官方答疑 Q 群:654700783

呀,这么认真都看到这里啦,帮忙在文章左侧点一下点赞和收藏,给我一个支持把,灰常感谢~