使用Espresso测试RecyclerView ViewHolder文本

时间:2018-12-31 12:35:27

标签: android android-espresso

我有以下浓缩咖啡测试:

    @Test
    fun testSearchItemFound() {
        onView(withId(R.id.action_search)).perform(click())

        onView(isAssignableFrom(EditText::class.java))
                .perform(typeText("Aquaman"),
                         closeSoftKeyboard())

        // Check error message view is not displayed
        onView(withId(R.id.error_msg)).check(matches(not<View>(isDisplayed())))

        onView(withId(R.id.list)).check(matches(isDisplayed()))

        // Check the item we are looking for is in the search result list.
        onView(withId(R.id.list))
                .check(matches(atPosition(0, withText("Aquaman"))))
    }

我在最后一行收到以下错误:

android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'has item at position 0: with text: is "Aquaman"' doesn't match the selected view.
Expected: has item at position 0: with text: is "Aquaman"
Got: "AutofitRecyclerView{id=2131296398, res-name=list, visibility=VISIBLE, width=1080, height=1848, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, layout-params=android.view.ViewGroup$LayoutParams@b445b91, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=0}"

at dalvik.system.VMStack.getThreadStackTrace(Native Method)
at java.lang.Thread.getStackTrace(Thread.java:1536)
at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:88)
at android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:51)
at android.support.test.espresso.ViewInteraction.waitForAndHandleInteractionResults(ViewInteraction.java:314)
at android.support.test.espresso.ViewInteraction.check(ViewInteraction.java:297)
at com.sample.android.tmdb.TestMainActivity.testSearchItemFound(TestMainActivity.kt:124)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at android.support.test.internal.runner.junit4.statement.RunBefores.evaluate(RunBefores.java:80)
at android.support.test.internal.runner.junit4.statement.RunAfters.evaluate(RunAfters.java:61)
at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:527)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at android.support.test.runner.AndroidJUnit4.run(AndroidJUnit4.java:101)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:384)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2106)
Caused by: junit.framework.AssertionFailedError: 'has item at position 0: with text: is "Aquaman"' doesn't match the selected view.
Expected: has item at position 0: with text: is "Aquaman"
Got: "AutofitRecyclerView{id=2131296398, res-name=list, visibility=VISIBLE, width=1080, height=1848, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, layout-params=android.view.ViewGroup$LayoutParams@b445b91, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=0}"

at android.support.test.espresso.matcher.ViewMatchers.assertThat(ViewMatchers.java:539)
at android.support.test.espresso.assertion.ViewAssertions$MatchesViewAssertion.check(ViewAssertions.java:103)
at android.support.test.espresso.ViewInteraction$SingleExecutionViewAssertion.check(ViewInteraction.java:415)
at android.support.test.espresso.ViewInteraction$2.call(ViewInteraction.java:279)
at android.support.test.espresso.ViewInteraction$2.call(ViewInteraction.java:265)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

最后一行解决方案来自@riwnodennyk在How to assert inside a RecyclerView in Espresso?中的答案:

onView(withId(R.id.list))
                    .check(matches(atPosition(0, withText("Aquaman"))))

为什么在我的情况下不起作用?

可以在:https://github.com/Ali-Rezaei/TMDb-Paging/tree/master/app/src/androidTest/java/com/sample/android/tmdb

中找到来源

0 个答案:

没有答案