您的位置:首页 > 产品设计 > UI/UE

Android UiAutomator:UiWatcher的使用

2016-06-21 12:18 806 查看
UiAutomator 是一个黑盒的自动化测试工具,UI界面被异常打断就会破坏脚本的正常运行。UiWatcher 可以处理某些可预知的打断场景。

这里例举来电打断场景处理。

代码

UiWatcher watcher = new UiWatcher(){
public boolean checkForCondition(){
UiObject Incall = new UiObject(new UiSelector().resourceId("com.android.incallui:id/answerFragment"));
if(Incall.exists())
{
try{
Incall.swapLeft(20);
return true;
}catch(Exception e){
e.printStackTrace();
return false;
}
}
else return false;
}
};
UiDevice.getInstance().registerWatcher("phone",watcher);
//注册UiWatcher


代码中处理来电打断的场景关闭来电的动作。

当UiSelector无法找到相关Object后,由testing framework启动UiWatcher中的checkForCondition方法。

相关资料

public abstract boolean checkForCondition ()

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息