android – 如何在Espresso中打开选项卡

如何在Espresso测试中打开标签?我试图做Espresso.onView(ViewMatchers.withId(R.id.practice_results_tab)).执行(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创建了一个示例项目

您的活动可能如下所示:

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,"tab1",R.string.textTabTitle1,R.drawable.ic_tab_settings,R.id.practice_settings_tab);
        this.setNewTab(this,"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 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;
    }

现在它变得有趣了,因为我们可以在这些注入的View对象上轻松设置ID或标签,并在Espresso中使用它们.

解决方案:标记视图

如果您调整该帮助程序以接受每个视图的标记,则帮助程序代码将如下所示:

private View getTabIndicator(Context context,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,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 tabsthe ActionBar可能是更好的选择.

使用ViewHierarchy工具

在这种情况下,第一个问题通常是找到合适的视图.为此,请使用“视图层次结构”工具.它是android SDK的一部分,位于tools目录中.

您可以从命令行启动它,如下所示:

cd ANDROID_SDK_LOCATION/tools
hierarchyviewer

或使用Android Studio菜单:工具> Android> Android设备监视器.然后从设备监视器的菜单中打开Hierarchy View透视图:Window> Open Perspective>层次结构视图.

我更喜欢第一个选项,因为设备监视器对我们的意图做得太多了.

现在将布局视图与“视图属性”视图结合使用,以查找所需视图的ID和标记.

有关此工具的一些说明:http://developer.android.com/tools/debugging/debugging-ui.html

相关文章

###实现效果*本实例主要实现用ViewPage和Fragment实现选项卡...
一、安装 JDK 下载JDK最新版本,下载地址如下: http://www....
这篇“android轻量级无侵入式管理数据库自动升级组件怎么实现...
今天小编给大家分享一下Android实现自定义圆形进度条的常用方...
这篇文章主要讲解了“Android如何解决字符对齐问题”,文中的...
这篇文章主要介绍“Android岛屿数量算法怎么使用”的相关知识...