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

Asked

Viewed 64 times

0

I would like a tip on how to check if it is the correct Fragment after the click

Follow the thought (Wrong) for example, someone has 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, I must extend the Appcompactivity?

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 your Ragments, and compare them to the Fragments names.

  • Oops, thank you! But that’s not the reasoning. I want to know, if clicking will be the correct Fragment. I’m automating interface tests.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.