gowthami gowthami - 4 months ago 41x
Android Question

android instrumentation test for listview

I wrote a code for performing test for the listview (i.e., we can check the position of the item in listview, display toast messame when we click on item in list). i want to move to another activity when we click on list item in android.

public class MainActivityTest {

public ActivityTestRule<MainActivity> main = new ActivityTestRule<MainActivity>(MainActivity.class) {
protected void beforeActivityLaunched() {

protected void afterActivityFinished() {

here i write a test for counting the nomber of items in list

public void testShouldLaunchTheMainActivityAndFindItemsInTheList() throws Exception {
ListView listview = (ListView) main.getActivity().findViewById(R.id.PhoneVideoList);

assertThat((int) listview.getCount(), is(61));

here i write a test for performing on click in list item

public void testShouldTestTheItemNameInTheList() throws Exception {
ListView listview = (ListView) main.getActivity().findViewById(R.id.PhoneVideoList);
listview.performItemClick(listview.getChildAt(1), 1, listview.getAdapter().getItemId(1));
listview.performItemClick(listview.getChildAt(4), 4, listview.getAdapter().getItemId(4));
listview.performItemClick(listview.getChildAt(10), 10, listview.getAdapter().getItemId(10));

public void testShouldShowTheItemDetailWhenAnItemIsClicked() {
Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
final ListView listview = (ListView) main.getActivity().findViewById(R.id.PhoneVideoList);

instrumentation.runOnMainSync(new Runnable() {
public void run() {
int position = 0;
listview.performItemClick(listview.getChildAt(position), position, listview.getAdapter().getItemId(position));

Log.e("in on click", "on click");

Instrumentation.ActivityMonitor monitor = instrumentation.addMonitor(VideoPlay.class.getName(), null, false);
Activity itemDetailActivity = instrumentation.waitForMonitorWithTimeout(monitor, 5000);

/* TextView detailView = (TextView) itemDetailActivity.findViewById(R.id.item_detail);
assertThat(detailView.getText().toString(), is("Android"));*/


now i want to know how to move to next activity when we click on list item


Using robotium:

1. Add following dependencies in app gradle:

    testCompile 'junit:junit:
    androidTestCompile 'junit:junit:4.12'
    androidTestCompile 'com.jayway.android.robotium:robotium:5.4.+'

2. Add click listener to the list item which starts new activity 3. Add following test

public class ListViewClickTest extends ActivityInstrumentationTestCase2<YourActivity> {

    protected final CountDownLatch signal       = new CountDownLatch (1);
    protected Context              context;
    protected Activity         activity;
    protected Solo                 solo;

    public ListViewClickTest () {

        super (YourActivity.class);

    public void testListViewClick () {

        waitForTime (4); //time to wait till your network request completes if data is being fetched from server, ignore otherwise

        solo.clickInList (0); //position of list view item

        waitForTime (50);

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

        //Prepare test data if any