Issue
How do I assert an activity in Espresso after I click on a view item?
onView(withId(com.example.android.notepad.R.id.XYZ)).perform(click());
Solution
You should simulate the process of clicking a button and then test if the activity at the top of the stack is the one you're looking for
@RunWith(AndroidJUnit4.class)
public class YourTestClass{
@Test
public void testButton() {
Espresso.onView(ViewMatchers.withId(R.id.yourButtonId)).perform(ViewActions.click());
Assert.assertEquals(getActivityInstance().getClass(), YourActivityThatShouldStart.class);
}
private Activity getActivityInstance() {
final Activity[] currentActivity = {null};
InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
public void run() {
Collection resumedActivities = ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(Stage.RESUMED);
if (resumedActivities.iterator().hasNext()) {
currentActivity[0] = (Activity) resumedActivities.iterator().next();
}
}
});
return currentActivity[0];
}
}
On the testButton function, there are two lines, the first one to click on your button, the second one is to check the resulting activity
Espresso works on the main thread so your fine
Answered By - William Kinaan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.