1

我的 android 应用程序中有一个布局文件map_layout.xml。持有MapFragment。我想在使用 Robotium 的测试项目中使用这个片段。目的是单击标记进行测试。

我正在获取 ID,但无法获取片段实例。有谁能帮忙吗?

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

1 回答 1

0

尝试使用:

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

    }
}
于 2016-05-06T10:19:45.620 回答