我试图在我的Android应用程序中生成单元测试。 我使用了Record Espresso工具。
我的应用程序以SplashScreen开始,然后向MainActivity启动一个意图,显示主菜单。 主要活动是一个带有四个标签的片段。
我尝试使用主要活动的元素生成几个单一测试。 F.e .:打开导航抽屉,在listView中选择一个项目,按FAB按钮......并始终得到相同的错误:
android.support.test.espresso.AppNotIdleException: Looped for 3600 iterations over 60 SECONDS.
这是使用Espresso Recorder生成的其中一个测试的代码。选择匹配列表中的第一项,然后按后退按钮。
public class SplashScreenTest {
@Rule
public ActivityTestRule<SplashScreen> mActivityTestRule = new ActivityTestRule<>(SplashScreen.class);
@Test
public void splashScreenTest() {
// Added a sleep statement to match the app's execution delay.
// The recommended way to handle such scenarios is to use Espresso idling resources:
// https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html
try {
Thread.sleep(30380);
} catch (InterruptedException e) {
e.printStackTrace();
}
ViewInteraction linearLayout = onView(
allOf(childAtPosition(
allOf(withId(R.id.listaPartidos),
withParent(allOf(withClassName(is("android.widget.LinearLayout")),
withParent(allOf(withClassName(is("android.widget.RelativeLayout")),
withParent(withId(R.id.viewpager))))))),
0),
isDisplayed()));
linearLayout.perform(click());
// Added a sleep statement to match the app's execution delay.
// The recommended way to handle such scenarios is to use Espresso idling resources:
// https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html
try {
Thread.sleep(30892);
} catch (InterruptedException e) {
e.printStackTrace();
}
pressBack();
}
private static Matcher<View> childAtPosition(
final Matcher<View> parentMatcher, final int position) {
return new TypeSafeMatcher<View>() {
@Override
public void describeTo(Description description) {
description.appendText("Child at position " + position + " in parent ");
parentMatcher.describeTo(description);
}
@Override
public boolean matchesSafely(View view) {
ViewParent parent = view.getParent();
return parent instanceof ViewGroup && parentMatcher.matches(parent)
&& view.equals(((ViewGroup) parent).getChildAt(position));
}
};
}}
有人可以告诉我在哪里可以找到解决方案吗?
SplashScreen仅启动MainActivity。这将执行访问数据和信息可视化的逻辑。
编辑: 主要活动的XML文件有三个: 首先,activity_inicio.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_inicio"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:itemBackground="@android:color/transparent"
app:headerLayout="@layout/menu_encabezado"
app:menu="@menu/activity_inicio_drawer" />
然后,app_bar_inicio.xml
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
tools:context="ccv.dam.isi.frsf.utn.edu.ar.tpdam2016.Actividades.inicio.Inicio">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:theme="@style/AppTheme.AppBarOverlay"
android:layout_height="wrap_content"
app:elevation="0dp">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
<Spinner
android:id="@+id/spinnerCampeonatoInicio"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:gravity="center_vertical"/>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_inicio" />
最后,contenido_inicio,其中声明了tabHost。这四个选项卡以编程方式合并
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="ccv.dam.isi.frsf.utn.edu.ar.tpdam2016.Actividades.inicio.Inicio"
tools:showIn="@layout/app_bar_inicio">
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal"
android:background="?attr/colorPrimary"
/>
<FrameLayout
android:id="@android:id/tabcontent"
android:background="@color/colorBlanco"
android:layout_width="0dp"
android:layout_height="0dp"
/>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
</LinearLayout>
</TabHost>