我尝试使用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">
<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>
这是我在活动类中的代码:
TabHost host = (TabHost) findViewById(R.id.tabHost);
host.setup();
//Tab 1
TabHost.TabSpec spec = host.newTabSpec(getString(R.string.object));
spec.setContent(R.id.tab1);
spec.setIndicator(getString(R.string.object));
host.addTab(spec);
//Tab 2
spec = host.newTabSpec(getString(R.string.json));
spec.setContent(R.id.tab2);
spec.setIndicator(getString(R.string.json));
host.addTab(spec);
我尝试按照此链接中的建议:How open tab in espresso,但仍然使用id和tag值获取错误。
使用ID的Espresso代码:
onView(withId(R.id.tab2)).perform(click());
收到错误:
android.support.test.espresso.PerformException:执行单击&#39;时出错在视图&#39;与id: com.greenwavesystems.axon.lib.testapp:ID / TAB2&#39;
android.app.Instrumentation $ InstrumentationThread.run(Instrumentation.java:1959) 引起:java.lang.RuntimeException:不会执行操作 因为目标视图与以下一个或多个不匹配 约束:显示至少90%的视图区域 用户。目标视图:&#34; LinearLayout {id = 2131558557,res-name = tab2, 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, root-is-layout-requested = false,has-input-connection = false,x = 0.0, y = 0.0,child-count = 1}&#34;在 android.support.test.espresso.ViewInteraction $ 1.run(ViewInteraction.java:138) 在 java.util.concurrent.Executors $ RunnableAdapter.call(Executors.java:423) 在java.util.concurrent.FutureTask.run(FutureTask.java:237)at android.os.Handler.handleCallback(Handler.java:739)at android.os.Handler.dispatchMessage(Handler.java:95)at android.os.Looper.loop(Looper.java:158)at android.app.ActivityThread.main(ActivityThread.java:7229)at java.lang.reflect.Method.invoke(Native Method)at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1230) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
使用标记值的Espresso代码:
onView(withTagValue(is((Object)"Json"))).perform(click());
收到错误:
android.support.test.espresso.NoMatchingViewException:没有视图 层次结构找到匹配:带标签值:是&#34; Json&#34;
我在这个blog中也尝试了建议但仍然没有运气。
在我的代码下面:
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());
未收到任何错误,但tab2仍未执行点击。
请告知我缺少哪一部分并真正适合任何帮助。