iOS UI Test總結

weixin_33860722發表於2016-05-28

新增UI Test target

1.如果是新建專案,在新增新專案時,勾選UI Test,即可新增UI Test。

674853-a7ea478b39129107.png
新增新專案

如果是為已有的專案新增UI Test,選擇File->New->Target 新建

674853-bfeb2eb532007c01.png
已有的專案

2.選擇iOS UI Testing Bundle

674853-36ba0a1045270dcb.png
iOS UI Testing Bundle

3.為UI Test命名,並選擇依賴的target

674853-94bc74d43a10f4cb.png
配置

新增UI Test target後,會自動生成CalcUITests資料夾,其中,有CalcUITests.m和Info.plist兩個檔案。

674853-76995a28fe87bd91.png
檔案目錄

錄製UI Test

1.如果想使用UI Test的錄製功能,則UI Test target必須配置相應的Build Phase。

674853-3e97fa63dd4e9dec.png
Build Phase

2.使用錄製按鈕開始錄製UI Test,此時模擬器會自動啟動,可以點選螢幕進行操作。

674853-d9de53d63ce15c17.png
錄製按鈕

3.在錄製的過程中,Xcode會根據使用者互動,自動生成程式碼。

4.再次點選錄製按鈕結束錄製。

生成的程式碼如下:
<pre><code>
XCUIApplication *app = [[XCUIApplication alloc] init];

[app.buttons[@"8"] tap];

[app.buttons[@"+"] tap];

[app.buttons[@"5"] tap];

[app.buttons[@"="] tap];

</pre></code>

具體操作,如下圖:(PS:因為是gif格式,有幾幀圖片丟失,所以8+5=13的過程不是特別連貫)

674853-297f0e25ac060cb1.gif
錄製UI Test.gif

UI Test

錄製好UI Test,點選該圖示進行UI Test。此時,模擬器自動啟動,並自動執行UI Test。

674853-1c0aa715aff47ff0.png
圖示

具體操作,如下圖:

674853-7cf92380ddda53fc.gif
UI Test.gif

在真機上進行UI Test的效果與在模擬器上的效果一致。

遇到的問題

1.Please select a scheme where “iOS_Calc” is the executable。

解決方案:target一定要選擇關聯的target,而不是UI Test的target。

674853-0f5154e956b3a657.png
target

2.編譯後報錯:Incomplete universal character name

由於是使用公司專案進行UI Test,專案中很多UI 元素都沒有tag,生成的指令碼都是以元素的title或其他屬性來區分。
生成的程式碼如下:
<pre><code>
XCUIApplication *app = [[XCUIApplication alloc] init];

[app.tabBars.buttons[@"\U60a3\U8005"] tap];



XCUIElementQuery *tablesQuery2 = app.tables;

XCUIElementQuery *tablesQuery = tablesQuery2;

[tablesQuery.buttons[@"\U79d1\U5ba4\U5de5\U5177"] tap];

[tablesQuery.staticTexts[@"\U65e5\U7a0b"] tap];



XCUIElement *reminderoverviewviewNavigationBar = 

app.navigationBars[@"ReminderOverviewView"];

[reminderoverviewviewNavigationBar.buttons[@"\U6dfb\U52a0"] tap];

[tablesQuery.textFields[@"\U6807\U9898"] tap];

[[[tablesQuery2 childrenMatchingType:XCUIElementTypeCell] elementBoundByIndex:0] 

childrenMatchingType:XCUIElementTypeTextField].element;

[app.buttons[@"Done"] tap];

[app typeText:@"\n"];

</pre></code>

解決方案:這其實是Xcode的一個bug,需要手動將\U替換為\u即可。

3.編譯後報錯:missing '[' at start of message send expression uitest

解決方案:在錄製UI Test的過程中,如果涉及到鍵盤輸入事件,Xcode生成的程式碼不完整,導致編譯不過。此時,需手動修改程式碼。

4.閃退

Xcode在進行UI Test過程中經常性閃退,希望蘋果能儘快解決這些bug,提高UI Test的使用者體驗。

總結

  • 提供錄製功能,減少程式碼輸入,方便開發者進行UI 測試

  • 錄製功能,還是有很多bug,經常閃退

  • 目前只能進行一些簡單的測試,且測試的過程還需要人工干預

  • 一次錄製可以在不同裝置、不同螢幕尺寸上進行自動化測試,在一定程度上解放了測試同學們的雙手

demo下載地址:

https://developer.apple.com/library/mac/samplecode/UnitTests/Introduction/Intro.html#//apple_ref/doc/uid/DTS40011742-Intro-DontLinkElementID_2

相關文章