UiAutomator getLastTraversedText()

2024-04-23

我试图使用 Android UiAutomator 测试 Android Webview。据我了解文档 http://developer.android.com/tools/help/uiautomator/UiDevice.html#getLastTraversedText,滚动 WebvView 会生成 UI 遍历事件,这些事件应该可以通过getUiDevice().getLastTraversedText().

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

我错过了什么?

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


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

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

  • running uiautomator events in an adb 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

  • 如果我启用“触摸浏览”辅助功能设置,则我无法在以下位置使用 uiautomatoradb shell。每次运行 uiautomator 命令时,我都会得到以下响应Killed。我使用“Explore-By-Touch”来朗读显示的文本。虽然我无法在启用“Explore-By-Touch”的情况下导航到所有链接(或者当使用令人恼火的虚拟方向键(Android Accessibility Suite 的一部分)时),但它确实读出了某些链接的 LinkText(例如,它跳过了我用于测试的页面上的日语字符http://pickalize.info/ http://pickalize.info/ )

  • 如果我运行与您类似的脚本(对于我为此目的创建的简单 WebView 应用程序),那么我可以看到 WebView 中的 UI 突出显示各种元素,在我的情况下,来自以下网页的 Web 链接我出于其他原因进行了调查:时间http://pickalize.info/ 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()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

UiAutomator getLastTraversedText() 的相关文章

随机推荐