其他测试框架 [espresso][webview] 基于 espresso web 依赖执行 android h5 页面的自动化操作

bauul · 2017年12月07日 · 最后由 bauul 回复于 2017年12月08日 · 1413 次阅读

前情提要

前些天碰到一个比较奇怪的问题,某个 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<WebElement> webElements = solo.getWebElements();

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

espresso web 例子

@RunWith(AndroidJUnit4.class)
public class ApplicationTest {

    @Rule
    public ActivityTestRule<Activity> mActivityRule = new ActivityTestRule<Activity>(
            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'
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 4 条回复 时间 点赞

用 appium 应该也可以吧,有了不同的 context 应该可以识别出来

是的,可以识别出来,只是某个特殊场景,控件信息和实际显示不对应:https://testerhome.com/topics/10937
为了解决这个问题,同时也学习下 espresso 的使用

bauul 回复

感觉 espresso 使用体验如何,相比其他框架

我还没有大批量使用,仅在 webview 页面加入了 espresso 的接口到 appium 源码中 ui2.0 server 中去,
不过是听说 google 内部大部分的 app 自动化都是用的 espresso
另外需要和被测应用同样的签名才可以。

需要 登录 後方可回應,如果你還沒有帳號按這裡 注册