如何使用android espresso执行单击选项卡

如何使用android espresso执行单击选项卡,android,unit-testing,junit,android-espresso,Android,Unit Testing,Junit,Android Espresso,我试图用浓缩咖啡点击标签,但总是出错 我有如下布局,并尝试单击tab2: <TabHost android:id="@+id/tabHost" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentTop="true" android:layout_centerHorizontal="true">

我试图用浓缩咖啡点击标签,但总是出错

我有如下布局,并尝试单击tab2:

 <TabHost
    android:id="@+id/tabHost"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"></TabWidget>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:id="@+id/tab1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <ScrollView
                    android:id="@+id/scroll_tab1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <TextView
                        android:id="@+id/object_response"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:padding="10dp"
                        android:textIsSelectable="true"
                        android:textSize="16sp">
                    </TextView>
                </ScrollView>
            </LinearLayout>


            <LinearLayout
                android:id="@+id/tab2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <ScrollView
                    android:id="@+id/scroll_tab2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <TextView
                        android:id="@+id/json_response"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:padding="10dp"
                        android:textIsSelectable="true"
                        android:textSize="16sp">
                    </TextView>
                </ScrollView>
            </LinearLayout>


        </FrameLayout>
    </LinearLayout>
</TabHost>
我已经尝试按照此链接中的建议进行操作:,但仍然在使用id和标记值时出错

使用ID的浓缩咖啡代码:

onView(withId(R.id.tab2)).perform(click());
收到错误:

android.support.test.espresso.PerformException:在id为的视图上执行“单击”时出错: com.greenwavesystems.axon.lib.testapp:id/tab2'

android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1959) 原因:java.lang.RuntimeException:将不执行操作 因为目标视图与以下一项或多项不匹配 约束:至少90%的视图区域显示给用户 用户。目标视图:“LinearLayout{id=2131558557,res name=tab2, 可见性=消失,宽度=0,高度=0,焦点=假, has focusable=false,has window focus=true,is clickable=false, 启用=真,聚焦=假,聚焦=假, 请求的布局是否为真,选择的布局是否为假, 根是布局请求=false,输入连接=false,x=0.0, y=0.0,子计数=1}“在 android.support.test.espresso.ViewInteraction$1.run(ViewInteraction.java:138) 在 Executors$RunnableAdapter.call(Executors.java:423) 在java.util.concurrent.FutureTask.run(FutureTask.java:237)处 handleCallback(Handler.java:739)位于 dispatchMessage(Handler.java:95)位于 Looper.loop(Looper.java:158)位于 main(ActivityThread.java:7229)位于 java.lang.reflect.Method.invoke(本机方法)位于 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

使用标记值的浓缩咖啡代码:

onView(withTagValue(is((Object)"Json"))).perform(click());
收到错误:

android.support.test.espresso.NoMatchingViewException:中没有视图 找到匹配的层次结构:标记值为“Json”

我也尝试过这个建议,但还是没有成功

下面是我的代码:

 public static Matcher<View> TabMatcher(final Matcher<View> parentMatcher, final int index) {
        return new TypeSafeMatcher<View>() {
            @Override
            protected boolean matchesSafely(View view) {
                if (!(view.getParent() instanceof TabHost)) {
                    return parentMatcher.matches(view.getParent());
                }
                TabHost tabHost = (TabHost) view.getParent();
                return parentMatcher.matches(view.getParent())
                        && tabHost.getTabContentView().getChildCount() > index &&
                        tabHost.getTabContentView().getChildAt(index) != null;
            }

            @Override
            public void describeTo(Description description) {

            }
        };
    }

onView(TabMatcher(withId(R.id.tabHost), 1)).perform(click());
公共静态匹配器TabMatcher(final Matcher parentMatcher,final int index){
返回新的TypeSafeMatcher(){
@凌驾
受保护的布尔匹配安全(视图){
if(!(view.getParent()instanceof TabHost)){
返回parentMatcher.matches(view.getParent());
}
TabHost TabHost=(TabHost)view.getParent();
返回parentMatcher.matches(view.getParent())
&&tabHost.getTabContentView().getChildCount()>索引&&
tabHost.getTabContentView().getChildAt(索引)!=null;
}
@凌驾
公共无效说明(说明){
}
};
}
onView(TabMatcher(withId(R.id.tabHost),1)).perform(click());
未收到任何错误,但tab2仍然未执行单击


请告知我缺少的部分,并感谢您的帮助。

当您运行测试时,您是否在屏幕上看到您的tab2?您好,是的,我在屏幕上看到我的tab2,但在执行单击时它不会打开(显示)。似乎此解决方案可能会有所帮助。运行测试时,是否在屏幕上看到tab2?您好,是的,我在屏幕上看到我的tab2,但当我执行单击时,它不会打开(显示)。似乎此解决方案可能会有所帮助。
 public static Matcher<View> TabMatcher(final Matcher<View> parentMatcher, final int index) {
        return new TypeSafeMatcher<View>() {
            @Override
            protected boolean matchesSafely(View view) {
                if (!(view.getParent() instanceof TabHost)) {
                    return parentMatcher.matches(view.getParent());
                }
                TabHost tabHost = (TabHost) view.getParent();
                return parentMatcher.matches(view.getParent())
                        && tabHost.getTabContentView().getChildCount() > index &&
                        tabHost.getTabContentView().getChildAt(index) != null;
            }

            @Override
            public void describeTo(Description description) {

            }
        };
    }

onView(TabMatcher(withId(R.id.tabHost), 1)).perform(click());