iOS UI测试

iOS UI测试

前言

  • UITesting 和 Accessibility
  • 测试app的功能和UI界面是否正确, UI测试可以省去人为直接操作app进行测试。

创建UI测试

  • File——New——Target——iOS UI Testing Bundle
  • 也可以如下图操作


    iOS UI测试_第1张图片
    UITesting_1.png

代码

  • 前提须写的代码:设置UI控件accessibilityIdentifier属性
- (void)viewDidLoad {
    [super viewDidLoad];
    self.userTextField.accessibilityIdentifier = @"userTextField";
    self.passwordTextField.accessibilityIdentifier = @"passwordTextField";
    // 辅助标识
    self.loginBtn.accessibilityIdentifier = @"login";
    
}

  • 测试样例
- (void)testEmptyUserNameAndPassword {
    
    // XCUIApplication app对象代理 继承自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属性 ,元素数量
    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行为录制

  • 将输入光标放在方法实现中,并点击工具栏上的录制按钮,就可以进行实时录制了


    iOS UI测试_第2张图片
    UITesting_2.png

你可能感兴趣的:(iOS UI测试)