UiWatcher example for Android UIAutomator not working -


import com.android.uiautomator.core.uiobject; import com.android.uiautomator.core.uiobjectnotfoundexception; import com.android.uiautomator.core.uiselector; import com.android.uiautomator.core.uiwatcher; import com.android.uiautomator.testrunner.uiautomatortestcase;

public class uiwatcherdemo extends uiautomatortestcase {    private static final string nointernet_string = "internetwatcher";    public void testwatcherdemotestexample() throws uiobjectnotfoundexception {        // define watcher , register //       uiwatcher internetwatcher = new uiwatcher() {           @override           public boolean checkforcondition() {               uiobject noconnobj = new uiobject(new uiselector().text("no connection"));               if(noconnobj.exists()) {                   uiobject retrybutton = new uiobject(new uiselector().classname("android.widget.button").text("retry"));                   try {                       retrybutton.click();                       try { thread.sleep(3000l); } catch(exception e) {}                       getuidevice().presshome();                   } catch (uiobjectnotfoundexception e) {                       e.printstacktrace();                   }               }               return false;           }       };       getuidevice().registerwatcher(nointernet_string, internetwatcher);       getuidevice().runwatchers();        // app test code //       getuidevice().presshome();       uiobject allappsbutton = new uiobject(new   uiselector().description("apps"));       allappsbutton.clickandwaitfornewwindow();       uiobject appstab = new uiobject(new uiselector().description("shop"));       appstab.clickandwaitfornewwindow();    } } 

ok understand how uiwatcher works ... note : uiwatcher invoke when api in retry mode . means when api call not able find element ui .. ui library automatically invoke registered watcher.

so per above example, open google play when internet connection off .. want watcher alive , check above condition , click retry , go home page. add these 2 line @ end of code .. in retry mode u can see registered watcher start working.

// below line in retry mode , watcher invoke automatically //         uiobject contact = new uiobject(new uiselector().text("contacts"));         contact.click(); 

Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -