简体   繁体   中英

How to load resources to test project which is using robotium?

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));

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 () {

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM