The margin is for you:
In the UI-based Google Library test system for the third-party APK Test example, there is an irregular pop-up window problem, the robustness and stability of the test case has a great impact.
In order to solve this problem, Google Open source Uiwatcher class to solve this problem.
With Google's official website class resolution address: Http://developer-android.ir/tools/help/uiautomator/UiWatcher.html#checkForCondition%28%29
Ask who you are:
| Public Methods |
| Abstract Boolean |
checkforcondition () The testing framework calls this handler method automatically when the framework was unable to find a match using the c6/>. |
That is: This method of the Uiwatcher class is called when the Uiselector method cannot find a match.
How to use:
The first thing to define is a class object: an example of a
1Uiwatcher Okcanceldialogwatcher =NewUiwatcher () {2 @Override//Override this method3 Public Booleancheckforcondition () {4UiObject Okcanceldialog =NewUiObject (NewUiselector (). Textstartswith ("Lorem ipsum"));5 if(Okcanceldialog.exists ()) {6LOG.W (Log_tag, "Found The example Ok/cancel dialog");7UiObject OKButton =NewUiObject (NewUiselector (). ClassName ("Android.widget.Button"). Text ("OK"));8 Try {9 Okbutton.click ();Ten}Catch(uiobjectnotfoundexception e) { One //TODO auto-generated Catch block A e.printstacktrace (); - } - the return(Okcanceldialog.waituntilgone (25000)); - } - return false; - } +};
Second, if you want it to work, you must first register and then invoke it before applying it, its related functions:
Public void registerwatcher (String name, Uiwatcher watcher)
Registers A to UiWatcher run automatically when the testing framework was unable to find a match using a UiSelector . SeerunWatchers()
Parameters name to register the Uiwatcher
Watcher Uiwatcher
Public
void
removewatcher (String name)
Removes a previously registered UiWatcher . SeeregisterWatcher(String, UiWatcher)
Parameters name used to register the UiwatcherPublic void resetwatchertriggers () Resets a
UiWatcherThat 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 any registered watchers to run. See
registerWatcher(String, UiWatcher)Public Boolean hasanywatchertriggered () Checks if any registered
UiWatcherHave triggered. See
registerWatcher(String, UiWatcher)See
hasWatcherTriggered(String)
Public
Boolean haswatchertriggered (String watchername)
Checks if a specific registered has UiWatcher triggered. See registerWatcher(String, UiWatcher) . If a Uiwatcher runs and its call checkForCondition() returned true , then the uiwatcher is considered triggered. This is helpful if a watcher are detecting errors from ANR or crash dialogs and the test needs to know if a uiwatcher have B Een triggered.
Returns true if triggered else false
Actual Application Demo:
Test code:
public void SendMessage (Uidevice dut, bundle bundle) throws Uiobjectnotfoundexception, RemoteException, Interruptedexception,uiaexception{int x=dut.getdisplaywidth (); int y=dut.getdisplayheight (); Thread.Sleep (+); UiObject setnum = new UiObject (new Uiselector (). PackageName ("Com.p1.chompsms"). ResourceId ("Com.p1.chompsms:id/ Recipients_field ")); This is the control number 1th, which was not found at first. Thread.Sleep (10000); String phone=bundle.getstring ("Phonenum"); if (setnum.exists ()) setnum.settext (phone); Thread.Sleep (1000);
Uiwatcher Code:
public void Game_uiwatcher (Uidevice dut, bundle bundle) throws Uiobjectnotfoundexception, Uiaexception, interruptedexception{ //Definition 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;}};
Registered
Uidevice.getinstance (). Registerwatcher (myokcanceldialogwatcher_string, Okcanceldialogwatcher);
Enable
Uidevice.getinstance (). Runwatchers (); }
Uiwatcher need to register and enable the procedure before calling:
The Game_uiwatcher () method registers and invokes the
then test the function The SendMessage () runtime has no effect.
UI Watcher to solve the problem of timed pop-up windows