简体   繁体   English

适用于Android Studio中活动开关的Espresso单元测试用例

[英]Espresso unit test case for Activity switch in android studio

I want to write unit test case using Espresso that should check whether the user succesfully navigated from logout page(Activity) to Login Page. 我想使用Espresso编写单元测试用例,该用例应检查用户是否成功从注销页面(活动)导航到登录页面。 Please let me know if anyboday know this. 请让我知道是否任何一天都知道这一点。 How to check whether User navigated from Activity A to Activity B or from one fragment to another. 如何检查用户是从活动A导航到活动B还是从一个片段导航到另一个片段。

Starting in Activity 1, you can press the "navigation" button and use intended in Espresso which it's been created to verify that an intent is launched. 在活动1开始,您可以按“导航”按钮,使用intended在它已经创建,以验证意图启动咖啡。

// 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 dependency needed: gradle依赖需要:

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

For a Fragment, maibe you can check if a view inside is showed when navigate to it 对于片段,您可以在导航到片段时检查是否显示了内部视图

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

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

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