iOS UI測(cè)試
前言
- UITesting 和 Accessibility
- 測(cè)試app的功能和UI界面是否正確, UI測(cè)試可以省去人為直接操作app進(jìn)行測(cè)試。
創(chuàng)建UI測(cè)試
- File——New——Target——iOS UI Testing Bundle
-
也可以如下圖操作
UITesting_1.png
代碼
- 前提須寫的代碼:設(shè)置UI控件accessibilityIdentifier屬性
- (void)viewDidLoad {
[super viewDidLoad];
self.userTextField.accessibilityIdentifier = @"userTextField";
self.passwordTextField.accessibilityIdentifier = @"passwordTextField";
// 輔助標(biāo)識(shí)
self.loginBtn.accessibilityIdentifier = @"login";
}
- 測(cè)試樣例
- (void)testEmptyUserNameAndPassword {
// XCUIApplication app對(duì)象代理 繼承自XCUIElement
XCUIApplication *app = [[XCUIApplication alloc] init];
[app.buttons[@"login"] tap];
// XCUIElement UI元素的代理
XCUIElement *label = app.alerts.staticTexts[@"Empty username/password"];
// XCUIElementQuery 查詢UI元素的類
XCUIElementQuery *alerts = app.alerts;
NSPredicate *alertCount = [NSPredicate predicateWithFormat:@"count == 1"]; // XCUIElementQuery有count屬性 ,元素?cái)?shù)量
NSPredicate *labelExist = [NSPredicate predicateWithFormat:@"exists == 1"]; // XCUIElement有exists屬性,是否存在
[self expectationForPredicate:alertCount evaluatedWithObject:alerts handler:nil];
[self expectationForPredicate:labelExist evaluatedWithObject:label handler:nil];
[self waitForExpectationsWithTimeout:5 handler: nil];
}
UI行為錄制
-
將輸入光標(biāo)放在方法實(shí)現(xiàn)中,并點(diǎn)擊工具欄上的錄制按鈕,就可以進(jìn)行實(shí)時(shí)錄制了
UITesting_2.png

