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];
    
复制代码
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值