有没有什么办法知道,如果活动已经开始以E preSSO? [英] Is there any way to know if an activity has been started with Espresso?

查看:149
本文介绍了有没有什么办法知道,如果活动已经开始以E preSSO?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做以E preSSO一个活动转换测试,但我不知道这是否是做的最好的方式:

I'm doing an Activity transition testing with Espresso but I don't know if this is the best way to do it:

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:

与此同时,该框架prevents直接访问活动和观点的应用程序,因为持有这些对象并对其关闭UI线程操作是测试片状的主要来源。因此,你不会看到长者preSSO API像getView和getCurrentActivity方法。

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. Thus, you will not see methods like getView and getCurrentActivity in the Espresso API.

不过,有一种方法来完成你需要的东西,像所示的此处。在我的版本,我还定义了一个断言方法,如:

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.

有关我自己的测试中,我没有使用它的任何问题(ES preSSO 2.0!),但它使之显得有些多余,因为我仍然会检查属于该活动的意见。所以它的工作原理,但我不会推荐它。

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. So it works, but I wouldn't recommend it.

祝你好运!

修改

还有一个检查,如果意图是从你的第一个活动发送到第二个的可能性(检查<一href=\"http://www.vogella.com/tutorials/AndroidTestingEs$p$psso/article.html#ex$p$psso_mockingintents\"相对=nofollow>这个简短的教程),但是,这并不一定意味着第二个活动显示其所有的观点正确。你还是应该检查它们被显示,这使你回到你开始的地方。

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.

这篇关于有没有什么办法知道,如果活动已经开始以E preSSO?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆