Check if it is the correct fragment after the click (UI Test)

0

I would like a hint on how to check for the correct fragment after the click

Follow the (Wrong) thought for example, does anyone have an example of how I can test this?

@RunWith(AndroidJUnit4.class)
public class MapsActivityTest extends AppCompatActivity {

    FragmentManager fragmentManager = getSupportFragmentManager();
    Fragment fragment = fragmentManager.findFragmentByTag(getResources().getString(R.string.fragment_routes_title));

    @Test
    @SmallTest
    public void showUIMap() {

        onView(withId(R.id.request_textview)).perform(click()).check(assertThat(fragment.getActivity().getSupportFragmentManager()
                .findFragmentByTag("MyRides)"),is(true));

    }
}

Another question, should I extend AppCompactivity?

    
asked by anonymous 26.08.2016 / 16:16

1 answer

-1

You need to do the following:

FragmentManager fm = getSupportFragmentManager();
            if (fm != null) {
                List<Fragment> fragments = fm.getFragments();

                if(fragments != null){
                    for(int i = fragments.size() - 1; i >= 0; i--){
                        final Fragment fragment = fragments.get(i);
                        if(fragment != null) { 
                            if(fragment instanceof NomeDoSeuFragment) {
                                // caso o fragment seja esse faça algo aqui dentro

                            }else if (fragment instanceof NomeDoSeuFragment2){
                                // caso o fragment seja esse faça algo aqui dentro
                            }                       
                        break;
                        }
                    }
                }
            }

This looping will scan all of your fragments, and compare them with the Fragment names.

    
26.08.2016 / 16:24