如何在Espresso中打开标签 [英] How open tab in Espresso

查看:69
本文介绍了如何在Espresso中打开标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Espresso测试中打开标签页?我试图做Espresso.onView(ViewMatchers.withId(R.id.practice_results_tab)).perform(ViewActions.click());,但这不起作用.在该代码中,我打开了此选项卡的布局. 有XML文件:

How can i open tab in Espresso test? I tried to do Espresso.onView(ViewMatchers.withId(R.id.practice_results_tab)).perform(ViewActions.click());, but that doesn't working. In that code i opened Layout of this tab. There is the XML file:

    <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打开标签页?

What ID should I use to open tab?

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://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, 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的帮助程序方法.

I found another code example at https://maxalley.wordpress.com/2012/10/27/android-styling-the-tabs-in-a-tabwidget/ with a helper method that injects an ImageView for the tabs.

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;
    }

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

Now it gets interesting cause this way we can easily set an ID or a tag on these injected View objects and use these in Espresso.

如果让该帮助程序接受每个视图的标签,则帮助程序代码将如下所示:

If you adapt that helper to accept a tag for each view helper code will look something like this:

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代码以查看以下标签:

And the Espresso code to click these tabs:

Espresso.onView(ViewMatchers.withTagValue(Matchers.is((Object)"tab1"))).perform(ViewActions.click());

替代解决方案:使用视图ID

如果您要获取ID,则需要在某个地方定义这些ID.使用带有一些ID定义的简单Android资源文件.

Alternative Solution: using View IDs

If you go for IDs you need these IDs to be defined somewhere. Use a simple Android resource file with some ID definitions.

名为/values/ids.xml的视图ID资源文件:

The view ID resource file, named /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代码以查看以下标签:

And the Espresso code to click these tabs:

Espresso.onView(ViewMatchers.withId(R.id.tab1)).perform(ViewActions.click());

关于一般TabHosts

为什么首先使用此TabHost?请注意,目前不推荐使用此类. 带有标签的ViewPager

About TabHosts in general

Why did you use this TabHost in the first place? Note that this class is deprecated by now. A ViewPager with tabs or the ActionBar might be better options depending on your use case.

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

In cases like this the first problem is often to find the proper view. For this use the View Hierarchy tool. It's part of the android SDK and lives in the tools directory.

您可以像这样从命令行启动它:

You can start it from command line like this:

cd ANDROID_SDK_LOCATION/tools
hierarchyviewer

或使用 Android Studio 菜单:工具> Android> Android设备监视器.然后从设备监视器的菜单中打开层次结构视图"透视图:窗口">打开透视图">层次结构视图".

Or use Android Studio menu: Tools > Android > Android Device Monitor. Then open the Hierarchy View perspective from the menu in the device monitor: Window > Open Perspective > Hierarchy View .

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

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

Now use the Layout View in combination with the View Properties view to find the ID and tags of the view you want.

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

这篇关于如何在Espresso中打开标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆