单元测试在通过"include tag"单击布局中包含的视图时失败



我的问题:

我当前的单元测试" OnClickSkipButtonAndVerifyAppexists"失败了,因为我无法单击" include layout ="@layout/navigation_bar中的跳过按钮。它的布局基本上是左侧的跳过按钮,右侧是一个下一个按钮。我怀疑它失败的原因是因为它无法从Inclupher标签中单击视图。

我有以下方案:

@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>

建议:

如果有人可以提供见解,而不是认为这是实际问题,请这样做。在得出结论认为这可能是问题之前,我尝试了多件事,但是再次欢迎任何想法。

首先,您应该手动检查是否显示跳过按钮并有效(您可以单击它(。从XML代码的外观中,帧布局 full_screen_content 都会越过底栏,这可能会导致问题。

如果按钮可见并且可单击,请再次检查ID navigation_bar_skip 是否正确。或者,您可以尝试使用wittext方法而不是withID ...

另外,您无需覆盖方法后启动和屈服时进行了反击。在准备工作之前,测试不会启动。如果需要此暂停,则可以将其放入@Test方法中。对于初始化,您应该使用@before和 @ferter,它将在每次测试之前和之后运行。

最新更新