Detailed description of the 6.UiWatcher API

Source: Internet
Author: User

1. The listener is not complete, so if the use case needs to set up a listener to prevent the use case from being interrupted, it is best to increase the delay time by a bit
2.UiDevice is not triggering the monitoring function
3. Whether the listener is a program or is interrupted in the method body or in the loop body
4. The listener is to be executed before the use case is interrupted

First, Uiwatcher class introduction and interrupt monitoring Check condition 1.UiWatcher class description

1) Uiwatcher is used to handle the non-imagined steps encountered during script execution
2) Uiwatcher Usage Scenarios

There was a call during the test.

I got a text message during the test.

The alarm rang during the test.

2. Interrupt the Monitoring check condition



public boolean checkforcondition (); Use the Uiselector test framework to automatically invoke the method of this handler when the test framework cannot find a match. When a match is found at a time-out, the framework calls the Checkforcondition () method to check all registered listening checks on the device.

For example:

    • If a dialog box appears, you can check whether the service information sent by the operator is not a test exception.

Second, the listener operation and inspection

1. Related APIs

return value



void Registerwatcher (String Name,uiwatcher Watcher) Registers a listener that triggers the listener when Uiselector cannot match to the object
void Removewatcher (String name) Cancels the specified listener before registering
void Resetwatchertriggers () Resets the uiwatcher that have been triggered, after resetting the equivalent of not running
void Runwatchers () Force all listeners to run
Boolean Hasanywatchertriggered () Check if a listener has triggered
Boolean Haswatchertriggered (String watchername) Check if a particular listener has triggered

 Public voidTestDemo2 ()throwsuiobjectnotfoundexception{//1. Register the Listener first (Registerwatcher (String Name,uiwatcher watcher))Uidevice.getinstance (). Registerwatcher ("Phone",NewUiwatcher () { Public Booleancheckforcondition () {//1. Hang up the phoneUiObject call=NewUiObject (NewUiselector (). Text ("Incoming call")); UiObject View=NewUiObject (NewUiselector (). ClassName ("Android.view.View")); if(Call.exists ()) {System.out.print ("The phone listener is triggered."); Try{view.swipeleft (20); return true; } Catch(uiobjectnotfoundexception e) {e.printstacktrace (); }            }            return false;    }    }); //1. Register a listener againUidevice.getinstance (). Registerwatcher ("Massage",NewUiwatcher () { Public Booleancheckforcondition () {System.out.print ("SMS monitoring is triggered."); return false;    }    }); //2. Perform use case steps//locating a parent class using Index+classnmae +text locating a subclass end to object [sound button]UiObject Vol =NewUiObject (Newuiselector (). ClassName ("Android.widget.LinearLayout"). Index (6). Getchild (NewUiselector (). Text ("Sound")); //get the Back button for the next level of interfaceUiObject back=NewUiObject (NewUiselector (). Description ("Sound: Navigate Up"));//      //using a For loop listener does not work//for (int i=0;i<10;i++) {//          //number of output cycles://System.out.println ("Current cycle" + (i+1) + "times;");//          //Click on the object and wait until a new window appears//Vol.clickandwaitfornewwindow ();//sleep (+);//          //Click the Back button//Back.clickandwaitfornewwindow ();//sleep (+);//      }    //you can enter the phone during the following five operations to debug//Click on the object for the first time and wait until the new window appearsVol.clickandwaitfornewwindow (); Sleep (2000); //Click the Back buttonBack.clickandwaitfornewwindow (); Sleep (2000); //second timeVol.clickandwaitfornewwindow (); Sleep (2000);    Back.clickandwaitfornewwindow (); Sleep (2000); //third timeVol.clickandwaitfornewwindow (); Sleep (2000);    Back.clickandwaitfornewwindow (); Sleep (2000); //fourth timeVol.clickandwaitfornewwindow (); Sleep (2000);    Back.clickandwaitfornewwindow (); Sleep (2000); //Fifth timeVol.clickandwaitfornewwindow (); Sleep (2000);    Back.clickandwaitfornewwindow (); Sleep (2000); //closes the specified listener (removewatcher (String name) after the fifth return, so that it will not be triggered after this timeUidevice.getinstance (). Removewatcher ("Phone"); //and then force all monitors to run .uidevice.getinstance (). Runwatchers (); //Sixth timeVol.clickandwaitfornewwindow (); Sleep (2000);    Back.clickandwaitfornewwindow (); Sleep (2000); //check and output the listener has been triggered after the fifth return//Check if a listener has been triggered    Booleanb=uidevice.getinstance (). hasanywatchertriggered (); //detects if a specific listener has been triggered    BooleanPhone1=uidevice.getinstance (). haswatchertriggered ("Phone"); BooleanMassage1=uidevice.getinstance (). haswatchertriggered ("Massage"); System.out.println ("Has the listener been triggered" +B); System.out.println ("Has the phone listener been triggered" +Phone1); System.out.println ("Has the SMS listener been triggered" +Massage1); //logical judgment then reset and force the Operation    if(b==true){        //Resetting a listener that has been runuidevice.getinstance (). Resetwatchertriggers (); }}

Detailed description of the 6.UiWatcher API

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.