UiAutomator getLastTraversedText() [英] UiAutomator getLastTraversedText()

查看:999
本文介绍了UiAutomator getLastTraversedText()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Android UiAutomator来测试一个Android网页视图。据我了解的<一个href=\"http://developer.android.com/tools/help/uiautomator/UiDevice.html#getLastTraversedText\">documentation,通过WebvView滚动会产生UI遍历的事件,而那些应通过 getUiDevice()是可读的。getLastTraversedText()

然而,当我使用 getUiDevice()。pressDPadDown()通过Web视图滚动, getUiDevice()。getLastTraversedText( )一直返回null。

什么我错过了?

如果有人已经得到了这一呼吁的工作我非常AP preciate短code样本。


解决方案

坏消息:我花了几个小时试图捉摸如何得到这个工作,但是我还没有得到null以外的任何响应在调用 getUiDevice()。getLastTraversedText()

仅供参考下面是我试过,发现了事情:


  • 运行 uiautomator事件亚行外壳应该转储所有的辅助活动。这肯定会将各种事件报告,但它几乎沉默的时候我手动滚动显示的WebView的内容。如果在web视图内容例如滚动导航向上或向下超出的显示在屏幕上后,我得到事件的类型如下报道:

03-10 19:44:47.436事件类型:TYPE_VIEW_SCROLLED; EVENTTIME:911700; PACKAGENAME:com.example.simplewebview; MovementGranularity:0;操作:0产品类别:android.webkit.WebView;文字:[]; ContentDescription:空; ItemCount中:-1; CurrentItemIndex:-1; IsEnabled:真实的; IsPassword:假的;器isChecked:假的; IsFullScreen:假的;滚动:真实的; BeforeText:空;的fromIndex:-1;元素范围:-1; ScrollX:0; ScrollY:270; MaxScrollX:0; MaxScrollY:544; AddedCount:-1; RemovedCount:-1; ParcelableData:空];总记录:0


  • 如果我让探索,通过触摸可访问性设置然后,我不能在亚行外壳使用uiautomator 。每次我运行一个命令uiautomator,我得到的响应终止。我用的是探索,通过触发言时显示的文字。虽然我没能浏览到所有的链接浏览,通过触启用(或使用真气虚拟方向键是Android辅助套件的一部分时),它没有读出连结文字的某些环节(例如,它跳过我使用 http://pickalize.info/ 的测试页)


  • 如果我运行类似你这样的脚本(我为目的创建了一个简单的WebView应用程序),那么我可以看到的WebView用户界面突出的各种元素,从下面的网页我我的情况下,网页链接正在调查在当时 http://pickalize.info/等原因当前选择链接的背景变为淡蓝色,但没有文字是由调用返回的 getUiDevice()。getLastTraversedText()


我最好的猜测是,我们要么试图不恰当地使用的方法。或许我们应该请求和解析的WebView的DOM(天知道怎样),但看到getLastTraversedText的文档如下评论()

当使用的视图控件可以返回
参照是文档对象模型,建议然后使用视图的
DOM来代替。

BTW:我的Andr​​oid 4.2.2上的物理设备测试

祝您的调查。我会继续看问题不时来帮助我了解更多有关用户界面的Automator的恰当性和能力。

更新(2013年3月26日):我再次测试增强网页辅助功能设置为设备的辅助功能设置菜单中的允许。我仍然可以通过空 getLastTraversedText返回()

下面是我的测试code的胆量:

公共类WebViewNavigator扩展UiAutomatorTestCase {

 公共无效testNavigationDownGeneratesEventText()抛出UiObjectNotFoundException {
    getUiDevice()pressHome()。
    UIObject类allAppsButton =新UIObject类(新UiSelector()描述(应用程序)。);
    allAppsButton.clickAndWaitForNewWindow();    UIObject类appsTab =新UIObject类(新UiSelector()文本(应用程序));
    appsTab.click();    UiScrollable浏览的应用数量=新UiScrollable(新UiSelector()滚动(真));
    appViews.setAsHorizo​​ntalList();    字符串nameOfAppToLaunch =SimpleWebView;
    UiAutomatorHelpers.launchAppCalled(nameOfAppToLaunch);    UIObject类simpleWebViewValidation =新UIObject类(新UiSelector()的packageName(com.example.simplewebview)。);
    assertTrue(包名称似乎不正确的,我们要启动的应用程序,simpleWebViewValidation.exists());
    simpleWebViewValidation.clickAndWaitForNewWindow();    // OK,我们应该现在已经开始应用......在对测试    Log.i(WebViewNavigatorTest,关于开始导航下来的WebView内容);
    INT closeToInfinity = 10;
    的for(int i = 0; I&LT; closeToInfinity;我++){
        布尔goneDown = getUiDevice()pressDPadDown()。
        如果(!goneDown){
            打破;
        }        字符串lastTraversedText = getUiDevice()getLastTraversedText()。
        如果(lastTraversedText!= NULL){
            Log.i(WebViewNavigatorTest,lastTraversedText);
        }其他{
            Log.w(WebViewNavigatorTest,(空)返回getLastTraversedText());
        }
    }
}

}

和这里的ADB命令我使用的时候在Android设备上执行该code看到的消息:
亚行logcat WebViewNavigatorTest:我*:•

其次是输出:

  I / WebViewNavigatorTest(29358):关于开始导航下来的WebView内容
W / WebViewNavigatorTest(29358):(空)返回getLastTraversedText()
W / WebViewNavigatorTest(29358):(空)返回getLastTraversedText()
W / WebViewNavigatorTest(29358):(空)返回getLastTraversedText()
W / WebViewNavigatorTest(29358):(空)返回getLastTraversedText()
W / WebViewNavigatorTest(29358):(空)返回getLastTraversedText()
W / WebViewNavigatorTest(29358):(空)返回getLastTraversedText()
W / WebViewNavigatorTest(29358):(空)返回getLastTraversedText()
W / WebViewNavigatorTest(29358):(空)返回getLastTraversedText()
W / WebViewNavigatorTest(29358):(空)返回getLastTraversedText()
W / WebViewNavigatorTest(29358):(空)返回getLastTraversedText()

I was trying to test an Android Webview using Android UiAutomator. As I understand the documentation, scrolling through a WebvView would generate UI traversal events, and those should be readable via getUiDevice().getLastTraversedText().

However, when I use getUiDevice().pressDPadDown() to scroll through a web view, getUiDevice().getLastTraversedText() keeps returning null.

What have I missed?

If anyone has gotten this call to work I would very much appreciate a short code sample.

解决方案

Bad news: I've spent several hours trying to fathom out how to get this working, however I've yet to get anything other than null in response to the calls to getUiDevice().getLastTraversedText().

FYI here are the things I've tried and discovered:

  • running uiautomator events in an adb shell should dump all the Accessibility Events. It certainly reports various events, however it's virtually silent when I manually scroll through the contents of a WebView. If the content in the WebView is scrolled e.g. after navigating up or down beyond what's displayed on screen I get the following type of event reported:

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

  • If I enable the Explore-by-touch accessibility setting then I cannot use uiautomator in adb shell. Each time I run a uiautomator command, I get a response of Killed. I was using Explore-By-Touch to speak the text that's displayed. While I wasn't able to navigate to all the links with Explore-By-Touch enabled (or when using the infuriating Virtual D-Pad which is part of the Android Accessibility Suite) it did read out the LinkText for some of the links (e.g. it skipped the Japanese characters on the page I was using for testing of http://pickalize.info/ )

  • If I run a script similar to yours (for a simple WebView app I created for the purpose) then I can see the UI in the WebView highlight various elements, in my case web links from the following web page I was investigating for other reasons at the time http://pickalize.info/ The background of the currently selected link goes to pale blue, however no text is returned by the call to getUiDevice().getLastTraversedText()

My best guess is that we're either trying to use the method inappropriately. Perhaps we're supposed to request and parse the DOM of the WebView (goodness knows how), but see the following comment from the documentation of 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.

BTW: I'm testing with Android 4.2.2 on physical devices.

Good luck with your investigations. I'll keep looking at the problem from time to time to help me learn more about the appropriateness and capabilities of Ui Automator.

Update (26 March 2013): I tested again with 'Enhance web accessibility' set to 'Allowed' in the device's Accessibility Setting menu. I still get null returned by getLastTraversedText()

Here's the guts of my test code:

public class WebViewNavigator extends 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()");
        }
    }   
}

}

And here's the adb command I use to see the messages when this code is executed on the Android device: adb logcat WebViewNavigatorTest:I *:S

Followed by the output:

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

这篇关于UiAutomator getLastTraversedText()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆