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')
3.相關連接:
https://blog.csdn.net/AirtestProject/article/details/108103656 ...................ios自動化測試實操案例詳解
https://blog.csdn.net/weixin_42550871/article/details/110120241 ...................方法說明
https://mp.weixin.qq.com/s/wfXATdx_U5gpwIQcSQHe2g ................清除iOS后台應用