基於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設備進行連接


捕捉參數錄製腳本

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

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


等待操作

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

POCO捕捉

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



    點擊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)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章