8

我正在尝试使用 Android UiAutomator 测试 Android Webview。据我了解文档,滚动浏览 WebvView 会生成 UI 遍历事件,这些事件应该可以通过getUiDevice().getLastTraversedText().

但是,当我使用getUiDevice().pressDPadDown()滚动浏览网页视图时,getUiDevice().getLastTraversedText()一直返回 null。

我错过了什么?

如果有人接到这个电话,我将非常感谢一个简短的代码示例。

4

1 回答 1

4

坏消息:我已经花了几个小时试图弄清楚如何让它工作,但是除了 null 之外,我还没有得到任何东西来响应对getUiDevice().getLastTraversedText().

仅供参考,这是我尝试并发现的事情:

  • 运行应该转储所有辅助功能事件uiautomator eventsadb shell它肯定会报告各种事件,但是当我手动滚动浏览 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

  • 如果我启用按触摸浏览可访问性设置,则我无法在adb shell. 每次我运行 uiautomator 命令时,我都会收到Killed. 我正在使用 Explore-By-Touch 来朗读显示的文本。虽然我无法导航到启用 Explore-By-Touch 的所有链接(或者当使用作为 Android Accessibility Suite 一部分的令人愤怒的 Virtual D-Pad 时)它确实读出了一些链接的 LinkText(例如,它跳过了我用来测试http://pickalize.info/的页面上的日文字符)

  • 如果我运行类似于您的脚本(对于我为此目的创建的简单 WebView 应用程序),那么我可以看到 WebView 中的 UI 突出显示了各种元素,在我的情况下,来自以下网页的 Web 链接我正在调查其他原因在时间http://pickalize.info/当前所选链接的背景变为淡蓝色,但是调用没有返回文本getUiDevice().getLastTraversedText()

我最好的猜测是我们要么不恰当地尝试使用该方法。也许我们应该请求并解析 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 日):我在设备的辅助功能设置菜单中将“增强网络辅助功能”设置为“允许”再次进行了测试。我仍然得到 null 返回getLastTraversedText()

这是我的测试代码的内容:

公共类 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()
于 2013-03-10T19:57:39.630 回答