Android Espresso执行(click())不起作用

时间:2017-11-23 08:41:31

标签: android android-espresso

我正在使用espresso和mockito为Android应用程序编写测试。在我尝试对视图或按钮执行单击操作的少数情况下,单击操作在测试完成之前不会注册。

活动:

public void leavePageClicked(View v) {
    Log.i(TAG, "Leaving page with url: "+url);
    Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    startActivity(browserIntent);
}

布局:

<Button
        android:id="@+id/leaveApplicationButton"
        style="@style/purple_button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="40dp"
        android:layout_marginStart="40dp"
        android:layout_marginTop="45dp"
        android:onClick="leavePageClicked"
        android:text="@string/leave_application"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/urlTextView"/>

试验:

@Rule
public IntentsTestRule<LeavingPageActivity> intentsTestRule =
            new IntentsTestRule<LeavingPageActivity>(LeavingPageActivity.class, true, false);


@Before
public void setup() {
    Context targetContext = InstrumentationRegistry.getInstrumentation()
            .getTargetContext();
    Intent intent = new Intent(targetContext, LeavingPageActivity.class);
    intent.putExtra(LeavingPageActivity.EXTRA_URL, "http://www.google.com");
    intentsTestRule.launchActivity(intent);
}

@After
public void cleanup() {
    intentsTestRule.finishActivity();
}

@Test
public void testLeavePageClick() {
   intending(allOf(
           hasAction(Intent.ACTION_VIEW)
   )).respondWith(new Instrumentation.ActivityResult(Activity.RESULT_OK, null));
    onView(withId(R.id.leaveApplicationButton)).perform(click());
    intended(allOf(
            hasAction(Intent.ACTION_VIEW),
            hasData("http://www.google.com")
    ));
}

Log.i(TAG,“使用网址离开页面:”+网址);永远不会用logcat写的。

11-22 13:31:36.339 24319-24337/com.app.test I/TestRunner: started: testLeavePageClick(com.app.test.LeavingPageActivityTest)
11-22 13:31:36.339 24319-24319/com.app.test I/MonitoringInstr: Activities that are still in CREATED to STOPPED: 0
11-22 13:31:36.339 24319-24337/com.app.test I/ActivityTestRule: Launching activity: ComponentInfo{com.app.test/com.app.test.activities.LeavingPageActivity}
11-22 13:31:36.339 24319-24381/com.app.test D/MonitoringInstr: execStartActivity(context, ibinder, ibinder, activity, intent, int, bundle
11-22 13:31:36.379 24319-24319/com.app.test D/LifecycleMonitor: Lifecycle status change: com.app.test.activities.LeavingPageActivity@2e635cc7 in: PRE_ON_CREATE
11-22 13:31:36.389 24319-24319/com.app.test W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
11-22 13:31:36.559 24319-24319/com.app.test D/LifecycleMonitor: Lifecycle status change: com.app.test.activities.LeavingPageActivity@2e635cc7 in: CREATED
11-22 13:31:36.559 24319-24319/com.app.test D/LifecycleMonitor: Lifecycle status change: com.app.test.activities.LeavingPageActivity@2e635cc7 in: STARTED
11-22 13:31:36.559 24319-24319/com.app.test D/LifecycleMonitor: Lifecycle status change: com.app.test.activities.LeavingPageActivity@2e635cc7 in: RESUMED
11-22 13:31:36.569 24319-24319/com.app.test D/Atlas: Validating map...
11-22 13:31:36.599 24319-24383/com.app.test I/Adreno-EGL: <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build: TEST SBA AU_LINUX_ANDROID_LA.AF.1.1_RB1.05.00.00.006.014 + c1105519 ()
                                                                           OpenGL ES Shader Compiler Version: E031.25.03.00
                                                                           Build Date: 01/23/15 Fri
                                                                           Local Branch: 
                                                                           Remote Branch: refs/tags/AU_LINUX_ANDROID_LA.AF.1.1_RB1.05.00.00.006.014
                                                                           Local Patches: NONE
                                                                           Reconstruct Branch: NOTHING
11-22 13:31:36.699 24319-24337/com.app.test D/InputManagerEventInjectionStrategy: Creating injection strategy with input manager.
11-22 13:31:36.729 24319-24319/com.app.test I/ViewInteraction: Performing 'single click' action on view with id: com.app.test:id/leaveApplicationButton
11-22 13:31:37.000 24319-24319/com.app.test I/ViewInteraction: Checking 'android.support.test.espresso.intent.Intents$2@148c4653' assertion on view is a root view.
11-22 13:31:37.040 24319-24319/com.app.test D/LifecycleMonitor: Lifecycle status change: com.app.test.activities.LeavingPageActivity@2e635cc7 in: PAUSED
11-22 13:31:37.050 24319-24337/com.app.test I/TestRunner: failed: testLeavePageClick(com.app.test.LeavingPageActivityTest)
11-22 13:31:37.050 24319-24337/com.app.test I/TestRunner: ----- begin exception -----
11-22 13:31:37.050 24319-24337/com.app.test I/TestRunner: android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: Wanted to match 1 intents. Actually matched 0 intents.

                                                                           IntentMatcher: (has action: is "android.intent.action.VIEW" and has data: is <http://www.google.com>)

                                                                           Matched intents:[]

                                                                           Recorded intents:[]
                                                                               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:90)
                                                                               at android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:52)
                                                                               at android.support.test.espresso.ViewInteraction.waitForAndHandleInteractionResults(ViewInteraction.java:314)
                                                                               at android.support.test.espresso.ViewInteraction.check(ViewInteraction.java:291)
                                                                               at android.support.test.espresso.intent.Intents.intended(Intents.java:185)
                                                                               at android.support.test.espresso.intent.Intents.intended(Intents.java:167)
                                                                               at com.app.test.LeavingPageActivityTest.testLeavePageClick(LeavingPageActivityTest.java:74)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at java.lang.reflect.Method.invoke(Method.java:372)
                                                                               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:433)
                                                                               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 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:58)
                                                                               at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:375)
                                                                               at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1976)
                                                                            Caused by: junit.framework.AssertionFailedError: Wanted to match 1 intents. Actually matched 0 intents.

                                                                           IntentMatcher: (has action: is "android.intent.action.VIEW" and has data: is <http://www.google.com>)

                                                                           Matched intents:[]

                                                                           Recorded intents:[]
                                                                               at junit.framework.Assert.fail(Assert.java:50)
                                                                               at android.support.test.espresso.intent.VerificationModes$Times.verify(VerificationModes.java:85)
                                                                               at android.support.test.espresso.intent.Intents.internalIntended(Intents.java:280)
                                                                               at android.support.test.espresso.intent.Intents$2.check(Intents.java:188)
                                                                               at android.support.test.espresso.ViewInteraction$SingleExecutionViewAssertion.check(ViewInteraction.java:415)
                                                                            at android.su
11-22 13:31:37.050 24319-24337/com.app.test I/TestRunner: ----- end exception -----
11-22 13:31:37.050 24319-24337/com.app.test I/TestRunner: finished: testLeavePageClick(com.app.test.LeavingPageActivityTest)

1 个答案:

答案 0 :(得分:1)

这不理想,我认为在测试中使用睡眠会失败,但是,如果必须的话,可以在单击之前添加睡眠以让仿真器有时间稳定下来。注意:使用Firebase Test Cloud时,我只会看到问题中描述的行为。所有本地仿真器都不会因此问题而失败。

在单击按钮之前添加睡眠使我的测试始终在Firebase测试云上通过:

        try {
        android.support.test.espresso.intent.Intents.init();
        ...

        scrollView.scrollTo(0, viewToScrollTo.getTop());
        sleep(1000);
        onView(withId(R.id.view_quote_button)).check(matches(isDisplayed()));
        onView(withId(R.id.view_quote_button)).perform(click());

        android.support.test.espresso.intent.Intents.intended(allOf(
                hasAdultsQuoteEntry(adultCount),
                hasChildrenEntry(childrenCount)));

        ...