如何在 Espresso 中打开选项卡
-
21-12-2019 - |
题
如何在 Espresso 测试中打开选项卡?我尝试做 Espresso.onView(ViewMatchers.withId(R.id.practice_results_tab)).perform(ViewActions.click());
, ,但这不起作用。在该代码中,我打开了此选项卡的布局。有 XML 文件:
<TabHost
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/practice_tabHost">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="@+id/practice_settings_tab"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:id="@+id/practice_results_tab"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
我应该使用什么 ID 来打开选项卡?
logcat 中出现错误:
Caused by: java.lang.RuntimeException: Action will not be performed because the target view does not match one or more of the following constraints:
at least 90 percent of the view's area is displayed to the user.
Target view: "LinearLayout{id=2131296384, res-name=practice_results_tab, 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}"
解决方案
已完成的代码
您确实需要添加代码以便我们给出正确的答案。我猜测您按照本示例的方式使用了 TabHost 和 TabWidget: https://maxalley.wordpress.com/2012/10/25/android-creating-a-tab-layout-with-tabhost-and-tabwidget/
我创建了一个示例项目 https://github.com/hanscappelle/SO-25016397
您的 Activity 可能如下所示:
public class MainActivity extends TabActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost tabHost = getTabHost();
// setNewTab(context, tabHost, tag, title, icon, contentID);
this.setNewTab(this, tabHost, "tab1", R.string.textTabTitle1, R.drawable.ic_tab_settings, R.id.practice_settings_tab);
this.setNewTab(this, tabHost, "tab2", R.string.textTabTitle2, R.drawable.ic_tab_results, R.id.practice_results_tab);
}
private void setNewTab(Context context, TabHost tabHost, String tag, int title, int icon, int contentID ){
TabSpec tabSpec = tabHost.newTabSpec(tag);
String titleString = getString(title);
tabSpec.setIndicator(titleString, context.getResources().getDrawable(android.R.drawable.star_on));
tabSpec.setContent(contentID);
tabHost.addTab(tabSpec);
}
}
我在以下位置找到了另一个代码示例 https://maxalley.wordpress.com/2012/10/27/android-styling-the-tabs-in-a-tabwidget/ 使用注入的辅助方法 ImageView
对于选项卡。
private View getTabIndicator(Context context, int title, int icon) {
View view = LayoutInflater.from(context).inflate(R.layout.tab_layout, null);
ImageView iv = (ImageView) view.findViewById(R.id.imageView);
iv.setImageResource(icon);
TextView tv = (TextView) view.findViewById(R.id.textView);
tv.setText(title);
return view;
}
现在它变得有趣了,因为这样我们就可以轻松地在这些注入的内容上设置 ID 或标签 View
对象并在 Espresso 中使用它们。
一个办法:标记视图
如果您调整该帮助程序以接受每个视图帮助程序的标签,代码将如下所示:
private View getTabIndicator(Context context, int title, int icon, int viewId, String viewTag) {
View view = LayoutInflater.from(context).inflate(R.layout.tab_layout, null);
ImageView iv = (ImageView) view.findViewById(R.id.image_view);
iv.setImageResource(icon);
TextView tv = (TextView) view.findViewById(R.id.text_view);
tv.setText(title);
tv.setTag(viewTag);
return view;
}
如果您只使用图标,您也可以在 ImageView 上设置 ID。
以及单击这些选项卡的 Espresso 代码:
Espresso.onView(ViewMatchers.withTagValue(Matchers.is((Object)"tab1"))).perform(ViewActions.click());
替代解决方案:使用视图 ID
如果您需要 ID,则需要在某处定义这些 ID。使用带有一些 ID 定义的简单 Android 资源文件。
视图ID资源文件,名为/values/ids.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="tab1" type="id" />
</resources>
适配的助手:
private View getTabIndicator(Context context, int title, int icon, int viewId, String viewTag) {
View view = LayoutInflater.from(context).inflate(R.layout.tab_layout, null);
ImageView iv = (ImageView) view.findViewById(R.id.image_view);
iv.setImageResource(icon);
TextView tv = (TextView) view.findViewById(R.id.text_view);
tv.setText(title);
tv.setId(viewId);
return view;
}
如果您只使用图标,您也可以在 ImageView 上设置 ID。
以及单击这些选项卡的 Espresso 代码:
Espresso.onView(ViewMatchers.withId(R.id.tab1)).perform(ViewActions.click());
关于 TabHost 的一般情况
您最初为什么使用这个 TabHost?请注意,此类现在已被弃用。A 带选项卡的 ViewPager 或者 操作栏 根据您的用例,可能是更好的选择。
使用视图层次结构工具
在这种情况下,第一个问题通常是找到正确的视图。为此,请使用 查看层次结构 工具。它是 android SDK 的一部分,位于 tools 目录中。
您可以从命令行启动它,如下所示:
cd ANDROID_SDK_LOCATION/tools
hierarchyviewer
或者使用 安卓工作室 菜单:工具 > Android > Android 设备监视器。然后从设备监视器的菜单中打开“层次结构视图”透视图:窗口 > 打开透视图 > 层次结构视图。
我更喜欢第一个选项,因为设备监视器对我们的意图做了太多的事情。
现在,结合使用“布局视图”和“视图属性”视图来查找所需视图的 ID 和标签。
有关此工具的一些说明: http://developer.android.com/tools/debugging/debugging-ui.html
其他提示
唯一对我有用的是:
public void clickOnTab(String tabText) {
Matcher<View> matcher = allOf(withText(tabText),
isDescendantOfA(withId(R.id.ll_tab_container)));
onView(matcher).perform(click());
}
在哪里 ll_tab_container
是我的自定义选项卡布局的线性布局。如果您有一个名为“购物”的选项卡,那么您可以将其作为 tabText 传递。
onview(withext(“您的选项卡标签”))。执行(单击())
还可以在选项卡文本上设置标签,而无需使用自定义指示器方法。您可以通过访问选项卡来执行此操作 TextView
创建后。
使用 MainActivity
从 hcpl 的示例代码来看,它看起来像这样:
public MainActivity extends TabActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost tabHost = getTabHost();
// setNewTab(context, tabHost, tag, title, icon, contentID);
this.setNewTab(this, tabHost, "tab1", R.string.textTabTitle1, R.drawable.ic_tab_settings, R.id.practice_settings_tab);
this.setNewTab(this, tabHost, "tab2", R.string.textTabTitle2, R.drawable.ic_tab_results, R.id.practice_results_tab);
// Set custom tag on first tab
View tabView1 = tabHost.getTabWidget().getChildAt(0);
TextView tabView1Text = (TextView) signUpTabView.findViewById(android.R.id.title);
signUpTextTab.setTag("TAG_TAB_ONE");
// Set custom tag on second tab
View tabView2 = tabHost.getTabWidget().getChildAt(1);
TextView tabView1Text = (TextView) signUpTabView.findViewById(android.R.id.title);
signUpTextTab.setTag("TAG_TAB_TWO");
}
}
然后在 Espresso 测试中,您可以像这样访问选项卡:
onView(withTagValue(Matchers.is((Object)"TAG_TAB_TWO"))).perform(click());