1

I have a layout file map_layout.xml in my android application. which holds MapFragment. I want to use this fragment in my test project using Robotium. Objective is to click the markers for testing purpose.

I am getting the ID but not able to get the fragment instance. Anyone who can help?

int id = activity.getResources().getIdentifier("googleMap","id",solo.getCurrentActivity().getPackageName());
Log.d(TAG, "My ID is..." + id);

Log.d(TAG, "Frag is..." + activity.getFragmentManager().findFragmentById(id));
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
ms ghotra
  • 59
  • 1
  • 6
  • Have you tried using the full name of the app's R class? For example activity.getFragmentManager().findFragmentById(com.company.app.R.id. googleMap); – Dave C Jun 01 '14 at 04:35
  • I tried using that but... there is no reference to R file from the test project. – ms ghotra Jun 01 '14 at 05:32

1 Answers1

0

Try using:

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import android.content.Context;
import android.test.ActivityInstrumentationTestCase2;
import android.test.suitebuilder.annotation.MediumTest;

import com.robotium.solo.Solo;

public class FragmentTest extends ActivityInstrumentationTestCase2<YourActivity> {

    protected final CountDownLatch signal = new CountDownLatch (1);
    protected Context context;
    protected YourActivity activity;
    protected YourFragment fragment;
    protected Solo solo;

    public FragmentTest () {

        super(YourActivity.class);
    }

    @MediumTest
    public void testFragment () {

        int fragmentId = yourfragmentId;

        solo.waitForFragmentById (fragmentId);

        waitForTime (1); //wait till your fragment is loaded

        fragment = (YourFragment) activity.getFragmentManager ().findFragmentById (fragmentId);

        waitForTime (100);
    }

    @Override
    protected void setUp () throws Exception {

        super.setUp ();

        this.context = getInstrumentation ().getTargetContext ();

        prepareTestData ();

        activity = getActivity ();

        solo = new Solo (getInstrumentation (), activity);
    }

    protected void waitForTime (long seconds) {

        try {
            signal.await (seconds, TimeUnit.SECONDS);
        }
        catch (InterruptedException e) {
            e.printStackTrace ();
        }
    }

    protected void prepareTestData () {

    }
}
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53