坏消息:我已经花了几个小时试图弄清楚如何让它工作,但是在响应getUiDevice().getLastTraversedText() 的调用时,除了 null 之外,我还没有得到任何东西。
仅供参考,以下是我尝试并发现的东西:
- 在
adb shell 中运行uiautomator events 应该转储所有辅助功能事件。它肯定会报告各种事件,但是当我手动滚动浏览 WebView 的内容时它几乎是无声的。如果 WebView 中的内容被滚动,例如在屏幕上显示的内容向上或向下导航后,我收到以下类型的事件报告:
03-10 19:44:47.436 EventType: TYPE_VIEW_SCROLLED; EventTime: 911700; PackageName: com.example.simplewebview; MovementGranularity: 0; Action: 0 [ ClassName: android.webkit.WebView; Text: []; ContentDescription: null; ItemCount: -1; CurrentItemIndex: -1; IsEnabled: true; IsPassword: false; IsChecked: false; IsFullScreen: false; Scrollable: true; BeforeText: null; FromIndex: -1; ToIndex: -1; ScrollX: 0; ScrollY: 270; MaxScrollX: 0; MaxScrollY: 544; AddedCount: -1; RemovedCount: -1; ParcelableData: null ]; recordCount: 0
我最好的猜测是我们要么不恰当地尝试使用该方法。也许我们应该请求并解析 WebView 的 DOM(天知道怎么做),但是请参阅 getLastTraversedText() 文档中的以下注释
When the view control used can return a
reference to is Document Object Model, it is recommended then to use the view's
DOM instead.
顺便说一句:我正在物理设备上使用 Android 4.2.2 进行测试。
祝您调查顺利。我会不时查看问题,以帮助我了解更多有关 Ui Automator 的适用性和功能的信息。
更新(2013 年 3 月 26 日):我在设备的辅助功能设置菜单中将“增强网络辅助功能”设置为“允许”再次进行了测试。 getLastTraversedText() 仍然返回 null
这是我的测试代码的内容:
公共类 WebViewNavigator 扩展 UiAutomatorTestCase {
public void testNavigationDownGeneratesEventText() throws UiObjectNotFoundException {
getUiDevice().pressHome();
UiObject allAppsButton = new UiObject(new UiSelector().description("Apps"));
allAppsButton.clickAndWaitForNewWindow();
UiObject appsTab = new UiObject(new UiSelector().text("Apps"));
appsTab.click();
UiScrollable appViews = new UiScrollable(new UiSelector().scrollable(true));
appViews.setAsHorizontalList();
String nameOfAppToLaunch = "SimpleWebView";
UiAutomatorHelpers.launchAppCalled(nameOfAppToLaunch);
UiObject simpleWebViewValidation = new UiObject(new UiSelector().packageName("com.example.simplewebview"));
assertTrue("The package name seems incorrect for the app we want to launch", simpleWebViewValidation.exists());
simpleWebViewValidation.clickAndWaitForNewWindow();
// Ok we should have started the app now... On to the test
Log.i("WebViewNavigatorTest", "About to start navigating down the contents of the webview");
int closeToInfinity = 10;
for (int i = 0; i < closeToInfinity; i++) {
boolean goneDown = getUiDevice().pressDPadDown();
if (!goneDown) {
break;
}
String lastTraversedText = getUiDevice().getLastTraversedText();
if (lastTraversedText != null) {
Log.i("WebViewNavigatorTest", lastTraversedText);
} else {
Log.w("WebViewNavigatorTest", "(null) returned for getLastTraversedText()");
}
}
}
}
下面是我在 Android 设备上执行此代码时用来查看消息的 adb 命令:
adb logcat WebViewNavigatorTest:I *:S
接着是输出:
I/WebViewNavigatorTest(29358): About to start navigating down the contents of the webview
W/WebViewNavigatorTest(29358): (null) returned for getLastTraversedText()
W/WebViewNavigatorTest(29358): (null) returned for getLastTraversedText()
W/WebViewNavigatorTest(29358): (null) returned for getLastTraversedText()
W/WebViewNavigatorTest(29358): (null) returned for getLastTraversedText()
W/WebViewNavigatorTest(29358): (null) returned for getLastTraversedText()
W/WebViewNavigatorTest(29358): (null) returned for getLastTraversedText()
W/WebViewNavigatorTest(29358): (null) returned for getLastTraversedText()
W/WebViewNavigatorTest(29358): (null) returned for getLastTraversedText()
W/WebViewNavigatorTest(29358): (null) returned for getLastTraversedText()
W/WebViewNavigatorTest(29358): (null) returned for getLastTraversedText()