UiWatcher不工作(甚至不叫)

时间:2014-01-28 08:20:33

标签: android ui-automation android-testing uiautomator

我无法让UiWatcher在我的代码中工作,即使UiSelector与任何内容都不匹配,它似乎也不会被调用。

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();
    }

这是我的代码:

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

此处当“Home”按钮出现无线未连接错误时,Uiwatcher无法单击主页按钮。(Uiwatcher甚至没有打电话)

1 个答案:

答案 0 :(得分:0)

runWirelessWatcher();添加到您的代码中。

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

除非你已经这样做而且你没有表现出来。

修改

试试这个:它有助于调试:(只需复制/粘贴)

 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);

}

和您的代码:

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