我想使用 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/