【问题标题】:UiAutomator getLastTraversedText()UiAutomator getLastTraversedText()
【发布时间】:2013-02-27 11:31:06
【问题描述】:

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

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

我错过了什么?

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

【问题讨论】:

    标签: android android-webview ui-automation android-uiautomator


    【解决方案1】:

    坏消息:我已经花了几个小时试图弄清楚如何让它工作,但是在响应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

    • 如果我启用“通过触摸浏览”辅助功能设置,则无法在 adb shell 中使用 uiautomator。每次我运行 uiautomator 命令时,我都会收到 Killed 的响应。我正在使用 Explore-By-Touch 来朗读显示的文本。虽然我无法导航到启用 Explore-By-Touch 的所有链接(或者当使用 Android Accessibility Suite 中令人生气的 Virtual D-Pad 时),但它确实读出了一些链接的 LinkText(例如,它跳过了我用来测试 http://pickalize.info/ 的页面上的日文字符)

    • 1234563当时出于其他原因正在调查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 日):我在设备的辅助功能设置菜单中将“增强网络辅助功能”设置为“允许”再次进行了测试。 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()
    

    【讨论】:

    • 感谢您的回复!我也没有比这更进一步。我发现 TalkBack 能够读取 WebView 的内容尤其令人困惑。我还将在此处添加任何其他发现。
    • 作为一个想法,也许我们需要安装 Talkback 用来增强其从 Web 浏览器读取内容的能力的 Web 脚本?
    • 记录下来,Android 4.3 没有任何改进。
    • 从 Android 4.4 开始,可以使用标准的 UiAutomator 方法解析 Web 视图。耶。
    • @Micha 对于 Android 5.0,我的 webview 上的 getLastTraversedText() 仍然返回(null)。它对你有用吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多