Uiautomator 2.0之UiWatcher类学习小记

1. 主要功能

使用此方法可以处理中断问题,从而保证测试用例的正常运行。

2. 相关API

API 说明
registerWatcher (String name, UiWatcher watcher) 注册一个监听器,当无法匹配到对象的时候,触发监听器
removeWatcher (String name) 取消之前注册的指定的监听器
resetWatcherTriggers () 重置已出发过的UiWatcher
runWatchers () 运行所有的监听器

3.简单示例

3.1 用例代码

@Test
    public void testCase09() throws InterruptedException {

        final UiObject2 ui = mDevice.findObject(By.text("短信"));
        //注册监听器
        mDevice.registerWatcher("testWatcher", new UiWatcher() {
            @Override
            public boolean checkForCondition() {
                if(mDevice.hasObject(By.text("联系人"))){
                    ui.click();
                    Log.i("testWatcher", "监听器被触发了");
                    return true;
                }
                Log.i("testWatcher", "监听器未被触发");
                return false;
            }
        });

        //运行用例步骤
        mDevice.wait(Until.findObject(By.text("写短信")), 2000);
        UiObject2 btn = mDevice.findObject(By.text("写短信"));
        btn.click();
        Thread.sleep(2000);
        mDevice.pressBack();

        //重置监听器
        mDevice.resetWatcherTriggers();
        mDevice.wait(Until.findObject(By.text("写短信")), 2000);
        btn.click();
        Thread.sleep(2000);
        mDevice.pressBack();
        Log.i("testWatcher", "重置监听器成功");

        //移除监听器
        mDevice.removeWatcher("testWatcher");
        Log.i("testWatcher", "移除监听器成功");
        mDevice.wait(Until.findObject(By.text("写短信")), 2000);
        btn.click();
        Thread.sleep(2000);
        mDevice.pressBack();

    }
3.2 运行结果



你可能感兴趣的:(android,测试)