سؤال

I could not get the UiWatcher to work in my code, it does not ever seem to be called even when the UiSelector does not match anything.

public void runWirelessWatcher() throws UiObjectNotFoundException { 


        String NONETWORKWATCHER_STRING = "CancelWirelessWatcher";
        UiWatcher CancelWirelessWatcher= new UiWatcher() {
            @Override
            public boolean checkForCondition() {
                UiObject okDialog = new UiObject(new UiSelector().text("No Network"));
                if(okDialog.exists()){

                    UiObject okButton = new UiObject(new UiSelector().text("Home"));
                    try {
                        okButton.click();
                    } catch (UiObjectNotFoundException e) {
                        e.printStackTrace();
                    }
                    return (okDialog.waitUntilGone(25000));
                }
                return false;
            }
        };
        UiDevice.getInstance().registerWatcher(NONETWORKWATCHER_STRING, CancelWirelessWatcher);
        UiDevice.getInstance().runWatchers();
    }

Here is my code:

  getUiDevice().pressHome();
  UiObject allAppsButton = new UiObject(new   UiSelector().description("Apps"));
  allAppsButton.clickAndWaitForNewWindow();
  UiObject playstoretab = new UiObject(new UiSelector().description("PlayStore"));
  playstoretab.clickAndWaitForNewWindow();

Here when the wireless not connected error appears with "Home" button, Uiwatcher fails to click the home button.(Uiwatcher not even called)

هل كانت مفيدة؟

المحلول

Add runWirelessWatcher(); to your code.

  runWirelessWatcher();
  getUiDevice().pressHome();
  UiObject allAppsButton = new UiObject(new   UiSelector().description("Apps"));
  allAppsButton.clickAndWaitForNewWindow();
  UiObject playstoretab = new UiObject(new UiSelector().description("PlayStore"));
  playstoretab.clickAndWaitForNewWindow();

Unless you are already doing this and you aren't showing it.

Edit

Try this: it will help debug: (Just copy/paste)

 public void runWirelessWatcher() throws UiObjectNotFoundException { 


    String NONETWORKWATCHER_STRING = "CancelWirelessWatcher";
    UiWatcher CancelWirelessWatcher= new UiWatcher() {
        @Override
        public boolean checkForCondition() {
            UiObject okDialog = new UiObject(new UiSelector().text("No Network"));
            if(okDialog.exists()){

                UiObject okButton = new UiObject(new UiSelector().text("Home"));
                try {
                    okButton.click();
                } catch (UiObjectNotFoundException e) {
                    e.printStackTrace();
                }
                Log.d("Watcher", "Found okButton!");
                return (true);
            }
            Log.d("watcher", "Watcher is running");
            return false;
        }
    };
    UiDevice.getInstance().registerWatcher(NONETWORKWATCHER_STRING, CancelWirelessWatcher);

}

And your code:

runWirelessWatcher();
UiDevice.getInstance.runWatchers();
getUiDevice().pressHome();
UiObject allAppsButton = new UiObject(new   UiSelector().description("Apps"));
allAppsButton.clickAndWaitForNewWindow();
UiObject playstoretab = new UiObject(new UiSelector().description("PlayStore"));
playstoretab.clickAndWaitForNewWindow();
//this is where your watcher should be called? 
//Do something here to keep test running
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top