Android:Espresso:为什么不在错误消息中打印当前背景色?

时间:2019-04-17 08:04:53

标签: android android-espresso

我要检查容器的背景颜色:

<androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/traderSummaryContainer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/mcgpalette0_100"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/jsonViewToolBar">

您会看到背景颜色为mcgpalette0_100

在color.xml中

<color name="mcgpalette0_100">#cccccc</color>
<color name="mcgpalette0_400">#6f6f6f</color>

我为背景颜色编写Espresso自定义匹配器。

fun withBackgroundColorResId(@IdRes expectedId: Int): Matcher<View> {

        return object : BoundedMatcher<View, ViewGroup>(ViewGroup::class.java) {
            override fun matchesSafely(view: ViewGroup): Boolean {
                val color = (view.background.current as ColorDrawable).color
                val expectedColor = ContextCompat.getColor(view.context, expectedId)
                var isEqual = false
                if (color == expectedColor) {
                    isEqual = true
                }
                return isEqual
            }

            override fun describeTo(description: Description) {
                description.appendText("with background color: ")
                description.appendValue(context.getResources().getString(expectedId))
            }
        }
    }

这里是Espresso成功测试:

@Test
    fun traderSummary_backgroundColor() {
        onView(withId(R.id.traderSummaryContainer))
                .check(matches(withBackgroundColorResId(R.color.mcgpalette0_100)))
    }

在这里测试失败

 @Test
    fun traderSummary_backgroundColor() {
        onView(withId(R.id.traderSummaryContainer))
                .check(matches(withBackgroundColorResId(R.color.mcgpalette0_400)))
    }

测试失败时,出现错误消息:

开始运行测试

androidx.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'with background color: "#ff6f6f6f"' doesn't match the selected view.
Expected: with background color: "#ff6f6f6f"
Got: "ConstraintLayout{id=2131231014, res-name=traderSummaryContainer, visibility=VISIBLE, width=1080, height=267, 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, layout-params=androidx.constraintlayout.widget.ConstraintLayout$LayoutParams@ed8a6d2, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=168.0, child-count=6}"

at dalvik.system.VMStack.getThreadStackTrace(Native Method)
at java.lang.Thread.getStackTrace(Thread.java:580)
at androidx.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:96)
at androidx.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:59)
at androidx.test.espresso.ViewInteraction.waitForAndHandleInteractionResults(ViewInteraction.java:318)
at androidx.test.espresso.ViewInteraction.check(ViewInteraction.java:300)
at com.myproject.android.activity.TraderDetailsActivityTest.traderSummary_backgroundColor(TraderDetailsActivityTest.kt:147)
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 androidx.test.internal.runner.junit4.statement.RunBefores.evaluate(RunBefores.java:80)
at androidx.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:531)
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 androidx.test.runner.AndroidJUnit4.run(AndroidJUnit4.java:104)
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 androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:389)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1879)
Caused by: junit.framework.AssertionFailedError: 'with background color: "#ff6f6f6f"' doesn't match the selected view.
Expected: with background color: "#ff6f6f6f"
Got: "ConstraintLayout{id=2131231014, res-name=traderSummaryContainer, visibility=VISIBLE, width=1080, height=267, 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, layout-params=androidx.constraintlayout.widget.ConstraintLayout$LayoutParams@ed8a6d2, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=168.0, child-count=6}"

at androidx.test.espresso.matcher.ViewMatchers.assertThat(ViewMatchers.java:539)

您可以看到我收到消息:

androidx.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'with background color: "#ff6f6f6f"' doesn't match the selected view.
Expected: with background color: "#ff6f6f6f"

但是我还需要在错误消息中打印容器的当前背景色。我该怎么做?

我需要这样的消息:

androidx.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'with background color: "#ff6f6f6f"' doesn't match the selected view.
Current:  with background color:  #ffcccccc
Expected: with background color: "#ff6f6f6f"

我该怎么做?

0 个答案:

没有答案