标签:
缘来是你:
在基于UI谷歌库的测试系统对第三方APK测试例,存在不定时弹窗问题,对测试例的健壮性和稳定性产生了很大影响。
为了解决这个问题,谷歌开源了UIwatcher 类来解决此问题。
附谷歌官网类解析地址:http://developer-android.ir/tools/help/uiautomator/UiWatcher.html#checkForCondition%28%29
问问你是谁:
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
abstract boolean | checkForCondition()
The testing framework calls this handler method automatically when the framework is unable to find a match using the
UiSelector . |
即: 在UiSelector 方法找不到匹配的时候,便调用uiwatcher类的此方法。
使用方法:
首先要定义一个类对象:示例如下
1 UiWatcher okCancelDialogWatcher = new UiWatcher() { 2 @Override //重写此方法 3 public boolean checkForCondition() { 4 UiObject okCancelDialog = new UiObject(new UiSelector().textStartsWith("Lorem ipsum")); 5 if(okCancelDialog.exists()){ 6 Log.w(LOG_TAG, "Found the example OK/Cancel dialog"); 7 UiObject okButton = new UiObject(new UiSelector().className("android.widget.Button").text("OK")); 8 try { 9 okButton.click(); 10 } catch (UiObjectNotFoundException e) { 11 // TODO Auto-generated catch block 12 e.printStackTrace(); 13 } 14 15 return (okCancelDialog.waitUntilGone(25000)); 16 } 17 return false; 18 } 19 };
其次若想其发挥作用,必须先要注册然后在应用之前调用,其相关函数:
Registers a UiWatcher
to run automatically when the testing framework is unable to find a match using a UiSelector
. See runWatchers()
watcher uiwatcher
Removes a previously registered UiWatcher
. See registerWatcher(String, UiWatcher)
UiWatcher
that has been triggered. If a UiWatcher runs and its checkForCondition()
call returned true
, then the UiWatcher is considered triggered. See egisterWatcher(String, UiWatcher)
registerWatcher(String, UiWatcher)
UiWatcher
have triggered. See registerWatcher(String, UiWatcher)
See hasWatcherTriggered(String)
public boolean hasWatcherTriggered (String watcherName) Checks if a specific registered UiWatcher
has triggered. See registerWatcher(String, UiWatcher)
. If a UiWatcher runs and its checkForCondition()
call returned true
, then the UiWatcher is considered triggered. This is helpful if a watcher is detecting errors from ANR or crash dialogs and the test needs to know if a UiWatcher has been triggered.
实际应用Demo:
测试代码:
public void SendMessage(UiDevice dut, Bundle bundle) throws UiObjectNotFoundException, RemoteException, InterruptedException,UIAException{ int x=dut.getDisplayWidth(); int y=dut.getDisplayHeight(); Thread.sleep(3000); UiObject SetNum = new UiObject(new UiSelector().packageName("com.p1.chompsms").resourceId("com.p1.chompsms:id/recipients_field")); //此为控件1号,刚开始是找不见的。 Thread.sleep(10000); String phone=bundle.getString("phoneNum"); if(SetNum.exists()) SetNum.setText(phone); Thread.sleep(1000);
uiwatcher代码:
public void Game_uiwatcher(UiDevice dut, Bundle bundle) throws UiObjectNotFoundException, UIAException, InterruptedException
{
//定义
UiWatcher okCancelDialogWatcher = new UiWatcher() {
@Override
public boolean checkForCondition() {
System.out.println("uiwatcher run ing.....");
UiObject SetTend= new UiObject(new UiSelector().className("oandroid.widget.LinearLayout").index(1).childSelector(new UiSelector().className("android.widget.TextView").index(0)));
System.out.println("uiwatcher run ing.....");
if(SetTend.exists()){
try {
SetTend.click();
} catch (UiObjectNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return (SetTend.waitUntilGone(25000));
}
return false;
}
};
//注册
UiDevice.getInstance().registerWatcher(MYOKCANCELDIALOGWATCHER_STRING, okCancelDialogWatcher);
//启用
UiDevice.getInstance().runWatchers(); }
uiwatcher需要在调用前注册并启用过程:
Game_uiwatcher()方法注册并调用
然后测试函数SendMessage()运行时方才起作用。
标签:
原文地址:http://www.cnblogs.com/udld/p/4330848.html