Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine fragment change while espresso testing

I'm testing my application in android composed of 1 main activity and multiple fragments inside in which we navigate.

For my tests I use espresso, and after a click on a particular button, I want to check if the current fragment has changed or not (the part with the button is ok).

So, how can I do in espresso to check if the fragment is still the same than before the click on the button?

like image 224
Gaétan Séchaud Avatar asked Dec 09 '14 00:12

Gaétan Séchaud


1 Answers

If you really want to do this, you can do

FragmentManager fragmentManager = getActivity().getFragmentManager()

and get information about the backstack from fragmentManager. If your fragment has a tag, you could assert that findFragmentByTag(tag) returns something non-null.

But it's normally better to make assertions about the view hierarchy. It's what Espresso was designed for, and it's more in the spirit of black-box testing.

So I would suggest finding some distinguishing feature of the new fragment, such as the page title if there is one, and asserting that that view is present using the usual Espresso methods, e.g.

onView(withText("Page Two Title")).check(matches(isDisplayed()));
like image 161
Daniel Lubarov Avatar answered Sep 28 '22 10:09

Daniel Lubarov