iOS UI测试

作者: dpruin | 来源:发表于2016-11-11 10:24 被阅读319次

iOS UI测试

前言

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

创建UI测试

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


    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行为录制

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


    UITesting_2.png

相关文章

网友评论

    本文标题:iOS UI测试

    本文链接:https://www.haomeiwen.com/subject/zdqfpttx.html