gpt4 book ai didi

android-uiautomator - Android UIAutomator 的 UiWatcher 示例不起作用

转载 作者:行者123 更新时间:2023-12-04 04:48:24 27 4
gpt4 key购买 nike

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

}
}

最佳答案

好的,现在我明白了 UiWatcher 的工作原理......
注意:仅当某些 API 处于重试模式时,UiWatcher 才会调用。这意味着当某些 API 调用无法从 UI 中找到元素时,UI 库将自动调用您注册的观察者。

所以根据上面的例子,当互联网连接关闭时打开 Google Play .. 现在我希望观察者活着并检查上述情况,然后单击重试并返回主页。只需在代码末尾添加这两行..这将处于重试模式,然后您可以看到注册的观察者开始工作。

// below line will be in retry mode and Watcher will be invoke automatically //
UiObject contact = new UiObject(new UiSelector().text("Contacts"));
contact.click();

关于android-uiautomator - Android UIAutomator 的 UiWatcher 示例不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17817821/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com