其他测试框架 Uiautomator 监听类 UiWatcher 场景应用

沈凯 · 2014年02月14日 · 最后由 思寒_seveniruby 回复于 2014年02月14日 · 1637 次阅读
本帖已被设为精华帖!

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;

            }
        });
    }
共收到 2 条回复 时间 点赞

注:补充 Uidevice 动作不会触发监听器!!

#1 楼 @shenkai600 这个特性还是挺有用的, 我看 appium 的代码里面也加了 watcher, 用来处理 ANR 等打断型的场景。

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册