Airtest在IOS端的用法及案例
1.概述:
本篇主要记录Airtest对ios操作常用小案例,及语法解释
2.开始案例:
2.1打开设置点击个人中心,并自定义截图
# -*- encoding=utf8 -*- __author__ = "root1" from airtest.core.api import * from airtest.aircv import * from poco.drivers.ios import iosPoco auto_setup(__file__) connect_device("ios:///127.0.0.1:8100") poco = iosPoco() start_app("com.apple.Preferences")#启动应用(设置) sleep(3) snapshot(filename='/Users/root1/Desktop/test_all.png',msg='这里是massage')#普通全局截图 poco("廖松").click()#点击个人中心 #____________指定位置截图____________________ screen = G.DEVICE.snapshot() local_screen = aircv.crop_image(screen,(300,900,950,1500))#指定区域 pil_img = utils.cv2_2_pil(local_screen)#实例化对象 pil_img.save("/Users/root1/Desktop/test123.png", quality=99, optimize=True)#保存图片位置
2.2清除后台应用并启动QQ
场景:每次打开iOS应用的界面都是上次退出时的界面,应用初始页面不一致,导致脚本运行出现了各种问题
备注:设置辅助触控(小圆点)时,只留一个“应用切换器”
# -*- encoding=utf8 -*- __author__ = "root1" from airtest.core.api import * from airtest.aircv import * from poco.drivers.ios import iosPoco auto_setup(__file__) connect_device("ios:///127.0.0.1:8100") poco = iosPoco()
#先判断是否有辅助触控(小圆点) if exists(Template(r"tpl1611048639356.png", record_pos=(0.423, 0.245), resolution=(1242, 2208))): log('准备清除后台程序') touch(Template(r"tpl1611048639356.png", record_pos=(0.423, 0.245), resolution=(1242, 2208)))#点击小圆点 sleep(1.5) for i in range(5):
#可以打开固定竖屏,就不用横向kill了 swipe((600,1500),(600,800))#向上滑动杀掉应用 swipe((600,1500),(600,800))#向上滑动杀掉应用 if not poco(name='无 SIM 卡').exists():#在唤醒多任务时,不会显示顶部sim卡 swipe((600,1500),(600,800)) swipe((600,1500),(600,800)) else: print('当前没有后台程序') break keyevent("HOME")#点击home键 start_app("com.tencent.mqq")#启动qq sleep(5) if poco(name='忘记密码').exists(): print('断言成功')
2.3通过tidevice自动处理WebDriverAgent
#!/usr/bin/env python # encoding: utf-8 __author__ = "晨晨" import os,time import requests,json #_______________________________________通过tidevice操作对应设备(用于自动化前检查设备状态)____________________________________________________________________________ def reboot(device_ip,device_id,package_name='com.game.test.dalan.xctrunner',timeout=15):#默认为iphone7p '''启动设备''' device_ip= 'http://'+device_ip+':8100/status' print('进程{}-启动tidevice代理程序{}'.format(os.getpid(),device_id)) start=os.popen(('tidevice -u '+device_id+' xctest -B '+package_name)) time.sleep(3) try: r = requests.get(url = device_ip)#获取状态 result=(r.text) result=json.loads(result) print(result['value']['state']) return result['value']['state'] except: print('connection failed') return 'connection failed'
2.4设备相关操作
#!/usr/bin/env python # encoding: utf- from airtest.core.api import * from airtest.aircv import * from poco.drivers.ios import iosPoco from airtest.core.ios.ios import IOS, wda #自动设置运行环境(初始化) auto_setup(__file__) #连接设备 ios_device = connect_device("ios:///192.168.40.206:8100") #初始化poco poco = iosPoco() #______________________________________屏幕状态___________________________________________________ device_status=ios_device.is_locked() print('设备是否锁屏:{}'.format(device_status)) #判断设备状态_并处理 if device_status == True: print('给设备解锁:{}'.format(ios_device.unlock())) elif device_status == False: print('给设备上锁:{}'.format(ios_device.lock())) #______________________________如果锁屏则解锁,否则就唤醒___________________________________________ print('执行设备解锁:',ios_device.unlock() if ios_device.is_locked() else '{}:设备已在解锁状态_尝试唤醒'.format(device_id,keyevent('home')))
#______________________________________弹框相关(系统弹框)___________________________________________ #判断弹窗是否存在:alert_exists print("是否出现弹窗:"+str(ios_device.alert_exists())) print('获取弹框的描述文字:{}'.format(ios_device.driver.alert.text)) #_______________________________home键______________________________________________________________ print("此时是否是HOME页:"+str(ios_device.home_interface())) keyevent("HOME")#点击home键
2.5单独点击home
##不会导入airtest日志
from airtest.core.api import * ios_device=connect_device("ios:///"+ip+":8100") keyevent('home')
2.6IOS设备IP地址改为静态
场景:ios自动化测试有时候设备ip会变,ios下没看见命令获取设备ip,这种情况可以设置设备静态ip了
备注:DNS可以是8.8.8.8 也可在局域网pc的ipconfig/all获取
3.相关连接:
https://blog.csdn.net/AirtestProject/article/details/108103656 ...................ios自动化测试实操案例详解(airtest支持ios的接口)
https://developer.aliyun.com/article/920825....................................................................1.1.8版本Airtest新增的iOS接口(源码显示)
https://blog.csdn.net/weixin_42550871/article/details/110120241 ...................方法说明
https://mp.weixin.qq.com/s/wfXATdx_U5gpwIQcSQHe2g ................清除iOS后台应用
posted on 2021-01-12 11:08 chen_2987 阅读(1693) 评论(0) 编辑 收藏 举报