【问题标题】:Google Espresso java.lang.RuntimeException: Could not launch intent Intent { act=android.intent.action.MAINGoogle Espresso java.lang.RuntimeException:无法启动意图 Intent { act=android.intent.action.MAIN
【发布时间】:2014-01-28 13:01: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());

        }

    }

【问题讨论】:

  • 发布你的代码你做了什么..

标签: android eclipse runtimeexception android-espresso


【解决方案1】:

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

在: 设置->
开发者选项-> 绘图

  1. 窗口动画缩放为关闭
  2. 将动画比例转换为关闭
  3. 动画持续时间缩放为关闭

【讨论】:

  • 我想知道是否有描述为什么会发生这种情况以及如何在不关闭动画的情况下解决这个问题
  • 我面临同样的问题。开发者选项中的动画已关闭。
【解决方案2】:

如果在创建 Activity 时有一个进度条在运行,则会出现这样的错误。您应该停止进度条以继续运行测试。

【讨论】:

  • 是的,这是正确的 - 可见 ProgressBar 可能是此异常的原因。检查您的活动/片段是否没有可见的ProgressBars(并且在使用带有 ProgressBar 的 alpha 时要小心)
【解决方案3】:

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

【讨论】:

    【解决方案4】:

    您的 Activity 中可能有动画,这会阻止 espresso 的执行。您必须禁用它 - 请参阅 https://github.com/googlesamples/android-testing/tree/master/ui/espresso/BasicSample

    【讨论】:

    • 当我按照上述解决方案时,我收到此错误“由于缺少权限而无法禁用动画”
    • 尝试添加请求权限,如上面链接的cmets中的状态
    【解决方案5】:

    我已经陷入这个问题好几个小时了。终于找到原因了。

    这对我有用。

    根据现象,这里有一些不同的原因。

    • 活动无法启动
    • 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)
        }
    

    第二种情况:Activity 启动但 UI 执行操作不起作用

    在这种情况下可能是因为

    1. 您的代码正在执行一些动画很长时间
    2. 您的代码正在执行您没有注意到的 UI 逻辑
      • 像处理程序发布事件或可运行
      • 在 ViewTreeObserver 中注册一个监听器,如 OnPreDrawListener 并且没有在适当的时机取消注册

    这些操作可能会导致 UI 线程繁忙,因此 espresso 无法进行测试

    【讨论】:

    • 除了断点和 println() 之外,关于如何调试第二种情况的任何提示? ;) 即阻塞 UI 逻辑....
    • 其实是因为活动无法启动,而你的第一个技巧奏效了,即添加了 CLEAR_TASK 和 NEW TASK 标志。非常感谢!几个月来一直在为此苦苦挣扎。虽然,我得到了下面的错误,这让我认为它更符合你的第二个解释。在下面发帖应该对其他人有帮助。
    • 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.)
    【解决方案6】:

    在我的例子中,自定义视图导致了这种行为。它包含一个不断滚动的Scroller。不幸的是,直到现在我还没有找到解决这个问题的方法,只是为了测试而禁用它......

    【讨论】:

    • 你是如何禁用它的?
    • 我告诉 View 这是一个自定义的,在测试运行时不要滚动。为了确定它是否正在运行,我使用了以下解决方案:stackoverflow.com/a/28701836/3734116
    【解决方案7】:

    在第一页你会调用太多的请求,这将花费超过 15 秒的时间,第一页应该很轻。 只需尝试创建一个新的欢迎页面,然后调用您原来的欢迎页面。 希望这对你有用。

    【讨论】:

      【解决方案8】:

      嗯,就我而言,这是由一件奇怪的事情引起的。

      我的一个 UI 测试打开了外部意图“android.app.action.CONFIRM_DEVICE_CREDENTIAL”,所以我决定将其存根。从现在开始,直到我从最近的任务中手动关闭由“android.app.action.CONFIRM_DEVICE_CREDENTIAL”打开的屏幕之前,MAIN Intent 才再次启动。

      不知道为什么会这样,现在也没有时间研究。也许以后我会更新这个帖子。

      【讨论】:

        【解决方案9】:

        当我尝试在用户单击给定视图时测试另一个活动的打开时遇到此错误。我做错的不是替换:

        @Rule
        public ActivityTestRule<MyActivity> myActivityActivityTestRule = new ActivityTestRule<>(MyActivity.class);
        

        每人:

        @Rule
        public IntentsTestRule<MyActivity> myActivityActivityTestRule =
                    new IntentsTestRule<>(MyActivity.class);
        

        【讨论】:

          【解决方案10】:

          我也遇到了这个问题,在我将物理设备更改为其他 Android 手机的那一刻,测试正在运行。只是尝试使用其他设备。并使用@rule 启动活动

          【讨论】:

            【解决方案11】:

            如果您在 MI 或 XIOMI 手机中执行此测试,那么它可能无法正常工作,因此您可以更换设备或使用 bluestack 模拟器。会成功的

            【讨论】:

              猜你喜欢
              • 2014-04-30
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2014-09-02
              • 2017-09-19
              相关资源
              最近更新 更多