gpt4 book ai didi

android - UiAutomator getLastTraversedText() 方法

转载 作者:塔克拉玛干 更新时间:2023-11-02 18:56:37 26 4
gpt4 key购买 nike

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

读取

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

我错过了什么?

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

最佳答案

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

仅供引用,这是我尝试和发现的东西:

  • adb shell 中运行 uiautomator 事件 应该转储所有辅助功能事件。它当然会报告各种事件,但是当我手动滚动浏览 WebView 的内容时它实际上是无声的。如果 WebView 中的内容滚动,例如向上或向下导航超出屏幕上显示的内容后,我收到以下类型的事件报告:

03-10 19:44:47.436 事件类型:TYPE_VIEW_SCROLLED;事件时间:911700;包名:com.example.simplewebview;运动粒度:0; Action :0 [类名:android.webkit.WebView;文本: [];内容描述:空;元素数量:-1;当前项目索引:-1;启用:真; IsPassword: false;已检查:假;是否全屏:假;可滚动:真;前文:空;从索引:-1; ToIndex:-1;滚动X:0;滚动Y:270;最大滚动X:0;最大滚动Y:544;添加计数:-1;移除次数:-1;包裹数据:空];记录数:0

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

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

  • 时未返回任何文本

我最好的猜测是我们要么试图不恰本地使用该方法。也许我们应该请求并解析 WebView 的 DOM(天知道怎么做),但请参阅 getLastTraversedText() 文档中的以下注释

当使用的 View 控件可以返回一个
引用的是Document Object Model,建议再用view的
取而代之的是 DOM。

顺便说一句:我正在物理设备上使用 Android 4.2.2 进行测试。

祝您调查顺利。我会不时地查看问题,以帮助我更多地了解 Ui Automator 的适用性和功能。

更新(2013 年 3 月 26 日):我再次测试了设备的辅助功能设置菜单中的“增强网络辅助功能”设置为“允许”。我仍然得到 getLastTraversedText()

返回的 null

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

公共(public)类 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()

关于android - UiAutomator getLastTraversedText() 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15111001/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com