Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Espresso perform(click()) doesn't work

Tags:

I m writing tests for an Android application using espresso and mockito. In a few cases where I try to perform a click action on a view or button the click action doesn't register before the test is finished.

In activity:

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);
}

layout:

<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"/>

test:

@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")
    ));
}

The Log.i(TAG, "Leaving page with url: "+url); is never written in 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)
like image 512
elin Avatar asked Nov 23 '17 08:11

elin


1 Answers

This is not ideal and I view using sleep in a test as a failure, but if you must, you can add a sleep before the click to give the emulator time to settle. Note: I would only see the behavior described in the issue when using Firebase Test Cloud. All local emulators did not fail with this issue.

Adding a sleep before doing the button click allowed my test that always failed on the firebase test cloud to pass:

        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)));

        ...
like image 173
dazza5000 Avatar answered Sep 19 '22 13:09

dazza5000