【问题标题】:Espresso testing on Webview within ViewPager在 ViewPager 中的 Webview 上进行 Espresso 测试
【发布时间】:2015-09-21 19:29:42
【问题描述】:
我有一个使用 ViewPager 的 Android 应用程序,并且视图分页器中的每个 Fragment 都有自己的 WebView。我能够编写测试用例来检查 ViewPager 上的导航。但是如何测试作为视图寻呼机的第一个孩子的 webview 中的 div 是否断言提供的字符串?
onWebView().withElement(findElement(Locator.ID, "desgination")).
check(webMatches(getText(), containsString("Doctor")));
这个扔了
android.support.test.espresso.AmbiguousViewMatcherException: 'WebView
启用 JS' 匹配层次结构中的多个视图。问题
视图在下方标有“**** MATCHES****”。
【问题讨论】:
标签:
android
webview
android-viewpager
android-espresso
【解决方案1】:
将您的代码更改为
onWebView(Matchers.allOf(isDisplayed(), isJavascriptEnabled()))
.withElement(findElement(Locator.ID, "desgination"))
.check(webMatches(getText(), containsString("Doctor")));
需要 isDisplayed() 方法来自动选择当前在屏幕上的那个。
【解决方案2】:
在我的例子中,我有几个带有嵌套 WebView 的片段。我正在使用片段管理器的添加/显示/隐藏。这意味着isDisplayed() 返回了多个匹配项。最后我为每个 webview 分配了一个唯一的 contentDescription:
webView.setContentDescription("webView[0]");
并使用 Espresso 找到特定的 webView:
onWebView(withContentDescription("webView[0]"))
.withElement(findElement(Locator.ID, "submit"))
.perform(webClick());
编辑:在阅读了更多关于 contentDescription 的内容后,很明显这是对用于可访问性的成员变量的滥用。 docs