简体   繁体   English

有没有办法知道Espresso是否已开始活动?

[英]Is there any way to know if an activity has been started with Espresso?

I'm doing an Activity transition testing with Espresso but I don't know if this is the best way to do it: 我正在使用Espresso进行活动转换测试,但我不知道这是否是最好的方法:

public void testStartLogin() {
    onView(withId(R.id.register)).perform(click());
    onView(withId(R.id.login_password)).check(matches(isDisplayed()));
    onView(withId(R.id.login_show_password)).check(matches(isDisplayed()));
}

The last two are from the second activity but this looks horrible for me. 最后两个来自第二个活动,但这对我来说太可怕了。 Is there any different way? 有什么不同的方式吗?

Asserting something against a view that belongs to a certain activity is in my opinion a very elegant way of checking if that specific activity has been started. 对于属于特定活动的视图,断言某些内容在我看来是一种检查特定活动是否已经开始的非常优雅的方式。 From the official docs: 来自官方文档:

At the same time, the framework prevents direct access to activities and views of the application because holding on to these objects and operating on them off the UI thread is a major source of test flakiness. 同时,该框架阻止直接访问应用程序的活动和视图,因为保持这些对象并在UI线程上对它们进行操作是测试片状的主要来源。 Thus, you will not see methods like getView and getCurrentActivity in the Espresso API. 因此,您不会在Espresso API中看到getView和getCurrentActivity等方法。

However, there is a way to accomplish what you need, like shown here . 然而,有一种方法来完成你所需要的,像显示在这里 In my version, I also defined an assertion method like: 在我的版本中,我还定义了一个断言方法,如:

 public void assertCurrentActivityIsInstanceOf(Class<? extends Activity> activityClass) {
    Activity currentActivity = getActivityInstance();
    checkNotNull(currentActivity);
    checkNotNull(activityClass);
    assertTrue(currentActivity.getClass().isAssignableFrom(activityClass));
}

which I used in the test methods. 我在测试方法中使用过的。

For my own tests I didn't have any issues using it (Espresso 2.0 !), but it made it somewhat redundant since I would still check the views belonging to that activity. 对于我自己的测试,我没有任何问题使用它(Espresso 2.0!),但它使它有点多余,因为我仍然会检查属于该活动的视图。 So it works, but I wouldn't recommend it. 所以它有效,但我不推荐它。

Good luck! 祝好运!

EDIT : 编辑

There is also the possibility of checking if the intent was sent from your first activity to the second one (check this short tutorial ), but that doesn't necessarily mean that the second activity displayed all of its views correctly. 还有可能检查意图是否从第一个活动发送到第二个活动(查看这个简短的教程 ),但这并不一定意味着第二个活动正确地显示了它的所有视图。 You should still check them being displayed, which brings you back to where you started. 您仍应检查它们是否正在显示,这会将您带回到您开始的位置。

Espresso authors discourage from accessing the actual activity to determine the state of the app. Espresso作者不鼓励访问实际活动以确定应用程序的状态。

Just like the real users have no idea what activity is in front of them, they determine where they are in the app by looking at the elements on the screen. 就像真实用户不知道他们面前有什么活动一样,他们通过查看屏幕上的元素来确定他们在应用程序中的位置。 I don't think that your current approach looks as horrible. 我认为你目前的做法看起来并不可怕。

However, espresso comes with ActivityLifecycleMonitor that tracks states of the activities. 但是,espresso附带了ActivityLifecycleMonitor ,用于跟踪活动的状态。 You can access it and perform you assertion just like this: 您可以访问它并执行断言,如下所示:

final Activity[] activity = new Activity[1];

InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
        @Override
        public void run() {
           activity[0] =Iterables.getOnlyElement(ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(Stage.RESUMED));
}

assertTrue(activity instanceof MyActivity.class);

Which doesn't look any better than your approach and it also might be flaky and subject to race conditions if your previous activity is doing any work after clicking the registration button. 哪个看起来不比你的方法好,如果你之前的活动在点击注册按钮之后做了任何工作,它也可能是不稳定的并且受比赛条件限制。

这个简短的片段应该工作:

intended(hasComponent(ExpectedActivity.class.getName()));

You can do with: 你可以这样做:

intended(hasComponent(new ComponentName(getTargetContext(), ExpectedActivity.class)));

Loot at response from @riwnodennyk 来自@riwnodennyk的回复

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

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