Uiautomator 监听类 UiWatcher 场景应用

84 篇文章 1 订阅
34 篇文章 0 订阅

The testing framework calls this handler method automatically when the framework is unable to find a match using the UiSelector. When a match is not found after a predetermined time has elapsed, the framework calls the checkForCondition() method of all registered watchers on the device. You can use this method to handle known blocking issues that are preventing the test from proceeding. For example, you can check if a dialog appeared that is blocking the the test, then close the dialog or perform some other appropriate action to allow the test to continue.

Returns
true to indicate a matched condition, or false if no matching condition is found

这是我直接从官方文档上扣下来的,简单来说,当你Uiobject对象无法匹配Uiselector条件时会触发当前所有已注册运行的监听器,避免测试中有类似意外弹出框导致终止运行.
以下就测试中来电挂断场景为例,各位测试同仁如遇困惑可参考.监听器必须在case前运行,一般在方法第一行注册this.watcherEndTheCall();

private void watcherEndTheCall(){
        UiDevice.getInstance().registerWatcher("endCall", new UiWatcher() {
            UiObject incomingCall = new UiObject(new UiSelector()
                                        .resourceId("com.android.phone:id/callStateLabel"));

            @Override
            public boolean checkForCondition() {
                System.out.println("watcherEndCall running");
                if(incomingCall.exists()){
                    try {
                        Runtime.getRuntime().exec("input keyevent 5");
                        sleep(1000);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    System.out.println("endCall success");
                    return true;
                }
                // TODO Auto-generated method stub
                System.out.println("endCall fail");
                return false;

            }
        });
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值