IOS 的 Airtest 使用方法和示例
最编程
2024-05-07 20:57:33
...
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.****.net/AirtestProject/article/details/108103656 ...................ios自动化测试实操案例详解(airtest支持ios的接口)
https://developer.aliyun.com/article/920825....................................................................1.1.8版本Airtest新增的iOS接口(源码显示)
https://blog.****.net/weixin_42550871/article/details/110120241 ...................方法说明
https://mp.weixin.qq.com/s/wfXATdx_U5gpwIQcSQHe2g ................清除iOS后台应用
上一篇: 文明 6 建立(虚拟)局域网连接 踩坑
推荐阅读
-
IOS 的 Airtest 使用方法和示例
-
DEAE Sephadex A-25 的使用方法和注意事项
-
[C 语言深度分析] 您真的了解 C 语言中的位运算符吗? 位和、位或、位异或)(代码示例 + 详细图解
-
iOS 16 的新错误:无法在 Final Cut Pro 和 iMovie 中打开电影效果视频
-
pandas ExcelWriter 的用法和代码示例
-
iOS 反向 (IV)-APP Smashing Shell 和类转储工具的使用
-
应用程序首次启动时的引导和启动界面设置 - iOS 开发
-
解决 H5 在 IOS WebView 下上拉和下拉会驱动整个 WebView 出现空白的问题
-
在不同的安卓和 iOS 设备上对移动 H5 页面进行兼容调整
-
35 岁实现财务*,腾讯程序员手握2300万提前退休?-1000万房产、1000万腾讯股票、加上300万的现金,一共2300万的财产。有网友算了一笔账,假设1000万的房产用于自住,剩下1300万资产按照平均税后20-50万不等进行计算,大约花上26-60年左右的时间才能赚到这笔钱。也就是说,普通人可能奋斗一辈子,才能赚到这笔钱。在很多人还在为中年危机而惶惶不可终日的时候,有的人的35岁,就已经安全着陆,试问哪个打工人不羡慕?但问题是有这样财富积累必然有像样的实力做靠山。没有人可以不劳而获。 看到这里,肯定有人说,那么对于普通人来说,卷可能真就成了唯一的出路。但是卷也有轻松的卷,“偷懒”的卷法,对于程序员而言,刨除掉一时无法改掉的开会传统占用的大部分时间,如何把有限的时间和精力放在真正重要的架构设计、需求设计上,而不是重复的造*,编码、改bug、手动测试。因此在科技改变生活的今天,学会使用AI工具成为程序员们的必备技能。 以全栈式全自动的软件开发工具飞算SoFlu软件机器人为例,作为全球首款面向微服务架构设计和最佳实践的软件机器人,SoFlu软件机器人改变了原来手工编码的作业模式,通过可视化拖拽方式以及参数配置就能实现等同于编写复杂代码的业务逻辑,在设计业务逻辑时就完成了微服务应用开发,做到“业务即图,图即代码”,实现“软件开发,十倍提效”。 而作为飞算SoFlu软件机器人的一个重要组成部分,FuncGPT(慧函数)支持所有类型函数创建。通过自然语言描述Java函数需求,实时生成高质量、高可读性的Java函数代码。生成代码可直接复制到IDEA,或一键导入Java全自动开发工具函数库。 FuncGPT(慧函数)具有五大核心能力: ● 自然语言:通过自然语言即可生成函数,降低软件开发门槛。 ● 秒级函数开发:全自动秒级开发函数,秒级完成,效率千倍提升。 ● 一站式函数开发应用:函数生成即可直接应用,效率、安全有保障。 ● 质量可靠:生成的函数代码符合业界规范、具备良好的可读性,符合最优解。 ● 灵活生成:可以根据开发人员的具体需求生成并支持调整和修改。 以“将文件流转换为File对象”这个需求为例,以下是FuncGPT(慧函数)秒级生成的代码: / 类名:FileConverter // 函数名:convertStreamToFile // 函数功能:将文件流转换为File对象 // POM依赖包:无 import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; public class FileConverter { /** * 将文件流转换为File对象 * @param inputStream 文件流 * @param filePath 文件路径 * @param fileName 文件名 * @return 转换后的File对象 * @throws IOException 如果发生I/O异常 */ public static File convertStreamToFile(InputStream inputStream, String filePath, String fileName) throws IOException { File file = new File(filePath + File.separator + fileName); // 创建File对象 try (FileOutputStream outputStream = new FileOutputStream(file)) { // 创建文件输出流 byte buffer = new byte[1024]; int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { // 从文件流读取数据并写入文件 outputStream.write(buffer, 0, bytesRead); } } return file; // 返回转换后的File对象 } } // 函数示例 // 将文件流转换为File对象示例 // 入参:inputStream,文件流 // 入参:filePath,文件路径 // 入参:fileName,文件名 // 出参:file,转换后的File对象 // 调用示例: // InputStream inputStream = new FileInputStream("example.txt"); // String filePath = "C:\\Users\\User\\Documents"; // String fileName = "example.txt"; // File file = FileConverter.convertStreamToFile(inputStream, filePath, fileName); // System.out.println(file.getAbsolutePath); // 输出结果:例如,将文件流转换为File对象后,文件的绝对路径为:C:\Users\User\Documents\example.txt // 则输出结果为:C:\Users\User\Documents\example.txt 通过分析,不难发现以上代码: