如何在 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/ 找到了另一个代码示例使用为选项卡注入(inject) 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;
}
现在它变得有趣了,因为通过这种方式我们可以轻松地在这些注入(inject)的 View
对象上设置 ID 或标签,并在 Espresso 中使用它们。
解决方案:标记 View
如果您调整该助手以接受每个 View 助手代码的标签,则代码将如下所示:
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());
替代解决方案:使用 View ID
如果您需要 ID,则需要在某处定义这些 ID。使用带有一些 ID 定义的简单 Android 资源文件。
View 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());
关于一般的 TabHosts
您为什么首先使用此 TabHost?请注意,此类现在已弃用。 ViewPager with tabs或 the ActionBar根据您的用例,可能是更好的选择。
使用 ViewHierarchy 工具
在这种情况下,第一个问题通常是找到合适的 View 。为此,请使用View Hierarchy 工具。它是 android SDK 的一部分,位于 tools 目录中。
您可以像这样从命令行启动它:
cd ANDROID_SDK_LOCATION/tools
hierarchyviewer
或使用 Android Studio 菜单:工具 > Android > Android 设备监视器。然后从设备监视器中的菜单打开 Hierarchy View 透视图:Window > Open Perspective > Hierarchy View。
我更喜欢第一个选项,因为设备监视器对我们的 Intent 来说太过分了。
现在结合使用布局 View 和 View 属性 View 来查找所需 View 的 ID 和标签。
有关此工具的一些说明:http://developer.android.com/tools/debugging/debugging-ui.html
关于android - 如何在 Espresso 中打开标签页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25016397/