浓缩咖啡“已启用”的状态与所选视图不匹配。

5

我正在使用Espresso 2来测试一个按钮是否启用。我的代码如下:

  onView(withId(R.id.fragment_vote_pause_btn)).check(matches(isEnabled()));

我的按钮定义如下:

    <Button
        android:id="@+id/fragment_vote_pause_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:background="@android:color/transparent"
        android:text="@string/audiowave_pause_or_play"
        android:textColor="@drawable/play_pause_button_selector"
        android:textAllCaps="false"
        android:textSize="12sp" />

有时测试通过,但有时会出现以下异常:
android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'is enabled' doesn't match the selected view.
Expected: is enabled
Got: "Button{id=2131296458, res-name=fragment_vote_pause_btn, visibility=INVISIBLE, width=329, height=144, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=true, is-enabled=false, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=375.0, y=649.0, text=Tap to play or pause, input-type=0, ime-target=false, has-links=false}"

at dalvik.system.VMStack.getThreadStackTrace(Native Method)
at java.lang.Thread.getStackTrace(Thread.java:580)
at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:82)
at android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:53)
at android.support.test.espresso.ViewInteraction.runSynchronouslyOnUiThread(ViewInteraction.java:185)
at android.support.test.espresso.ViewInteraction.check(ViewInteraction.java:158)
at be.appfoundry.identify.ui.VoteActivityTest.test2PlayPauseButton(VoteActivityTest.java:39)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:192)
at junit.framework.TestCase.runBare(TestCase.java:134)
at junit.framework.TestResult$1.protect(TestResult.java:115)
at junit.framework.TestResult.runProtected(TestResult.java:133)
at android.support.test.internal.runner.junit3.DelegatingTestResult.runProtected(DelegatingTestResult.java:90)
at junit.framework.TestResult.run(TestResult.java:118)
at android.support.test.internal.runner.junit3.AndroidTestResult.run(AndroidTestResult.java:49)
at junit.framework.TestCase.run(TestCase.java:124)
at android.support.test.internal.runner.junit3.NonLeakyTestSuite$NonLeakyTest.run(NonLeakyTestSuite.java:63)
at junit.framework.TestSuite.runTest(TestSuite.java:243)
at junit.framework.TestSuite.run(TestSuite.java:238)
at android.support.test.internal.runner.junit3.DelegatingTestSuite.run(DelegatingTestSuite.java:103)
at android.support.test.internal.runner.junit3.AndroidTestSuite.run(AndroidTestSuite.java:63)
at android.support.test.internal.runner.junit3.JUnit38ClassRunner.run(JUnit38ClassRunner.java:90)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:24)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at org.junit.runner.JUnitCore.run(JUnitCore.java:136)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:270)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1837)
Caused by: junit.framework.AssertionFailedError: 'is enabled' doesn't match the selected view.
Expected: is enabled
Got: "Button{id=2131296458, res-name=fragment_vote_pause_btn, visibility=INVISIBLE, width=329, height=144, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=true, is-enabled=false, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=375.0, y=649.0, text=Tap to play or pause, input-type=0, ime-target=false, has-links=false}"

at android.support.test.espresso.matcher.ViewMatchers.assertThat(ViewMatchers.java:955)
at android.support.test.espresso.assertion.ViewAssertions$2.check(ViewAssertions.java:89)
at android.support.test.espresso.ViewInteraction$2.run(ViewInteraction.java:170)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

有人有想法吗?

1个回答

7
在您检查按钮是否启用时,我觉得您的按钮目前不可见。
可能是因为您正在检查尚未激活的窗口/按钮?还可能是操作不同步,并且您必须使用空闲资源等待资源启用/显示。
情况可能是: 1)活动没有完全打开/加载(适配器等) 2)手势没有完全完成(滚动等) 3)单击消息框后… 4)其他情况下,您的按钮被阻止/不可见
哪一种是您的情况?当它失败时,您是否在设备屏幕上看到了?
这个链接也可能有帮助 - https://code.google.com/p/android-test-kit/wiki/EspressoSamples

我的按钮从一开始就可见,但是它并没有从一开始就启用。 - user1007522
据我理解,它是可见的但未启用...某些操作正在启用它,您想检查它是否已启用。但是错误显示在您进行检查时它的 visibility=INVISIBLEis-enabled=false。您是否与启用操作同步? - Anna
你是对的。在代码中,按钮被禁用在xml中没有。 - user1007522

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接