前情提要

前些天碰到一个比较奇怪的问题,某个 H5 页面,使用 ui2.0 去抓取控件信息时,个别控件的坐标和实际显示的位置不对应:

请教 H5 页面自动转 native 控件后,控件信息与坐标不对应的问题

尝试的角度

espresso

在 github 上面搜到了一些 espresso 测试 webview 的github 例子,

参考例子,尝试解决问题,结果在点击跳转后的页面时一直出错,

测试代码:[espresso][webview] 页面跳转后如何查找控件?

后来发现例子中是同一个 webview,而我们的测试应用在页面跳转时,新起了一个 webview,

知道这个原因后,接下来就是如何区分这两个 webview?它们的控件信息完全一样。

在开发小哥哥的帮助下,给不同的 webview 设置了不同的 content-description,

测试了一下,历时几天的 webview 操作终于搞定!

robotium

使用 robotium 获取 webElements 时结果是 0,听说 robotium 可以测 web,不知道为啥不行呢

solo.clickOnText("FAQ");

List webElements = solo.getWebElements();

这里的 FAQ 是 native 控件,执行成功的,但是接下来的 getWebElements 获取到的结果是 0 个控件

espresso web 例子 @RunWith(AndroidJUnit4.class)

public class ApplicationTest {

@Rule

public ActivityTestRule mActivityRule = new ActivityTestRule(

Activity.class, false, false) {

@Override

protected void afterActivityLaunched() {

// Technically we do not need to do this - WebViewActivity has javascript turned on.

// Other WebViews in your app may have javascript turned off, however since the only way

// to automate WebViews is through javascript, it must be enabled.

onWebView().forceJavascriptEnabled();

}

};

@Test

public void useAppContext() throws Exception {

onWebView(withContentDescription("FAQ"))

.withContextualElement(findElement(Locator.XPATH, "(//*[@class='ng-binding'])[3]"))

.perform(webClick());

onWebView(withContentDescription("Question Details"))

.withContextualElement(findElement(Locator.XPATH, "(//*[@class='ng-binding'])[2]"))

.perform(webClick());

gradle 依懒 androidTestCompile 'com.android.support.test.espresso:espresso-web:3.0.1'

androidTestCompile 'com.android.support.test.espresso:espresso-core:3.0.1'

androidTestCompile 'com.jayway.android.robotium:robotium-solo:5.6.3'

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐