UITesting:设置accessibilityIdentifier属性

首先为控件添加accessibilityIdentifier属性,以最常见的button为例:

_leftButton.accessibilityIdentifier = @"live_broadcast button";

以test为方法名前缀,写个点击的小测试:

- (void)testStartLiving{
    //这里给个重新启动,若不需要,可去掉这行
    [self setUp];
    //循环点击10次
    for (int i = 0; i < 10; i ++) {
        [self startLivingApplication];
    }
}

- (void)startLivingApplication{
    //获取当前的window和所有控件
    XCUIApplication *app = [[XCUIApplication alloc] init];
    XCUIElement *windown = app.windows.allElementsBoundByIndex[0];

    XCUIElement *liveBroadcastButtonButton = app.buttons[@"live_broadcast button"];
    //私有方法,判断是否可点击
    if ([self canOperateElement:liveBroadcastButtonButton];) {
        [liveBroadcastButtonButton pressForDuration:2.0];
    }
}

- (BOOL)canOperateElement:(XCUIElement *)element{
    if (element != nil) {
        if (element.exists) {
            if (element.hittable) {
                return YES;
            }
        }
    }
    return NO;
}

至此,一个简单的点击操作就完成了。

备注:
1、关于canOperateElement方法,是为了防止点击了不存在或者无法点击的控件,发生错误;
2、若出现以下错误,说明当前想要操作的控件不再可视范围内,这个错误暂时没有明确的解决方法;

failed: UI Testing Failure - Failed to scroll to visible (by AX action) Button 0x12e762170: traits: 8589934593, {{9.0, 24.0}, {47.5, 32.0}}, label: 'Button', error: Error -25204 performing AXAction 2003

可能出现的原因有:
(1)当前view图层过于复杂,控件布局混乱,点击时触发了其他控件;
(2)当前view没有完全显示,找到了预期想要操作的控件,但是当前不能点击。
尝试以下方法来解决这个问题:

    NSPredicate *existPredicate = [NSPredicate predicateWithFormat:@"exists == true"];
    [self expectationForPredicate:existPredicate evaluatedWithObject:liveBroadcastButtonButton handler:nil];
    
    //直播按钮页 view弹出预留时间
    if (![self canOperateElement:liveBroadcastButtonButton]) {
        //强制点击操作
        [liveBroadcastButtonButton customTapElement:CGVectorMake(0.0, 0) pressDuration:4.0];
    }else{
        [liveBroadcastButtonButton tap];
    }
    [self waitForExpectationsWithTimeout:5 handler:nil];
    
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,983评论 25 708
  • 《ilua》速成开发手册3.0 官方用户交流:iApp开发交流(1) 239547050iApp开发交流(2) 1...
    叶染柒丶阅读 10,913评论 0 11
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥ios动画全貌。在这里你可以看...
    每天刷两次牙阅读 8,551评论 6 30
  • 本来周计划应该是昨天制定的,但由于昨天开学,各种聚会,没来得及。今天补上来。 本学期开学,跟着老师做...
    lxx的壳阅读 203评论 1 1
  • 【与泽齐新】数理妈妈思维营践行d29 今天他要求玩“猜拳”的游戏。没想到他会喜欢玩这个游戏。虽然都猜不对,但是都伸...
    嗨新新新阅读 135评论 0 0