Espresso test on a Capacitor app shows "Could not launch intent Intent within 45 seconds"

3

I'm trying to implement a Espresso test with a Capacitor app. The test looks like this:

package com.getcapacitor.myapp;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import android.content.Intent;

import androidx.test.espresso.web.webdriver.DriverAtoms;
import androidx.test.espresso.web.webdriver.Locator;
import androidx.test.filters.LargeTest;
import androidx.test.rule.ActivityTestRule;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import ekt.moveus.applikate.MainActivity;

import static androidx.test.espresso.web.sugar.Web.onWebView;
import static androidx.test.espresso.web.webdriver.DriverAtoms.clearElement;
import static androidx.test.espresso.web.webdriver.DriverAtoms.findElement;
import static androidx.test.espresso.web.webdriver.DriverAtoms.webClick;

/**
 * Instrumented test, which will execute on an Android device.
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
@LargeTest
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {

  @Rule
  public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<MainActivity>(
    MainActivity.class, false, true) {
    @Override
    protected void afterActivityLaunched() {}
  };

  private final static String USER_EMAIL = "email@gmail.com";
  private final static String USER_PASS = "password";

  @Test
  public void typeTextInInput_clickButton_SubmitsForm() {
    Intent webFormIntent = new Intent();
    mActivityRule.launchActivity(webFormIntent);
    onWebView()
      .withElement(findElement(Locator.NAME, "ion-input-1"))
      .perform(clearElement())
      .perform(DriverAtoms.webKeys(USER_EMAIL))

      .withElement(findElement(Locator.NAME, "ion-input-0"))
      .perform(clearElement())
      .perform(DriverAtoms.webKeys(USER_PASS))

      .withElement(findElement(Locator.NAME, "ion-input"))
      .perform(webClick());
  }
}

But when I run the test case I get this error:

java.lang.RuntimeException: Could not launch intent Intent { flg=0x10000000 cmp=ekt.moveus.applikate/.MainActivity } within 45 seconds. Perhaps the main thread has not gone idle within a reasonable amount of time? There could be an animation or something constantly repainting the screen. Or the activity is doing network calls on creation? See the threaddump logs. For your reference the last time the event queue was idle before your activity launch request was 1594199984877 and now the last time the queue went idle was: 1594200024655. If these numbers are the same your activity might be hogging the event queue.
at androidx.test.runner.MonitoringInstrumentation.startActivitySync(MonitoringInstrumentation.java:481)
at androidx.test.rule.ActivityTestRule.launchActivity(ActivityTestRule.java:358)
at com.getcapacitor.myapp.ExampleInstrumentedTest.typeTextInInput_clickButton_SubmitsForm(ExampleInstrumentedTest.java:44)
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.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.ext.junit.runners.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:2209)

As far as I know and after reading some questions and tutorials, it can be caused by an animation (I´ve disabled them on the emulator), a constant redrawing of some component or a custom view. Since Capacitor works under the hood, I don't really know what's causing the issue.

Anyone faced this? How can I correctly implement an Espresso test case with a Capacitor App?

Thanks

android
ionic-framework
android-espresso
capacitor
asked on Stack Overflow Jul 8, 2020 by axierjhtjz • edited Jul 8, 2020 by axierjhtjz

1 Answer

1

The problem is that you're launching the activity twice. First you set up the rule to start the activity right away (by having the third parameter as true in new ActivityTestRule<MainActivity>(MainActivity.class, false, true), and then in the test, you explicitly launch the activity again (by doing mActivityRule.launchActivity(webFormIntent);).

Set the third param to false instead.

answered on Stack Overflow Aug 6, 2020 by gosr

User contributions licensed under CC BY-SA 3.0