【问题标题】:No activities found. Did you forget to launch the activity by calling getActivity() or startActivitySync or similar未找到任何活动。您是否忘记通过调用 getActivity() 或 startActivitySync 或类似方法来启动活动
【发布时间】:2019-08-28 04:31:40
【问题描述】:

当启动我的 android 应用程序时,首先启动 FirstActivity。 当我单击FirstActivity 上的按钮而不是启动AddTraderActivity。 如果我按下AddTraderActivity 中的“START REQUEST”按钮,那么我会调用:

setResult(RESULT_OK);
finish();

结果AddTraderActivity 被销毁并显示FirstActivity。 不错。

现在我想为 AddTraderActivityTest 编写 Espresso 的测试。 这里测试:

@RunWith(AndroidJUnit4::class)
@SmallTest
class AddTraderActivityTest {

    @get:Rule
    var addTraderActivity: IntentsTestRule<AddTraderActivity> =
        IntentsTestRule(AddTraderActivity::class.java)

    @Test
    fun toolBarHeight() {
        onView(withId(R.id.toolBar))
            .check(matches(withHeightResId(R.dimen.tool_bar_height)))
    }

    @Test
    fun buttonStartTextUppercase() {
        onView(withId(R.id.startButton))
            .check(matches(withTextUppercaseResId(R.string.start)))
    }
}

结果当我开始这个测试时,然后只启动AddTraderActivity 并且测试成功通过。 不错。

现在我想为点击按钮“START REQUEST”编写测试

这里测试:

@Test
fun pressButtonStartProgressBarDisplayed() {
    onView(withId(R.id.baseTextInputEditText)).perform(typeText("BASE_TEST"))
    onView(withId(R.id.quoteTextInputEditText)).perform(typeText("QUOTE_TEST"))
    onView(withId(R.id.startButton)).perform(click())

    onView(withId(R.id.containerProgressBarLayout)).check(matches(isDisplayed()))
}

当测试运行并按下按钮“START REQUEST”时,我得到下一个错误:

测试于 16:05 开始 ...

$ adb shell am instrument -w -r   -e debug false -e class 'com.myproject.AddTraderActivityTest#pressButtonStartProgressBarDisplayed' com.myproject.debug.test/androidx.test.runner.AndroidJUnitRunner
Client not ready yet..
Started running tests

java.lang.RuntimeException: No activities found. Did you forget to launch the activity by calling getActivity() or startActivitySync or similar?
at androidx.test.espresso.base.RootViewPicker.waitForAtLeastOneActivityToBeResumed(RootViewPicker.java:169)
at androidx.test.espresso.base.RootViewPicker.get(RootViewPicker.java:83)
at androidx.test.espresso.ViewInteractionModule.provideRootView(ViewInteractionModule.java:77)
at androidx.test.espresso.ViewInteractionModule_ProvideRootViewFactory.provideRootView(ViewInteractionModule_ProvideRootViewFactory.java:35)
at androidx.test.espresso.ViewInteractionModule_ProvideRootViewFactory.get(ViewInteractionModule_ProvideRootViewFactory.java:24)
at androidx.test.espresso.ViewInteractionModule_ProvideRootViewFactory.get(ViewInteractionModule_ProvideRootViewFactory.java:10)
at androidx.test.espresso.base.ViewFinderImpl.getView(ViewFinderImpl.java:62)
at androidx.test.espresso.ViewInteraction$2.call(ViewInteraction.java:276)
at androidx.test.espresso.ViewInteraction$2.call(ViewInteraction.java:268)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

我收到此错误是因为堆栈中没有活动FirstActivity,因为我的测试没有启动它。 所以问题是。 我怎样才能隔离只测试AddTraderActivity

【问题讨论】:

    标签: android android-espresso


    【解决方案1】:

    理想情况下,您可能应该使用单独的类来测试单独的活动。

    但是,如果您需要,您可以使用以下方法隔离每个方法的规则

    [编辑](注意:您可以不要再使用@get:Rule 注释):

    @Test
    public void myTest() {
          // launch desired activity
          var firstActivity: IntentsTestRule<FirstActivity> = IntentsTestRule(FirstActivity::class.java)
          firstActivity.launchActivity(Intent())
    
          // add tests
          onView(withId(R.id.baseTextInputEditText)).perform(typeText("BASE_TEST"))
          onView(withId(R.id.quoteTextInputEditText)).perform(typeText("QUOTE_TEST"))
    }
    

    【讨论】:

    • 现在我收到错误:java.lang.IllegalStateException: #init 连续调用了两次。确保在每个 #init 之后调用 #release
    • 是的,对不起,我应该具体一点,您可以再使用@get:Rule 注释。它也被称为您的第二种方法。正在更新答案。
    • 我试试这个:Rule JvmField var jsonViewActivity = ActivityTestRule(JsonViewActivity::class.java) Intents.init() expected(hasComponent(JsonViewActivity::class.java.getName())),但我得到错误:androidx.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError:想要匹配 1 个意图。实际上匹配了 0 个意图。
    • 什么是 IntentsTestRule?
    【解决方案2】:

    如果您从ActivityTestRule 迁移到ActivityScenarioRule(或ActivityScenario)并且您尝试测试在其onCreate() 方法中具有以下代码的活动,则可能会发生此问题:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
    
      // Workaround the Android bug of multiple Activity task stacks when
      // the app is launched from the Google Play Store. See:
      // https://issuetracker.google.com/issues/36941942
      // https://stackoverflow.com/questions/4341600/multiple-instances-of-activity-from-google-play-launch
      // https://stackoverflow.com/questions/19545889/app-restarts-rather-than-resumes
      if (!isTaskRoot() &&
          getIntent().hasCategory(Intent.CATEGORY_LAUNCHER) &&
          Intent.ACTION_MAIN.equals(getIntent().getAction())) {
        finish();
        return;
      }
    
      ...
    }
    

    这段代码在ActivityTestRule 上运行良好。但它不适用于ActivityScenarioRuleActivityScenario,因为它在尝试启动 Activity 时会导致错误:

    问题发生是因为 Activity 已启动,然后在 onCreate() 中立即关闭。所以在使用ActivityScenarioRuleActivityScenario 时必须删除整个if 块。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-28
      • 2016-02-24
      • 2018-07-08
      • 1970-01-01
      • 2013-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多