Question

I am trying to use Testdroid which is backed by Robotium.

Background: FragmentStatePagerAdapter does not assign tag values for fragments (unlike FragmentPagerAdapter) so waitForFragmentByTag won't work; as a result I use waitForFragmentById, which I am unable to get working as expected.

My fragments are defined in xml (fragment.xml) which defines the UI structure and layout. My fragments are also defined in code (Fragment.java), which is where listeners are registered etc.

I have debugged through the ExtSolo source code and when the Waiter calls the fragmentManager (both) null values are returned for both the ID defined in the @id tag of the parent element in the XML (R.id.fragment) and also the ID defined by the file itself (R.layout.fragment).

Are these the correct ID values to be using? If not, what is? Any help greatly appreciated.

Était-ce utile?

La solution 2

I ended up using a nasty work around which will cause tests to fail if structure changes:

private void assertAtHomeFragment() {
    assertTrue(solo.waitForView(ListView.class, 1, 2000));
    assertTrue(solo.waitForView(ImageButton.class, 3, 2000));
    assertTrue(solo.waitForView(Button.class, 1, 2000));
    solo.sleep(1000);
}

private void assertAtLibraryFragment() {
    assertTrue(solo.waitForView(ListView.class, 1, 2000));
    assertTrue(solo.waitForView(ImageButton.class, 0, 2000));
    solo.sleep(1000);
}

Autres conseils

Robotium bases on FragmentManager and method findFragmentById/findFragemntByTag. With sample application Support4Demos, I checked fragments in State Pager, what seems to be similar to your context. Using this code:

List<Fragment> fragList = new ArrayList<Fragment>();

@Override
public void onAttachFragment (Fragment fragment) {
    if (!fragList.contains(fragment)) {
        fragList.add(fragment);
        Log.d("TEST", "Fragment id ("+ fragment.getId() +")");
        if (fragment.getId() != View.NO_ID && fragment.getId() != 0) {
            String id = fragment.getResources().getResourceEntryName(
                    fragment.getId());
            Log.d("TEST", "Fragment id " + id);
        }
    }
}

you can check, what id your fragments have. Unfortunately they all have id of ViewPager or id = 0, so it seems to be impossible to distinguish them by id. Don't know, if it will help you, but I hope so.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top