Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Espresso - How can I check if an activity is launched after performing a certain action?

the following is one of my Espresso test cases.

    public void testLoginAttempt() {         Espresso.onView(ViewMatchers.withId(R.id.username)).perform(ViewActions.clearText()).perform(ViewActions.typeText("[email protected]"));         Espresso.onView(ViewMatchers.withId(R.id.username)).perform(ViewActions.clearText()).perform(ViewActions.typeText("invalidpassword"));          Espresso.onView(ViewMatchers.withId(R.id.login_button)).perform(ViewActions.click());         // AFTER CLICKING THE BUTTON, A NEW ACTIVITY WILL POP UP.         // Clicking launches a new activity that shows the text entered above. You don't need to do         // anything special to handle the activity transitions. Espresso takes care of waiting for the         // new activity to be resumed and its view hierarchy to be laid out.         Espresso.onView(ViewMatchers.withId(R.id.action_logout))                 .check(ViewAssertions.matches(not(ViewMatchers.isDisplayed())));      } 

Currently what I did was to check if a view in the new activity (R.id.action_logout) is visibible or not. If visible, I will assume tha the activity opened successfully. But it doesn't seem to work as I expected. Is there a better way to check if a new activity is successfully launched instead of checking a view in that activity is visible? Thanks

like image 467
user2062024 Avatar asked Sep 23 '14 15:09

user2062024


People also ask

How do you test espresso intent?

Validate intentsUsing the intended() method, which is similar to Mockito. verify() , you can assert that a given intent has been seen. However, Espresso-Intents doesn't stub out responses to intents unless you explicitly configure it to do so. // User action that results in an external "phone" activity being launched.

Does espresso support test recording?

The Espresso Test Recorder tool lets you create UI tests for your app without writing any test code. By recording a test scenario, you can record your interactions with a device and add assertions to verify UI elements in particular snapshots of your app.

What is activity test rule?

public ActivityTestRule (Class<T> activityClass, boolean initialTouchMode) Similar to ActivityTestRule but defaults to launch the activity under test once per Test method. It is launched before the first Before method, and terminated after the last After method.


2 Answers

You can use:

intended(hasComponent(YourExpectedActivity.class.getName())); 

Requires this gradle entry:

androidTestCompile ("com.android.support.test.espresso:espresso-intents:$espressoVersion") 

The import for the intended() and hasComponent()

import static android.support.test.espresso.intent.Intents.intended; import static android.support.test.espresso.intent.matcher.IntentMatchers.hasComponent; 

as mentioned by Shubam Gupta please remember to call Intents.init() before calling intended(). You can eventually call it in the @Before method.

like image 64
baskara Avatar answered Sep 22 '22 09:09

baskara


Try:

intended(hasComponent(YourActivity.class.getName()));

Also, keep in mind

java.lang.NullPointerException is thrown if Intents.init() is not called before intended()

like image 33
Shubham Gupta Avatar answered Sep 18 '22 09:09

Shubham Gupta