6

I am working on the project. I start learning instrumental tests using Espresso to write UI testing. I have a bottom view each bottom view tab has a parent fragment & their child fragments. I have done one wrong fragment transaction i.e when navigating toward home bottom view tab from services bottom view tab home is selected but showing services fragment.

I want to test when clicking on the home bottom view tab the home fragment is added or not. I have tried the code like this but it is passing on the wrong case as well

I have also go through the below StackOverflow posts but not working

Determine fragment change while espresso testing

How to test if a fragment is visible with Espresso

@Test
fun testHomeFragment_isDisplayed() {

    //click on home navigation button of bottom view
    onView(withId(R.id.navigation_home)).perform(click())

    //click on services navigation button of bottom view
    onView(withId(R.id.navigation_more)).perform(click())

    //click on recycler view item at given position
    onView(withId(R.id.more_rv)).perform(
            RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>(
                    1,
                    MyViewAction.clickChildViewWithId(R.id.row_more_parent)));

    //Again click on home navigation button of bottom view
    onView(withId(R.id.navigation_home)).perform(click())

    //check that the home fragment is displayed or not.
    onView(allOf(withId(R.id.home_pager), withParent(withId(R.id.papa)))).
            check(matches(isCompletelyDisplayed()))

}

How to know which fragment is currently displayed on the screen?

Fazal Hussain
  • 1,129
  • 12
  • 28
  • any solution for this? – sak Sep 03 '20 at 08:34
  • I have not found any solution for this – Fazal Hussain Sep 04 '20 at 09:07
  • As a hack you might to add an extra view in each fragment and check if it is displayed like onView(withId(R.id.first_fragment_view)).check(matches(isDisplayed())). If you keep this view small as 1x1dp with transparent background it would not break your UX. – Joe Dow Jun 29 '22 at 13:11

0 Answers0