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?
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()));
Answered By – Daniel Lubarov