iOS UI Test總結
新增UI Test target
1.如果是新建專案,在新增新專案時,勾選UI Test,即可新增UI Test。
![674853-a7ea478b39129107.png](https://i.iter01.com/images/23ba4006a7c4e4e6db5f6bf5056dedc81003215e25cb352f38767f14b71eb9e9.png)
如果是為已有的專案新增UI Test,選擇File->New->Target 新建
![674853-bfeb2eb532007c01.png](https://i.iter01.com/images/2e0e52966ea40856c808ef3fda15341444a6326707492e233a0e302bf6f367b5.png)
2.選擇iOS UI Testing Bundle
![674853-36ba0a1045270dcb.png](https://i.iter01.com/images/8e876c8141d343526327be4b8cc499ac2659d0122938fa1eafde6ac722b54014.png)
3.為UI Test命名,並選擇依賴的target
![674853-94bc74d43a10f4cb.png](https://i.iter01.com/images/819d3812b9629c57e12272fc26b55d7a2c68a58b16593e0e5b251dcd8497b4ae.png)
新增UI Test target後,會自動生成CalcUITests資料夾,其中,有CalcUITests.m和Info.plist兩個檔案。
![674853-76995a28fe87bd91.png](https://i.iter01.com/images/eb84ed68d23b7c3d1af473d920d0122a3b01ab1fb63e78a141636e746f56bce8.png)
錄製UI Test
1.如果想使用UI Test的錄製功能,則UI Test target必須配置相應的Build Phase。
![674853-3e97fa63dd4e9dec.png](https://i.iter01.com/images/8a3af8701fd0ccb4845ff9843042cdf3bd55e62154c60ca087245979d51aecab.png)
2.使用錄製按鈕開始錄製UI Test,此時模擬器會自動啟動,可以點選螢幕進行操作。
![674853-d9de53d63ce15c17.png](https://i.iter01.com/images/fb0611e3ad43020dbfc7e5c83bafe8366244ee96bc25191395770dd5709e05e8.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](https://i.iter01.com/images/ec3d5d66b83177746b91d2dace5d55e1282123f8adc2a15600feb58de8c3844c.gif)
UI Test
錄製好UI Test,點選該圖示進行UI Test。此時,模擬器自動啟動,並自動執行UI Test。
![674853-1c0aa715aff47ff0.png](https://i.iter01.com/images/e223fa4841a94b549f77896ac79dfd4936612d6b95c22d517eb389f7ae942ffe.png)
具體操作,如下圖:
![674853-7cf92380ddda53fc.gif](https://i.iter01.com/images/3d2dd3a9286a2e728fa7de7906b0f1d9f9579cf34bd0b36e0cafba4be0ad7b11.gif)
在真機上進行UI Test的效果與在模擬器上的效果一致。
遇到的問題
1.Please select a scheme where “iOS_Calc” is the executable。
解決方案:target一定要選擇關聯的target,而不是UI Test的target。
![674853-0f5154e956b3a657.png](https://i.iter01.com/images/58c574225c555e551fd340492f0fab5b070a05e7d10271a4cef66cc0dc4a7455.png)
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下載地址:
相關文章
- iOS開發UI篇--iOS動畫(Core Animation)總結iOSUI動畫
- iOS RunTime 總結iOS
- iOS RAC總結iOS
- Kendo UI Grid 使用總結UI
- iOS 小坑總結iOS
- iOS 面試總結iOS面試
- iOS 面試題總結iOS面試題
- iOS 知識點總結iOS
- iOS快取的總結iOS快取
- iOS中常見Crash總結iOS
- iOS 音訊-audioUnit 總結iOS音訊
- IOS元件化方案總結iOS元件化
- iOS 效能優化總結iOS優化
- UI技術總結--效能優化UI優化
- Element-UI個人使用總結UI
- iOS 多執行緒總結iOS執行緒
- iOS面試題總結(四)iOS面試題
- iOS面試題總結(三)iOS面試題
- iOS底層原理總結 – RunLoopiOSOOP
- iOS面試題總結(七)iOS面試題
- iOS面試題總結(五)iOS面試題
- iOS面試題總結(六)iOS面試題
- iOS底層面試總結iOS面試
- iOS開發經驗總結iOS
- iOS野指標定位總結iOS指標
- iOS面試題總結(一)iOS面試題
- IOS常用程式碼總結(一)iOS
- iOS底層原理總結 - RunLoopiOSOOP
- iOS面試題總結(二)iOS面試題
- iOS邊下邊播總結iOS
- 翻譯:iOS Swift單元測試 從入門到精通 Unit Test和UI測試 UITestiOSSwiftUI
- android ios UIAndroidiOSUI
- Kendo UI Grid 批量編輯使用總結UI
- iOS中WKWebView互動使用總結iOSWebView
- iOS開發經驗總結2iOS
- iOS @property 屬性相關的總結iOS
- iOS開發經驗總結3iOS
- iOS初學之填坑總結iOS
- iOS 動畫基礎總結篇iOS動畫