gpt4 book ai didi

android - 有什么方法可以知道 Espresso 是否已经开始一项 Activity ?

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:54:24 25 4
gpt4 key购买 nike

我正在使用 Espresso 进行 Activity 转换测试,但我不知道这是否是最好的方法:

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()));
}

最后两个来自第二个 Activity ,但这对我来说看起来很糟糕。有什么不同的方法吗?

最佳答案

在我看来,针对属于特定 Activity 的 View 断言某些内容是检查该特定 Activity 是否已启动的一种非常优雅的方式。来自官方文档:

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.

但是,有一种方法可以满足您的需求,如图所示 here .在我的版本中,我还定义了一个断言方法,例如:

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

我在测试方法中使用了它。

对于我自己的测试,我在使用它时没有遇到任何问题(Espresso 2.0!),但它有点多余,因为我仍然会检查属于该 Activity 的 View 。所以它有效,但我不推荐它。

祝你好运!

编辑:

还有可能检查 Intent 是否从您的第一个 Activity 发送到第二个 Activity (检查 this short tutorial ),但这并不一定意味着第二个 Activity 正确显示了它的所有 View 。您仍应检查它们的显示情况,这会将您带回到开始的地方。

关于android - 有什么方法可以知道 Espresso 是否已经开始一项 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30891794/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com