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

问题描述 投票:0回答: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
12个回答
12
投票

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

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

  1. 窗口动画缩放到关闭
  2. 过渡动画比例到 OFF
  3. 动画师持续时间缩放到关闭

5
投票

如果创建活动时有进度条在运行,就会出现这样的错误。为了继续运行测试,您应该停止进度条。


3
投票

我在 6.0 设备上运行 Espresso 测试时遇到了这个错误,但在 5.1.1 或 7.0 设备上没有。我将原因追溯到在样式中使用

android:fadeScrollbars
。从我的风格中删除这个项目解决了这个问题。


3
投票

我在这个问题上纠结了几个小时。终于找到原因了

这对我有用。

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

  • 活动无法启动
  • Activity 已启动,但 UI 执行操作不起作用

第一种情况:活动无法启动

因为你的目标活动可能已经在活动堆栈中。 添加

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 逻辑
    • 像 Handler post event 或 runnable
    • 在 ViewTreeObserver 中注册一个侦听器,如 OnPreDrawListener,但没有在适当的时间取消注册

这些操作可能会导致UI线程繁忙,所以espresso不能做测试.


2
投票

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


1
投票

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


1
投票

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


0
投票

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


0
投票

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

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

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


0
投票

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

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

每个:

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

0
投票

我也有这个问题,当我把物理设备换成其他安卓手机时,测试是有效的。只是尝试使用其他设备。并使用@rule 启动活动


0
投票

就我而言,授权请求返回 401 错误。这就是为什么登录活动继续显示,而 MainActivity 没有启动的原因。无法通过测试修复。

© www.soinside.com 2019 - 2024. All rights reserved.