测试检查“ textView.typeface.style”在Android 5.0+上通过,但在Android 4.4上失败

时间:2019-05-09 13:57:52

标签: android android-espresso ui-testing

在app / build.xml中

ext.ESPRESSO_VERSION = '3.2.0-alpha02'

    androidTestCompile 'com.azimolabs.conditionwatcher:conditionwatcher:0.2'

    androidTestImplementation "androidx.test.espresso:espresso-core:$ESPRESSO_VERSION"
    androidTestImplementation "androidx.test.espresso:espresso-intents:$ESPRESSO_VERSION"
    androidTestImplementation "androidx.test.espresso:espresso-contrib:$ESPRESSO_VERSION"
    androidTestImplementation "androidx.test:rules:$ANDROIDX_TEST"
    androidTestImplementation "androidx.test:runner:$ANDROIDX_TEST"
    androidTestImplementation 'com.squareup.okhttp3:mockwebserver:3.8.0'

    testImplementation 'junit:junit:4.12'

My xml with button


        <com.google.android.material.button.MaterialButton
            android:id="@+id/startButton"
            android:layout_width="0dp"
            android:layout_height="@dimen/min_height"
            android:layout_marginTop="@dimen/default_margin"
            android:layout_marginBottom="@dimen/default_margin"
            android:gravity="center"
            android:onClick="@{() -> handler.onClickStart()}"
            android:text="@string/start"
            android:textSize="@dimen/tool_bar_text_size"
            app:layout_constraintEnd_toEndOf="@+id/baseTextView"
            app:layout_constraintStart_toStartOf="@+id/baseTextView"
            app:layout_constraintTop_toBottomOf="@+id/quoteTextInputEditText"
            app:textAllCaps="true" />

Espresso的测试,以检查文本样式是否正常

 @Test
    fun buttonStart_textStyle() {
        onView(withId(startButton))
                .check(matches(withTextStyleValue(Typeface.NORMAL)))
}

此处是自定义匹配项:

 fun withTextStyleValue(exptectedTypeFaceValue: Int): Matcher<View> {
        return object : BoundedMatcher<View, TextView>(TextView::class.java) {

            override fun matchesSafely(textView: TextView): Boolean {
                return exptectedTypeFaceValue == textView.typeface.style
            }

            override fun describeTo(description: Description) {
                description.appendText("has exptectedTypeFaceValue resource $exptectedTypeFaceValue")
            }
        }
    }

测试成功在Android 5.0及更高版本上通过。在模拟器5.0上成功,在Android 6.0设备上-成功。 很好。

但是在Android 4.4上。模拟器此测试失败,并显示以下消息:

$ adb shell am instrument -w -r   -e debug false -e class 'com.myproject.activity.AddTraderActivityTest#buttonStart_textStyle' com.myproject.debug.test/androidx.test.runner.AndroidJUnitRunner
Client not ready yet..
Started running tests

androidx.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'has exptectedTypeFaceValue resource 0' doesn't match the selected view.
Expected: has exptectedTypeFaceValue resource 0
Got: "MaterialButton{id=2131230974, res-name=startButton, visibility=VISIBLE, width=432, height=72, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, layout-params=androidx.constraintlayout.widget.ConstraintLayout$LayoutParams@ad0a0880, tag=null, root-is-layout-requested=false, has-input-connection=false, x=24.0, y=356.0, text=Start, input-type=0, ime-target=false, has-links=false}"

at dalvik.system.VMStack.getThreadStackTrace(Native Method)
at java.lang.Thread.getStackTrace(Thread.java:579)
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.activity.AddTraderActivityTest.buttonStart_textStyle(AddTraderActivityTest.kt:351)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
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.internal.runner.junit4.statement.RunAfters.evaluate(RunAfters.java:61)
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:392)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701)
Caused by: junit.framework.AssertionFailedError: 'has exptectedTypeFaceValue resource 0' doesn't match the selected view.
Expected: has exptectedTypeFaceValue resource 0
Got: "MaterialButton{id=2131230974, res-name=startButton, visibility=VISIBLE, width=432, height=72, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, layout-params=androidx.constraintlayout.widget.ConstraintLayout$LayoutParams@ad0a0880, tag=null, root-is-layout-requested=false, has-input-connection=false, x=24.0, y=356.0, text=Start, input-type=0, ime-target=false, has-links=false}"

at androidx.test.espresso.matcher.ViewMatchers.assertThat(ViewMatchers.java:539)
at androidx.test.espresso.assertion.ViewAssertions$MatchesViewAssertion.check(ViewAssertions.java:103)
at androidx.test.espresso.ViewInteraction$SingleExecutionViewAssertion.check(ViewInteraction.java:419)
at androidx.test.espresso.ViewInteraction$2.call(ViewInteraction.java:282)
at androidx.test.espresso.ViewInteraction$2.call(ViewInteraction.java:268)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案