android - android studio 中 Activity 开关的 Espresso 单元测试用例

标签 android junit4 android-espresso

我想使用 Espresso 编写单元测试用例,它应该检查用户是否成功地从注销页面( Activity )导航到登录页面。 如果有人知道这一点,请告诉我。如何检查用户是从 Activity A 导航到 Activity B 还是从一个 fragment 导航到另一个 fragment 。

最佳答案

从 Activity 1 开始,您可以按“导航”按钮并在创建的 Espresso 中使用 intended 来验证是否启动了 intent。

// Click on the item that starts navigation
onView(withId(R.id.buttonToGoActivity2)).perform(click());

// Check if intent with Activity 2 it's been launched
intended(hasComponent(Activity2.class.getName()));

需要gradle依赖:

androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2.2'

对于一个 fragment ,也许你可以在导航到它时检查是否显示了里面的 View

// Click on the item that starts navigation
onView(withId(R.id.buttonToShowFragment)).perform(click());

// wait for navigation delay
Thread.sleep(2000);

// Check that a view inside the fragment is shown
// Means navigaition to fragment is correct
onView(withId(R.id.viewInFragment)).check(matches(isDisplayed()));

关于android - android studio 中 Activity 开关的 Espresso 单元测试用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41467333/

相关文章:

java - 线性布局无法转换到工具栏

java - 当构造函数作为方法引用出现在流中时,如何模拟它?

Android Studio 1.0 - "local path doesn' t 存在”与外部库项目

android - 带光标的 AlertDialog

java - 在 JUnit4 和 Maven 中运行 TestSuite

android - 如何在 Espresso 上按键编辑器操作

android - Espresso 如何等待一段时间(1 小时)?

Android Espresso - 如何检查 EditText 提示?

java - 当 JSON 没有 "Name attribute"时如何使用 GSON ?

java - JUnit 在类之间传递参数