【質量平臺】淺嘗UI自動化之Airtest實踐

{"type":"doc","content":[{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"一、背景","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#494949","name":"user"}}],"text":"由於很多公司都採用敏捷開發的模式,測試也要跟着進行敏捷測試。而每個迭代的週期非常短,經常要對原有功能進行迴歸測試,這樣就增加了大量重複人力成本。引入UI自動化測試可以用來快速回歸測試app原有功能,測試人員只需要關注新功能的測試。其次,移動端App的測試用例大部分是功能驗證相關的用例,通過UI操作即可驗證,這就爲UI自動化提供了便利條件。因此,可以將測試重複度高,執行效率低的用例,通過UI自動化快速重複執行,達到提高測試效率的目的。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"二、Airtest簡介","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"目前主流的UI自動話測試工具主要有:Selenium, Appium和Airtest。其中Selenium是一款開源的Web應用自動化測試工具,它可以直接運行在多種瀏覽器平臺,其支持的瀏覽器幾乎涵蓋了所有主流的瀏覽器,但是由於得物是APP,不需要web端的測試,故這個工具被殘忍捨棄。而Appium和AirTest都是針對APP的自動化測試工具,都可以進行自動話測試腳本的錄製和回放。但是之所以選擇了AirTest最主要的原因是他很容易生成測試腳本,即使測試人員不會編程,不懂腳本,也可以通過正常用戶的點擊拖拽等操作,自動完成腳本的錄製,從而大幅度降低自動化維護成本。本人經過親身實踐,從UI自動化小白到第一個自動化測試腳本成功運行,用了半天時間,感興趣的同學可以嘗試一下。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"   AirtestIDE 是一個跨平臺的UI自動化測試編輯器,適用於遊戲和App。它的特點如下:","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"自動化腳本錄製、一鍵回放、報告查看,輕而易舉實現自動化測試流程支持","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"基於圖像識別的 Airtest 框架,適用於所有Android和Windows遊戲支持","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"基於UI控件搜索的 Poco 框架,適用於Unity3d,Cocos2d與Android App","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"能夠運行在Windows和MacOS上。","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"架構圖","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/f4/f491f1c7b8568925e3e8312976b4a4b3.jpeg","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"可以看到,底層的主要測試框架是AirTest和Poco,二者區別在於:","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"AirTest:基於Python的、跨平臺的UI自動化測試框架,基於圖像識別原理,適用於遊戲和APP。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Poco:基於UI控件搜索的自動化測試框架,其核心優勢是除了對Android 和IOS之外,對遊戲也是支持的,同時也支持微信小程序、微信小遊戲和H5應用。","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"三、Airtset安裝和連接設備","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"    目前AirTestIDE 提供了Windows和Mac兩個版本的客戶端,請從","attrs":{}},{"type":"link","attrs":{"href":"http://airtest.netease.com/changelog.html","title":null,"type":null},"content":[{"type":"text","marks":[{"type":"underline","attrs":{}}],"text":"官網","attrs":{}}]},{"type":"text","text":"下載,解壓即用。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"    無論是Android/IOS手機,還是Windows窗口,在AirTest中都將它視爲一個設備。下邊重點介紹一下Android設備的連接。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"連接Android手機","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"通過ADB連接電腦和Android手機,ADB是Google官方提供的Android調試工具,AirTestIDE依賴","attrs":{}},{"type":"link","attrs":{"href":"https://developer.android.com/studio/command-line/adb.html?hl=zh-cn","title":null,"type":null},"content":[{"type":"text","marks":[{"type":"underline","attrs":{}}],"text":"ADB","attrs":{}}]},{"type":"text","text":"與安卓設備進行通信。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"打開AirTestIDE,按照以下步驟進行連接:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"1.打開手機設置-開發者選項-USB調試開關,參考","attrs":{}},{"type":"link","attrs":{"href":"https://developer.android.com/studio/debug/dev-options.html#debugging","title":null,"type":null},"content":[{"type":"text","marks":[{"type":"underline","attrs":{}}],"text":"安卓官方文檔","attrs":{}}]},{"type":"text","marks":[{"type":"color","attrs":{"color":"#393939","name":"user"}}],"text":";","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#393939","name":"user"}}],"text":"2.在AirTestIDE設備面板中點擊 refresh ADB 按鈕,查看連接上的設備;","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#393939","name":"user"}}],"text":"3.如果沒有顯示出設備,試試restart ADB,如果還不行,參考","attrs":{}},{"type":"link","attrs":{"href":"https://airtest.doc.io.netease.com/","title":null,"type":null},"content":[{"type":"text","marks":[{"type":"underline","attrs":{}}],"text":"FAQ文檔","attrs":{}}]},{"type":"text","marks":[{"type":"color","attrs":{"color":"#393939","name":"user"}}],"text":"進行問題排查。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#393939","name":"user"}}],"text":"4.能夠成功看到設備後,點擊對應設備的connect 按鈕,進行初始化。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#393939","name":"user"}}],"text":"連接成功後,即可在AirTestIDE中看到手機屏幕的鏡像顯示,並進行實時操作,如下圖所示:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/32/32f0494946a16e2c64b1c8126517fc28.png","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"連接IOS手機","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"要連接一臺IOS手機,你需要準備好一臺安裝了Xcode的MAC電腦,連接方式參考","attrs":{}},{"type":"link","attrs":{"href":"https://airtest.doc.io.netease.com/","title":null,"type":null},"content":[{"type":"text","marks":[{"type":"underline","attrs":{}}],"text":"文檔","attrs":{}}]},{"type":"text","marks":[{"type":"color","attrs":{"color":"#393939","name":"user"}}],"text":"。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"四、錄製自動化腳本","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在連接上設備以後,就可以開始錄製自動化測試腳本了,在接下來的內容中,我將會在一臺Android手機上,給大家演示如何錄製腳本。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"模擬輸入","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"AirTest支持通過圖像識別的方式,找到你想要的位置並進行操作,這是基於AirTest框架實現的。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們可以先看看如何自動錄製腳本:點擊AirTestIDE左側的AirTest輔助窗上的“錄製”按鈕,然後隨着你在設備窗口上操作手機,代碼會自動生成在代碼窗口中。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"錄製完成後,點擊“運行”,就可以運行你的第一個自動化腳本了。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/1a/1afa537f1ba02d0df622a7a60fa0d0c1.jpeg","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果你覺得自動錄製生成的圖標不夠準確,還可以點擊AirTest輔助窗上的 touch 按鈕,然後再設備窗口上框選精確的圖標,也可以制動生成一條touch語句。如下:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"https://v.qq.com/txp/iframe/player.html?vid=e3252k79dir","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"類似的模擬輸入操作還有滑動:點擊swipe按鈕,在設備窗口上框選精確的圖標最爲滑動起點,然後點擊滑動終點位置,即會自動生成一個swipe語句。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"其他的模擬輸入的API包括:","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Text:文字輸入","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"KeyEvent:按鍵輸入,包括(HOME/BACK/MENU等)","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Sleep:等待","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Snapshot:截屏","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"斷言","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"到這裏,我們已經有各種模擬輸入方法,配合邏輯控制語句讓手機動起來。自動化測試中還有很重要的一個步驟:結果驗證。我們就可以來看看怎樣聲明斷言。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"(1)驗證UI界面","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"錄製方法與模擬輸入類似。","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"assert_exists:斷言圖片存在","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"assert_not_exists:斷言圖片不存在","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"(2)驗證數值","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"通過Poco獲取屬性值,手寫代碼進行斷言。","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"assert_equal:斷言箱單","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"assert_not_equal:斷言不等","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"例如","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/e7/e782b9b71f248099e86806a5ad97cb25.jpeg","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"查看測試報告","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"腳本運行完畢後,點擊“查看報告”那妞,會使用默認瀏覽器打開結果報告頁面,報告中將展示每一個步驟的內容和實際執行過程的截圖、運行結果,方便查看步驟是否執行成功。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/cc/cce496110e0cf1e82ad229c72ec01a32.png","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/e9/e9981d03d96b577e5bc6e3c86192cfeb.jpeg","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"五、AirTest腳本介紹","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"AirTest是一款基於Python的,跨平臺的UI自動化測試框架,基於圖像識別原理,適用於遊戲和APP。雖然可以藉助IDE提供的錄製功能快速的生成腳本,但是通常來說,熟練掌握Python語法能夠幫助我們寫出應用更廣泛、更不容易出錯的腳本。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"一個簡單的.air腳本解析","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在下載解壓Airtest腳本的專屬IDE——AirtestIDE後,點擊“新建腳本”按鈕,默認即可創建一個後綴名爲.air的腳本文件,.air這是Airtest腳本的專屬後綴。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"讓我們打開剛纔新建腳本的文件夾,可以看到實際上.air腳本文件是一個普通的文件夾,裏面附帶了一個同名的.py文件,AirtestIDE在執行腳本時,實際上執行的是裏面的.py文件。也就是說,Airtest腳本本質上仍是Python腳本,遵循的是Python語法,我們可以根據實際需要自由地import其它Python第三方庫。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"值得注意的是,.air文件夾中必須要有同名的.py文件,否則在命令行執行airtest run test.air這樣的運行指令時會導致失敗。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"AirTest腳本示例","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/e9/e901d86635b46b8efc1b4825bd4cf4c4.jpeg","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"初始化環境","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"首先,就像一個普通的Python腳本一樣,我們需要在代碼文件的最開頭部分,寫上from airtest.core.api import \\*,將AirTest 的主要API都import進來,以便在後續腳本中使用這些API。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"auto_setup是一用來初始化環境的接口,接口文檔在","attrs":{}},{"type":"link","attrs":{"href":"https://airtest.readthedocs.io/zh_CN/latest/all_module/airtest.core.api.html#airtest.core.api.auto_setup","title":null,"type":null},"content":[{"type":"text","marks":[{"type":"underline","attrs":{}}],"text":"這裏","attrs":{}}]},{"type":"text","marks":[{"type":"color","attrs":{"color":"#393939","name":"user"}}],"text":",它接受4個參數,我們可以設置當前腳本所在的路徑、指定運行腳本的設備、設置默認的log路徑和設置腳本父路徑。","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#393939","name":"user"}}],"text":"如果auto_setup不傳入任何參數,AirTest將會讀取運行時命令行中傳入的各項參數,來對環境進行初始化。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#393939","name":"user"}}],"text":"在AirTestIDE創建腳本時,默認生成的代碼裏是最簡單的初始化代碼auto_setup(__file__),意思是將腳本文件作爲腳本路徑傳入,其他參數內容將默認讀取運行命令行傳入的參數。","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#393939","name":"user"}}],"text":"腳本運行命令行有兩種形式,命令行中的參數包含device,log等:","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#393939","name":"user"}}],"text":"命令行運行AirTest腳本的示例:>airtest run untitled.air --device Android:///手機設備號 --log log。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#393939","name":"user"}}],"text":"在使用AirTestIDE運行腳本時,會在\"Log 查看窗\"中自動生成一個可用的命令行,可以供大家作爲參考。","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"模擬點擊","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Airtest作爲自動化測試框架,模擬的是人的操作,常見的接口主要有:","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Touch點擊某個位置,可以設定被點擊的位置、次數、按住時長參數","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Swipe從一個位置華東到另一個位置","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Text調用輸入法輸入指定內容","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"KeyEvent輸入某個按鍵響應,禮盒回車鍵、刪除鍵","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Wait等待某個指定的圖片元素出現","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Snapshot對當前畫面截一張圖","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"其他","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"核心API請參見","attrs":{}},{"type":"link","attrs":{"href":"https://airtest.readthedocs.io/zh_CN/latest/all_module/airtest.core.api.html","title":null,"type":null},"content":[{"type":"text","marks":[{"type":"underline","attrs":{}}],"text":"文檔","attrs":{}}]},{"type":"text","marks":[{"type":"color","attrs":{"color":"#393939","name":"user"}}],"text":",在這個文檔頁裏出現的API都是","attrs":{}},{"type":"text","text":"跨平臺API,由於我們在代碼的第一行裏將airtest.core.api裏的接口全部import進來了,因此這些API可以在代碼裏直接進行調用,像這樣:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/c3/c3be1348a112222406471f8a0dc40ab1.jpeg","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在很多接口中,支持傳入Template圖片對象作爲參數,在運行時將會去點擊圖片在畫面中的所在位置,類似這樣:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/b8/b87fba9bc81d264b2415dddd9c90ac53.jpeg","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"其中,Template對象是一個圖片類,AirTest會先嚐試在當前畫面中找到匹配這張圖片的位置,如果找到了,將對這個座標進行點擊操作,如果找不到,就拋出識別異常。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"斷言語句","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"斷言在單元測試代碼中非常重要,因此建議在我們的腳本里使用斷言語句來判定被測應用當前的漲停是否是我們預期中的狀態。Airtest提供了assert_exists和assert_not_exists兩個接口,來斷言一張圖片存在或者不存在於當前畫面中。同時,還提供了assert_equal和assert_not_equal兩個語句,來斷言傳入的兩個值相等或者不相等。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"如何在Python腳本中使用AirTest","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"AirTestIDE在創建新腳本時,也能夠直接創建一個.py腳本文件,但是在創建之前會彈出一個設置窗口,要求填寫一些指定的參數。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在我們瞭解過auto_setup接口後就會知道,這些參數就是爲了傳給它,然後初始化AirTest運行環境使用的。因此,一個純.py腳本的初始化代碼可以是這樣的:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/97/9767a03c0d4689235d1bf75df77d5cfb.jpeg","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"上邊這段代碼的意思是說,當使用python xxx.py來運行本文件,且不帶任何命令行參數是,則自動使用auto_setup這個接口來對AirTest相關的參數進行初始化。這樣只需要在寫py腳本時,填好指定的參數就能直接用python xxx.py指令來運行腳本。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"同時,原先傳統的airtest run xxx.air --devices Android: /// 命令行方式也不受影響,只要腳本檢測到傳入了命令行參數(即代碼中的 if not cli_setup()判斷),就依然優先使用命令行參數來初始化AirTest環境。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當然,熟練掌握API的各位,也可以根據實際需求在自己的Python腳本中調用AirTest API,與使用正常的pyhton第三方庫方法相同。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"六、總結","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"本文只是簡單講解了如何使用AirTest進行UI自動化腳本的錄製和回放,以及對自動化腳本進行了介紹。如果要進行UI自動化的持續集成,還需要進一步的瞭解和學習。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"希望有時間的同學可以進行一些實踐,瞭解一下UI自動話相關的知識。在以後的工作中,我們就","attrs":{}},{"type":"text","marks":[{"type":"color","attrs":{"color":"#494949","name":"user"}}],"text":"可以將測試重複度高,執行效率低的用例,通過UI自動化快速重複執行,以達到提高測試效率的目的。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#393939","name":"user"}}],"text":"此外,本文只是介紹了通過AirTest的框架進行圖像識別,編寫測試腳本,但是在某些特殊情況下,例如對於遊戲或者APP裏的動態元素,通過圖像識別比較困難。AirTest還提供了另外一種基於UI控件搜索的自動化框架Poco,同學們可以在下邊的文章中,進行學習和實踐。","attrs":{}},{"type":"text","text":"詳情請查看:","attrs":{}},{"type":"link","attrs":{"href":"https://edu.uwa4d.com/lesson-detail/124/470/0?isPreview=0","title":null,"type":null},"content":[{"type":"text","marks":[{"type":"underline","attrs":{}}],"text":"https://edu.uwa4d.com/lesson-detail/124/470/0?isPreview=0","attrs":{}}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"文|Lynne","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"關注得物技術,攜手走向技術的雲端","attrs":{}}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章