標籤:
緣來是你:
在基於UIGoogle庫的測試系統對第三方APK測試例,存在不定時彈窗問題,對測試例的健壯性和穩定性產生了很大影響。
為瞭解決這個問題,Google開源了UIwatcher 類來解決此問題。
附Google官網類解析地址: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 block12 e.printStackTrace();13 }14 15 return (okCancelDialog.waitUntilGone(25000));16 }17 return false;18 }19 };
其次若想其發揮作用,必須先要註冊然後在應用之前調用,其相關函數:
public void registerWatcher (String name, UiWatcher watcher)
Registers a UiWatcher to run automatically when the testing framework is unable to find a match using a UiSelector. See runWatchers()
Parameters name to register the UIWatcher
watcher uiwatcher
public void
removeWatcher (String name)
Removes a previously registered UiWatcher. See registerWatcher(String, UiWatcher)
Parameters name used to register the UIWatcher public void resetWatcherTriggers () Resets a
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)
public void runWatchers () This method forces all registered watchers to run. See
registerWatcher(String, UiWatcher) public boolean hasAnyWatcherTriggered () Checks if any registered
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.
Returns true if triggered else false
實際應用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() {@Overridepublic 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 blocke.printStackTrace();}return (SetTend.waitUntilGone(25000));}return false;}};
//註冊
UiDevice.getInstance().registerWatcher(MYOKCANCELDIALOGWATCHER_STRING, okCancelDialogWatcher);
//啟用
UiDevice.getInstance().runWatchers(); }
uiwatcher需要在調用前註冊並啟用過程:
Game_uiwatcher()方法註冊並調用
然後測試函數SendMessage()運行時方才起作用。
UI Watcher 解決不定時彈窗問題