在TabLayout下执行对TabText的单击始终提供NoMatchingViewException [英] Perform click to tabtext under TabLayout always gives NoMatchingViewException

查看:76
本文介绍了在TabLayout下执行对TabText的单击始终提供NoMatchingViewException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用espresso点击我的一个标签页

I would like to perform click on one of my tabs using espresso,

<com.google.android.material.tabs.TabLayout
        android:id="@+id/tabs"
        ...>
        <com.google.android.material.tabs.TabItem
            android:id="@+id/tabItem"
            android:text="Tab1" />

        <com.google.android.material.tabs.TabItem
            android:id="@+id/tabItem2"
            android:text="Tab2" />
    </com.google.android.material.tabs.TabLayout>

我尝试断言isDisplayed()是否是

I tried asserting if it isDisplayed(), and it is

onView(allOf(withId(R.id.tabs), withText("Tab2"), isDescendantOfA(withId(R.id.container)), isDisplayed()));

但是当我尝试单击文本时,它导致NoMatchingViewException

But when I try to perform click on the text, it results to NoMatchingViewException

onView(allOf(withId(R.id.tabs), withText("Tab2"), isDescendantOfA(withId(R.id.container)), isDisplayed())).perform(click());

我尝试将其放在不同的行中,

I tried putting it in different lines,

1. check if displayed
2. perform click

我们如何使用espresso成功单击viewPager下的tabtext?

How can we successfully click tabtext under viewPager using espresso?

尝试使用标签ID,但结果仍然相同. 使用viewTag的建议不起作用

Tried using the tab IDs, but still same result. Suggestions to use viewTags does not work

推荐答案

尝试一下:

onView(allOf(withText("Tab2"), isDescendantOfA(withId(R.id.tabs)))).perform(click());

因为带有文本withText("Tab2")的视图没有id withId(R.id.tabs)(因为其父级是具有该id的视图),所以这就是为什么要获得NoMatchingViewException

As the view with the text withText("Tab2") doesn't have the id withId(R.id.tabs) (because its parent is the one with that id) so that's why you get the NoMatchingViewException

这篇关于在TabLayout下执行对TabText的单击始终提供NoMatchingViewException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆