Skip to content Skip to sidebar Skip to footer

How Do I Get A Reference To A Fragment From Within A Unit/robotium Test?

How do I get a reference to a fragment within Robotium unit test. In my case the fragement houses a WebView. I want to use solo to get reference to the fragment? How can this be

Solution 1:

I have been away from robotium (and SO) for a little while but you used to be able to do the following because robotium has no inbuilt way to get the fragment.

The first step is you need the current activity which is easy enough:

Activitycurrent= solo.getCurrentActivity();

If you look over the Android activity API you will notice it has the method getFragmentManager() and then if you follow that you will find methods named findFragmentByXXX();

so the code becomes:

Fragment fragment = solo.getCurrentActivity().getFragmentManager().findFragmentByID(xxx);

you will then have your fragment!

There is one issue with this potentially though, some activities do not have the getFramentManager() method they instead have the method getSupportFragmentManager() which works the same but gave fragment support to older devices, so you may need to use one or the other. If you need to support both you will have to do some reflection to determine which method to call unfortunately.

Post a Comment for "How Do I Get A Reference To A Fragment From Within A Unit/robotium Test?"