【发布时间】:2017-01-02 07:12:42
【问题描述】:
我在我的应用程序中使用了两个导航视图,第二个(右)一个是通过自定义组件。它在手动测试期间工作正常,但是 espresso 在测试期间无法识别它。第二个 NAV 保持不可见并激活它对应方法的 onCreate 方法。
我尝试使用相同的方法测试第一个导航视图,它工作正常..
下面是相关代码sn-ps和日志
<?xml version="1.0" encoding="utf-8"?>
<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_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_left_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
<com.example.tt0401.NavigationViewRight
android:id="@+id/nav_right_view"
android:layout_width="wrap_content"
android:layout_height="600dp"
android:layout_marginTop="80dp"
android:layout_gravity="end"
android:fitsSystemWindows="true"
android:visibility="gone"
app:itemBackground="@drawable/nav_item_selector"
app:headerLayout="@layout/nav_header_right"
app:menu="@menu/activity_right_drawer" />
</android.support.v4.widget.DrawerLayout>
//onCreate(Bundle savedInstanceState) 方法中的代码使其可见
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED, navigationViewRight);
//Espresso 测试方法
@Test
public void navigationDrawer() throws Exception {
fabAddClick();
onView(withId(R.id.drawer_layout)).perform(DrawerActions.open(Gravity.END));
onView(withId(R.id.nav_right_view)).perform(NavigationViewActions.navigateTo(1000003));
}
浓缩咖啡失败日志
01-02 12:25:58.228 18570-18585/? I/TestRunner:android.support.test.espresso.PerformException:在视图“带有 id:com.example.tt0401:id/nav_right_view”上执行“点击带有 id 的菜单项”时出错。 在 android.support.test.espresso.PerformException$Builder.build(PerformException.java:83) 在 android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:80) 在 android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:56) 在 android.support.test.espresso.ViewInteraction.runSynchronouslyOnUiThread(ViewInteraction.java:184) 在 android.support.test.espresso.ViewInteraction.doPerform(ViewInteraction.java:115) 在 android.support.test.espresso.ViewInteraction.perform(ViewInteraction.java:87) 在 com.example.tt0401.RecordActivity3Test.navigationDrawer(RecordActivity3Test.java:135) 在 java.lang.reflect.Method.invoke(本机方法) 在 org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) 在 org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 在 org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) 在 org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 在 org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) 在 android.support.test.internal.statement.UiThreadStatement.evaluate(UiThreadStatement.java:55) 在 android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:270) 在 org.junit.rules.RunRules.evaluate(RunRules.java:20) 在 org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) 在 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) 在 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) 在 org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 在 org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 在 org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 在 org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 在 org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 在 org.junit.runners.ParentRunner.run(ParentRunner.java:363) 在 org.junit.runners.Suite.runChild(Suite.java:128) 在 org.junit.runners.Suite.runChild(Suite.java:27) 在 org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 在 org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 在 org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 在 org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 在 org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 在 org.junit.runners.ParentRunner.run(ParentRunner.java:363) 在 org.junit.runner.JUnitCore.run(JUnitCore.java:137) 在 org.junit.runner.JUnitCore.run(JUnitCore.java:115) 在 android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59) 在 android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262) 在 android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1932) 原因:java.lang.RuntimeException:由于目标视图不匹配以下一个或多个约束,将不会执行操作: (可从类分配:类 android.support.design.widget.NavigationView 并且视图具有有效的可见性=可见,并且至少 90% 的视图区域显示给用户。) 目标视图:“NavigationViewRight{id=2131624075, res-name=nav_right_view, visibility=INVISIBLE, width=980, height=2100, 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=false, is-selected=false, root-is-layout-requested=false, has-input-连接=假,x=1440.0,y=0.0,子计数=1}" 在 android.support.test.espresso.ViewInteraction$1.run(ViewInteraction.java:138) 在 java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:428) 在 java.util.concurrent.FutureTask.run(FutureTask.java:237) 在安卓 01-02 12:25:58.228 18570-18585/? I/TestRunner: ----- 结束异常 ----- 01-02 12:25:58.231 18570-18585/? I/TestRunner:完成:navigationDrawer(com.example.tt0401.RecordActivity3Test) 01-02 12:25:58.250 18570-18585/? I/TestRunner:运行完成:1 次测试,1 次失败,0 次忽略
【问题讨论】: