问答 [espresso][webview] 页面跳转后如何查找控件?

bauul · 2017年12月06日 · 最后由 lovexiaov 回复于 2018年03月15日 · 1504 次阅读

问题

我用 espresso 测试 webview 的发生了异常,在 web 页面 a,执行了点击,页面跳转到 web 页面 b 后,再尝试点击就失败了,大家有遇到吗?

测试代码

@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 webviewTest() throws Exception {

    Thread.sleep(60000);
    long start = System.currentTimeMillis();
    mActivityRule.getActivity();
    onIdle();

    long end = System.currentTimeMillis();
    System.out.println("wait for idle:" + (end - start));

    //点击web页面a上的菜单
    onWebView()
            .withElement(findElement(Locator.XPATH, "(//*[@class='ng-binding'])[3]"))
            .perform(webClick());

    onIdle();

    //点击web页面b上的菜单
    onWebView()
            .withElement(findElement(Locator.XPATH, "(//*[@class='ng-binding'])[3]"))
            .perform(webClick());

日志

I/TestRunner: ----- begin exception -----
I/TestRunner: android.support.test.espresso.AmbiguousViewMatcherException: 'WebView with JS enabled' matches multiple views in the hierarchy.
              Problem views are marked with '****MATCHES****' below.

              View Hierarchy:
              +>DecorView{id=-1, visibility=VISIBLE, width=1080, height=1920, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=WM.LayoutParams{(0,0)(fillxfill) sim=#10 ty=1 fl=#81810100 wanim=0x1030465 needsMenuKey=2 naviIconColor=0}, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1} 
              |
              +->LinearLayout{id=-1, visibility=VISIBLE, width=1080, height=1920, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.widget.FrameLayout$LayoutParams@a6f675c, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2} 
              |
              +-->ViewStub{id=16909425, res-name=action_mode_bar_stub, visibility=GONE, width=0, height=0, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=true, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@153a65, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0} 
              |
              +-->FrameLayout{id=-1, visibility=VISIBLE, width=1080, height=1920, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@f9bcbeb, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1} 
              |
              +--->FitWindowsLinearLayout{id=2131690172, res-name=action_bar_root, visibility=VISIBLE, width=1080, height=1920, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.widget.FrameLayout$LayoutParams@baae906, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2} 
              |
              +---->ViewStubCompat{id=2131690173, res-name=action_mode_bar_stub, visibility=GONE, width=0, height=0, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=true, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@b693bc7, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0} 
              |
              +---->ContentFrameLayout{id=16908290, res-name=content, visibility=VISIBLE, width=1080, height=1920, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@1e9511d, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1} 
              |
              +----->LinearLayoutForStatusBar{id=2131690225, res-name=main_content, visibility=VISIBLE, width=1080, height=1920, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.widget.FrameLayout$LayoutParams@3b4cd60, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2} 
              |
              +------>View{id=-1, visibility=VISIBLE, width=1080, height=72, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@2cee219, tag=null, root-is-layout-r
I/TestRunner: ----- end exception -----
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 4 条回复 时间 点赞
bauul 回复

看提示是 在 b 页面匹配到了多个控件啊~

lovexiaov 回复

嗯,是两个 webview,但是控件信息都一样,所以没法区分,让开发加了属性后,解决了

bauul 回复

👏 👏 👏 论有一个容易沟通的开发的重要性

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册