Android UiWatcher不工作(甚至未调用)

Android UiWatcher不工作(甚至未调用),android,ui-automation,android-testing,android-uiautomator,Android,Ui Automation,Android Testing,Android Uiautomator,我无法让UiWatcher在我的代码中工作,它似乎永远不会被调用,即使UiSelector与任何内容都不匹配 public void runWirelessWatcher() throws UiObjectNotFoundException { String NONETWORKWATCHER_STRING = "CancelWirelessWatcher"; UiWatcher CancelWirelessWatcher= new UiWatcher() {

我无法让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();

此处,当“主页”按钮出现无线未连接错误时,Uiwatcher无法单击主页按钮。(Uiwatcher甚至未被调用)

添加
runWirelessWatcher()到您的代码

  runWirelessWatcher();
  getUiDevice().pressHome();
  UiObject allAppsButton = new UiObject(new   UiSelector().description("Apps"));
  allAppsButton.clickAndWaitForNewWindow();
  UiObject playstoretab = new UiObject(new UiSelector().description("PlayStore"));
  playstoretab.clickAndWaitForNewWindow();
除非你已经在做这件事,而且你没有表现出来

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

和您的代码:

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

你确定没有人叫它吗?您是否尝试过输入日志以查看该对话框是否存在?我已经给出了一条要打印的消息(在“字符串非网络…”之前)以确保它正在被调用。但是,即使消息没有打印…也意味着…它没有被调用。另外,如果您希望监视者是代码中的最后一个对象,它也不会工作。监视程序的工作方式是,它们只在测试运行时运行。测试不会“等待”观察程序执行。如果在观察程序中返回false,则添加行
Log.d(“观察程序”,“观察程序正在运行”)。这样我们就可以真正看到。在您的代码中,您没有调用
runWirelessWatcher()方法工作正常,我添加了runWirelessWatcher();UiDevice.getInstance.runWatchers();在您提到的代码中。一旦无线覆盖出现,就会调用watcher并按home键。
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