基於AirTest+Python的ios自動化測試demo(微信朋友圈無限點贊)


AirTest相比Appuim有個好處就是可以對GUI圖片進行捕捉和最新版本支持WebView(目前Appuim不支持iOS12的WebView進行Xpath抓取) AirTest環境搭建可參考以下鏈接: https://airtest.netease.com/docs/docs_AirtestIDE-zh_CN/1_quick_start.html

環境配置: libimobiledevice: stable 1.2.0 (bottled), HEAD Python 3.7 WebDriverAgent 用AirTest提供的[https://github.com/AirtestProject/IOS-Tagent ] Xcode10.1 AirtestIDE

啟動AirTest

  1. 運行WebDriverAgent【運行之前先用Xcode打開WebDriverAgent.xcodeproj,選擇開發者賬號來Build正常】 可以采用終端方式來運行:
xcodebuild build-for-testing test-without-building -project [WebDriverAgent.xcodeproj目錄地址] -scheme WebDriverAgentRunner -destination id=[設備UDID] test
  1. 事先用$ brew install libimobiledevice 安裝 libimobiledevice 每次在WDA啟動后進行端口映射
$ iproxy 8100 8100

頁面上輸入http://127.0.0.1:8100/status 有Json格式返回,就代表連接成功

  1. 然后打開AirtestIDE對iOS設備進行連接

iOS連接.png

捕捉參數錄制腳本

Airtest提供了兩個插件庫讓我們方便進行自動化測試: 1:AirTest庫,主要通過對GUI圖片進行定位來操作實現自動化 2:POCO庫,類似appuim,通過形成UI樹Xpath,對多層次控件進行操作來實現自動化

因為我們自動化測試主要是針對多型號設備進行測試,每台設備的UI可能因為適配狀況,會有不一樣,會導致AirTest的成功率不穩定,所以,理論上優先使用POCO進行元素定位,其次才是AirTest進行定位

AirTest捕捉

  1. AirTest初始化:
from airtest.core.api import *
  1. 手動捕捉 在AirtestIDE的Airtest錄制輔助窗內,包含有三種類型的錄制按鈕:

操作類型

輔助類型

斷言類型

touch

text

assert_exists

swipe

keyevent

assert_not_exists

wait

sleep

assert_equal

exist

 

assert_not_equal

snapshot

 

AirTestIDE.png

點擊 touch/swipe/wait/exists/assert_exists/assert_not_exists 按鈕后,在設備畫面上按下鼠標左鍵進行截圖框選,抬起鼠標左鍵完成框選。對應操作語句會自動插入編輯器腳本中。 點擊操作

點擊.png

等待操作

assert_exists(圖片, "等待成功登陸出現")
  1. 自動捕捉 點擊AirTest輔助窗右上角的錄制按鈕,然后在設備視窗進行操作,就能自動錄制操作腳本,但因為這種方式錯誤性比較高,建議少用,主要是通過手動捕捉對應的圖片再進行之后的操作

POCO捕捉

  1. POCO初始化
from poco.drivers.ios import iosPoco poco = iosPoco()
  1. 手動捕捉

POCO.png 點擊POCO輔助窗右上方第一個按鈕【POCO Pause】,對窗口進行凍結,然后在Log顯示元素屬性,並且通過點擊右鍵【UI-Path Code】獲取對應的UI-path,然后再進行操作,如:

poco("iconUser3").wait(3).click() poco("iconLogin").click() transBtn = poco("iconLogin") poco.wait_for_all([transBtn]) transBtn.click() #向上滑動一個屏幕的高度 screenWidth,screenHeigth = poco.get_screen_size() swipe((screenWidth*0.5,screenHeigth*0.9),vector=[0,-0.5])

POCO輔助窗右上方第二個按鈕【POCO Inspector】是在不凍結窗口的情況下進行捕捉

  1. 自動捕捉 POCO輔助窗右上方第三個按鈕【POCO Auto Recording】是進行自動捕捉錄制,任何操作都錄制成腳本

微信朋友圈無限點贊

from airtest.core.api import * from poco.drivers.ios import iosPoco poco = iosPoco() auto_setup(__file__) assert_exists(Template(file:///Users/cengsijian/Desktop/AirTest/AirTestWeixinTest.air/tpl1545103410488.png, record_pos=(0.119, -0.708), resolution=(750, 1334)), "進入微信測試") poco("微信").click() assert_exists(Template(file:///Users/cengsijian/Desktop/AirTest/AirTestWeixinTest.air/tpl1545103527211.png, record_pos=(0.125, 0.815), resolution=(750, 1334)), "請填寫測試點") poco("發現").click() poco("朋友圈").click() screenWidth,screenHeigth = poco.get_screen_size() while True: #查找評論按鈕 tableList = poco("Table").child('Cell').offspring('評論') #點擊評論按鈕 for child in tableList: childX,childY = child.get_position() print(childX) print(childY) if (childY>=0.1 and childY<1.0): child.click() if poco("贊").exists(): touch(Template(file:///Users/cengsijian/Desktop/AirTest/AirTestWeixinTest.air/tpl1545118102228.png, record_pos=(0.057, 0.385), resolution=(750, 1334))) # poco("贊").click() #向上滑動一個屏幕的高度 swipe((screenWidth*0.5,screenHeigth*0.9),vector=[0,-0.8],duration=2.5) #等滾動動畫結束 sleep(5)

代碼.png

演示.gif


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM