添加UI Test target
1.如果是新建項目,在添加新項目時,勾選UI Test,即可添加UI Test。

如果是為已有的項目添加UI Test,選擇File->New->Target 新建

2.選擇iOS UI Testing Bundle

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

添加UI Test target后,會自動生成CalcUITests文件夾,其中,有CalcUITests.m和Info.plist兩個文件。

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

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

3.在錄制的過程中,Xcode會根據(jù)用戶交互,自動生成代碼。
4.再次點擊錄制按鈕結(jié)束錄制。
生成的代碼如下:
<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的過程不是特別連貫)

UI Test
錄制好UI Test,點擊該圖標進行UI Test。此時,模擬器自動啟動,并自動運行UI Test。

具體操作,如下圖:

在真機上進行UI Test的效果與在模擬器上的效果一致。
遇到的問題
1.Please select a scheme where “iOS_Calc” is the executable。
解決方案:target一定要選擇關(guān)聯(lián)的target,而不是UI Test的target。

2.編譯后報錯:Incomplete universal character name
由于是使用公司項目進行UI Test,項目中很多UI 元素都沒有tag,生成的腳本都是以元素的title或其他屬性來區(qū)分。
生成的代碼如下:
<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過程中經(jīng)常性閃退,希望蘋果能盡快解決這些bug,提高UI Test的用戶體驗。
總結(jié)
提供錄制功能,減少代碼輸入,方便開發(fā)者進行UI 測試
錄制功能,還是有很多bug,經(jīng)常閃退
目前只能進行一些簡單的測試,且測試的過程還需要人工干預
一次錄制可以在不同設(shè)備、不同屏幕尺寸上進行自動化測試,在一定程度上解放了測試同學們的雙手