【问题标题】:Unit test failing on clicking on view included in layout via "include tag"通过“包含标签”单击布局中包含的视图时单元测试失败
【发布时间】:2017-04-20 18:47:32
【问题描述】:

我的问题:

我当前的单元测试“onClickSkipButtonAndVerifyAppExists”失败,因为我无法单击包含布局 =“@layout/navigation_bar”中的跳过按钮。它的布局基本上是左侧的跳过按钮和右侧的下一步按钮。我怀疑它失败的原因是因为它无法单击包含标签中的视图。

我有以下场景:

@RunWith(AndroidJUnit4.class)
@LargeTest
public class MainActivityTest {

    @Rule
    public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<MainActivity>(MainActivity.class, true, true) {

        @Override
        protected void afterActivityLaunched() {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        @Override
        protected void afterActivityFinished() {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    };

    @Test
    public void onClickSkipButtonAndVerifyAppExists() {
        onView(withId(R.id.navigation_bar_skip)).perform(click());
    }

}

另外,我用于该特定测试的 XML 布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ignite="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="false">
    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/main_activity_outer_coordinator"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/bottom_bar">
        <RelativeLayout
            android:id="@+id/main_activity_inner"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".MainActivity">

            <android.support.design.widget.AppBarLayout
                android:id="@+id/toolbar_app_bar"
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:theme="@style/AppTheme.AppBarOverlay"
                android:background="@color/header_footer">


                <com.digitalturbine.igniteui.view.DynamicLayoutToolbar
                    android:id="@+id/main_toolbar"
                    android:layout_height="wrap_content"
                    android:layout_width="match_parent"
                    ignite:popupTheme="@style/AppTheme.PopupOverlay"
                    ignite:contentInsetStart="0dp"
                    android:background="@drawable/toolbarBackground"
                    ignite:layoutType="left_two_line_title_w_icon"/>

            </android.support.design.widget.AppBarLayout>

            <FrameLayout
                android:id="@+id/content_container"
                android:layout_below="@+id/toolbar_app_bar"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

        </RelativeLayout>

        <View
            android:id="@+id/main_activity_overlay"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/wizard_disabled_overlay"
            android:visibility="gone"/>

    </android.support.design.widget.CoordinatorLayout>

    <LinearLayout
        android:id="@+id/bottom_bar"
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_height="@dimen/navigation_bar_height"
        android:layout_alignParentBottom="true">
        <include layout="@layout/navigation_bar"/>
    </LinearLayout>

    <FrameLayout
        android:id="@+id/full_screen_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>

建议:

如果有人可以提供见解并且不认为这是实际问题,请提供。在得出结论认为这可能是问题之前,我尝试了多种方法,但同样欢迎任何想法。

【问题讨论】:

    标签: android unit-testing android-espresso


    【解决方案1】:

    首先您应该手动检查是否显示跳过按钮并且它是否有效(您可以单击它)。从您的 XML 代码来看,框架布局 full_screen_content 超出了底栏,因此可能会导致问题。

    如果按钮可见且可点击,请再次检查 id navigation_bar_skip 是否正确。或者你可以尝试使用 withText 方法而不是 withId...

    另外,您不需要重写方法 afterActivityLaunched 和 afterActivityFinished。在活动准备好之前,测试不会开始。如果你想要这个暂停,你可以把它放在@Test 方法中。对于初始化,您应该使用@Before 和@After,它们将在每次测试之前和之后运行。

    【讨论】:

    • 这实际上是由于 MainActivity 类。它的一项功能没有正确执行,因此我遇到了这个问题。
    猜你喜欢
    • 1970-01-01
    • 2017-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-22
    • 1970-01-01
    • 1970-01-01
    • 2010-12-25
    相关资源
    最近更新 更多