kumar
kumar

Reputation: 780

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. 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.

Upvotes: 1

Views: 2388

Answers (1)

adalpari
adalpari

Reputation: 3121

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.

// 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:

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

Upvotes: 3

Related Questions