【问题标题】:How open tab in Espresso如何在 Espresso 中打开标签
【发布时间】:2014-07-29 13:22:25
【问题描述】:

如何在 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}"

【问题讨论】:

  • 该异常的完整堆栈跟踪是什么?对于 PerformExceptions,“Caused by”部分是最重要的。
  • @haffax 添加完整的堆栈跟踪。
  • 该错误表明您的目标视图是线性布局。我将首先在 xml 中仔细检查您的 id。你的标签上有文字吗?你能用“onView(withText('string here'))”来引用你的观点吗?
  • @Maxwell 添加 xml 文件。我不明白我应该如何打开标签?

标签: android android-tabs android-espresso


【解决方案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;
    }

现在它变得有趣了,因为这样我们可以轻松地在这些注入的View 对象上设置一个 ID 或标签,并在 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());

关于 TabHosts 的一般性

你为什么首先使用这个 TabHost?请注意,此类现在已弃用。 ViewPager with tabsthe ActionBar 可能是更好的选择,具体取决于您的用例。

使用 ViewHierarchy 工具

在这种情况下,第一个问题通常是找到正确的视图。为此,请使用 View Hierarchy 工具。它是 android SDK 的一部分,位于 tools 目录中。

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

cd ANDROID_SDK_LOCATION/tools
hierarchyviewer

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

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

现在结合使用布局视图和视图属性视图来查找所需视图的 ID 和标签。

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

【讨论】:

  • 这个解决方案是否与当前的 ViewPager api 兼容?
【解决方案2】:

唯一对我有用的是:

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 传递。

【讨论】:

  • 完全不需要标记视图。我认为这是最干净的解决方案。
【解决方案3】:

onView(withText("你的标签标签")).perform(click())

【讨论】:

  • 这是最简单的!
【解决方案4】:

也可以在标签文本上设置标签而不使用自定义指示器方法。您可以在创建标签后访问标签的TextView

使用 hcpl 示例代码中的 MainActivity,看起来像这样:

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());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-17
    • 2022-07-06
    • 2017-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多