0

If one of my fragments, rattingfragment, I have to perform click. If not, do other action.

How I know this is the fragment on my screen?

Android UI Tests. Below my code to test EDIT CODE:

   @Rule
    public ActivityTestRule mActivityRule = new ActivityTestRule<>(MainActivity.class);

    @After
    public void setUp(){
        Logout();
    }  
  @Test
    public void shouldOfferRide(){
        SystemClock.sleep(1500);
        Login();
        onView(withId(R.id.searchButton)).perform(typeText(emailSearch));
        onView(withId(R.id.searchButton)).perform(pressImeActionButton());
        onView(withId(R.id.community_user_ask_button)).perform(click());
        SystemClock.sleep(1500);
        onView(withId(R.id.button1)).perform(click());
        onView(withId(R.id.send_request)).perform(click());
        SystemClock.sleep(1500);
        ((MainActivity)mActivityRule.getActivity()).navItemClick(4);
        SystemClock.sleep(3500);
        LoginMyrides();
        onView(withId(R.id.my_rides_tab_bar)).perform(click());
        SystemClock.sleep(1000);

        {I have to know is ratingfragment before perform click below}

        onView(withId(R.id.rating_bar)).perform(click());
        onView(withId(R.id.deny_btn)).perform(click());
    }

        public void Login(){
        onView(withId(R.id.edt_new_login_email)).perform(typeText(emailLogin));
        onView(withText(R.string.next_button)).perform(click());
        onView(withId(R.id.edt_new_password)).perform(typeText(senha));
        onView(withText(R.string.login_new_pass)).perform(click());
    }

    public void Logout(){
        new SessionManager(mActivityRule.getActivity()).logoutUser();
    }
}
  • There are many solution for this case, but the best is the instanceOf method through which you can get what current fragment is. use it like if(currentFragmet instanceOf rattingfragment){} – HAXM Sep 29 '16 at 20:11
  • http://stackoverflow.com/a/24589081/4961126 – HAXM Sep 29 '16 at 20:14

3 Answers3

0
If you want to know it in activity, then just use:

Fragment fragment = getFragmentManager().findFragmentByTag(stringTag);
if (fragment != null) { 
       //here it means the fragment is been shown
}

And If you want to know it in fragment then do your stuff in fragment's any of the following life cycle method:


onAttach(); 
onCreate(); 
onCreateView(); 
onActivityCreated(); 
onStart(); //or
onResume();

These will get called as soon as your fragment will get shown on screen. Reference: https://developer.android.com/guide/components/fragments.html#Creating

mithil1501
  • 506
  • 9
  • 20
0

Espresso was designed for tests which can follow a step by step recipe without any decision making. You should consider situations when the fragment will be visible and when it will not be. Then write separate tests for each scenario.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
-1

Use the following function in your fragment :

 @Override
 public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);

    if (isVisibleToUser) {
        //fragment is visible on screen...do your stuff here
    } 
    else {
        // fragment is no longer visible
    }
 }
Ayan
  • 8,192
  • 4
  • 46
  • 51