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;
}
});
}

本文介绍了一种在测试过程中遇到来电时自动挂断电话的解决方案,通过注册监听器并检查是否出现来电状态,从而允许测试继续进行。此方法避免了意外弹出框导致测试终止的问题。

被折叠的 条评论
为什么被折叠?



