Google Espresso java.lang.RuntimeException: 无法启动意图 Intent { act=android.intent.action.MAIN }

12

我是Espresso UI测试的新手。

在运行测试时(ADT Eclipse IDE),我遇到了这个错误。

该应用程序已经开发完毕,在启动应用程序时会有大量请求。无法重写该应用程序,但我需要找到一种方式来测试UI,即使组件加载存在延迟。

        java.lang.RuntimeException: Could not launch intent Intent { act=android.intent.action.MAIN flg=0x14000000 cmp=com.xx.android/com.yy.core.android.map.MapActivity } 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 1390913271702 and and now the last time the queue went idle was: 1390913271767. If these numbers are the same your activity might be hogging the event queue.
        at com.google.android.apps.common.testing.testrunner.GoogleInstrumentation.startActivitySync(GoogleInstrumentation.java:277)
        at android.test.InstrumentationTestCase.launchActivityWithIntent(InstrumentationTestCase.java:119)
        at android.test.InstrumentationTestCase.launchActivity(InstrumentationTestCase.java:97)
        at android.test.ActivityInstrumentationTestCase2.getActivity(ActivityInstrumentationTestCase2.java:104)
        at com.gulesider.android.test.UItest.setUp(UItest.java:25)
        at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
        at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
        at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
        at com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner.onStart(GoogleInstrumentationTestRunner.java:167)
        at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1799)
  1. 我有一个名为“Core”的库项目 - 它不会生成任何.apk文件。
  2. 我还有一个名为“AA”的Android项目,它将访问“Core” - 这是AA.apk。
  3. 现在我创建了一个名为“UItest”的测试项目。

清单:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.AA.android.test"
        android:versionCode="1"
        android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="8" 
            android:targetSdkVersion="18" />
    <instrumentation
      android:name="com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
      android:targetPackage="com.AA.android"/>
    <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name" >
    <activity
                android:name="com.core.android.map.MapActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <uses-library android:name="android.test.runner" />
        </application>
    </manifest>

我的测试:

    public class UItest extends ActivityInstrumentationTestCase2<MapActivity> {
        public UItest() {
            super(MapActivity.class);
        }

        @Override
        public void setUp() throws Exception {
            super.setUp();

            getActivity();

        }

        public void testSearchBox() {

            Espresso.onView(ViewMatchers.withId(R.id.menu_button_logo)).perform(ViewActions.click());

        }

    }

3
请发布你的代码,展示你所做的工作。 - Shailendra Madda
12个回答

12

对于Espresso测试,强烈建议您在用于测试的虚拟或物理设备上关闭系统动画。因此,您可以按照以下步骤手动关闭动画:

在: 设置->
开发者选项-> 显示

  1. 将窗口动画缩放设置为OFF
  2. 将转换动画缩放设置为OFF
  3. 将动画器持续时间缩放设置为OFF

2
我想知道为什么会发生这种情况以及如何在不关闭动画的情况下解决它的描述。 - Roger Alien
12
我遇到了同样的问题。在开发者选项中已经关闭了动画。 - Inderdeep Singh

5
如果在创建活动时有进度条正在运行,您会收到以下错误。您需要停止进度条以继续运行测试。

是的,这是正确的 - 可见的 ProgressBar 可能是这个异常的原因。请检查您的活动/片段是否没有可见的 ProgressBar(并且在使用 ProgressBar 时要小心 alpha 值)。 - xCh3Dx

4

我卡在这个问题上了几个小时。最后,我找到了原因。

对我有用的解决方案如下。

根据现象,以下是一些不同的原因:

  • 无法启动Activity
  • Activity已启动,但UI操作无效

第一个情况:无法启动Activity

可能是由于您的目标Activity已经在activity堆栈中。添加一个CLEAR标志和NEW_TASK标志

    @get:Rule
    val activityRule = ActivityTestRule<MainActivity>(MainActivity::class.java)

    private lateinit var launchedActivity: MainActivity

    @Before
    fun setUp() {
        val intent = Intent(Intent.ACTION_PICK)
        //this is the key part
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
        //this is the key part
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        launchedActivity = activityRule.launchActivity(intent)
    }

第二种情况:启动了活动但UI执行操作不起作用
在这种情况下,可能是由于以下原因之一:
1.您的代码执行了一些长时间的动画。
2.您的代码正在执行您没有注意到的UI逻辑,例如:
a.使用Handler post事件或runnable。 b.在ViewTreeObserver中注册一个监听器(如OnPreDrawListener),但未在适当的时间注销。
这些操作可能导致UI线程繁忙,因此espresso无法进行测试。

1
有没有关于如何调试第二种情况的提示,除了使用断点和println()之外?例如:阻塞UI逻辑... - xavierdominguez
实际上,这是因为无法启动该活动,而您的第一个技巧起了作用,即添加CLEAR_TASK和NEW_TASK标志。非常感谢!我已经苦苦挣扎了几个月。虽然我一直在收到下面的错误,这让我认为它更多地沿着您的第二个解释方向。如果有帮助其他人,我会将其发布如下。 - xavierdominguez
java.lang.RuntimeException(Could not launch intent { flg=0x10000000 cmp=com.fake.package.id.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. ... The last time the event queue was idle before your activity launch request was 1604665354699 and now the last time the queue went idle was: 1604665354766. If these numbers are the same your activity might be hogging the event queue.) - xavierdominguez

3

我在运行Espresso测试时,在6.0设备上遇到了此错误,但在5.1.1或7.0设备上没有。 我追踪到问题的原因是在样式中使用了android:fadeScrollbars。从我的样式中删除此项解决了该问题。


2

如果您正在使用小米手机或者MIUI系统进行此项测试,可能会无法正常运行,您可以更换设备,或者使用Bluestacks模拟器,这样就可以正常运行了。


2

当我按照上述解决方案操作时,出现了以下错误:“无法禁用动画,因为权限不足”。 - user3241003
尝试添加请求权限,如上面链接的评论中所述。 - Bolhoso

1
在我的情况下,一个自定义视图导致了这种行为。它包含一个不断滚动的Scroller。不幸的是,我到目前为止还没有找到解决这个问题的方法,除了在测试时禁用它...

你是如何禁用它的? - anuja jain
我告诉了这个视图(它是自定义的),当测试运行时不要滚动。为了确定是否正在运行,我使用了以下解决方案:https://dev59.com/KF4b5IYBdhLWcg3w61V3#28701836 - luckyhandler

0

嗯,在我的情况下,这是由一个奇怪的事情引起的。

我的一个UI测试打开了外部意图“android.app.action.CONFIRM_DEVICE_CREDENTIAL”,所以我决定对其进行存根。从现在开始,除非我手动关闭由“android.app.action.CONFIRM_DEVICE_CREDENTIAL”打开的屏幕,否则MAIN意图不会再次启动。

不知道为什么会发生这种情况,现在没有时间进行研究。也许以后我会更新这个帖子。


0

在第一页上,您将发起太多请求,这将花费超过15秒的时间,第一页应该非常轻量级。 尝试创建一个新的欢迎页,然后调用您的原始欢迎页。 希望对您有所帮助。


0

我也遇到了这个问题,当我将物理设备更换为其他安卓手机时,测试就可以正常运行了。尝试使用其他设备,并使用@rule来启动活动。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接