简体   繁体   English

Capacitor 应用程序上的 Espresso 测试显示“无法在 45 秒内启动 Intent Intent”

[英]Espresso test on a Capacitor app shows “Could not launch intent Intent within 45 seconds”

I'm trying to implement a Espresso test with a Capacitor app.我正在尝试使用 Capacitor 应用程序实施 Espresso 测试。 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.据我所知,在阅读了一些问题和教程后,它可能是由 animation(我在模拟器上禁用它们)、某些组件的不断重绘或自定义视图引起的。 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?如何使用 Capacitor App 正确实施 Espresso 测试用例?

Thanks谢谢

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); ).首先,您设置规则以立即启动活动(通过在new ActivityTestRule<MainActivity>(MainActivity.class, false, true) true然后在测试中,您再次明确启动活动(通过做mActivityRule.launchActivity(webFormIntent); )。

Set the third param to false instead.将第三个参数设置为false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM