0

I have an Activity.java which includes some tabs. Each tab represents different Fragments. One tab namely Menu has a listview where the items are being fetched from API call.

Now, how can I write test cases for the items on listview?

I tried the code below :

onData(withId(R.id.navigation_menu)).check(matches(withText(R.string.log_out))) .perform(click());

Here, navigation_menu is the list name and log_out is the name of item string. But then I get the following error:

android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching

The hierarchy is:

HomeActivity.java --> TabAdapter.java --> MenuFragment.java --> MenuListAdapter.java

So, what should be the test case for an item of a listview that resides in a fragment of an activity ? Please HELP!!

Sanzi
  • 13
  • 1
  • 2

2 Answers2

0

navigation_menu must be a type of Scrollable View to be used with onData() and all view HAVE TO visible on screen (If your screen is small and you have to scroll to see that view --> That also means Not Visible.

If navigation_menu is a kind of Scrollable view already. you can call additional method if it is hidden on screen. Example:

 onData(withId(R.id.navigation_menu)).perform(scrollTo()).check(...)
Toan Tran Van
  • 695
  • 1
  • 7
  • 25
  • Thank You but navigation_menu is already scrollable view. I think I am stuck cause I can't understand how to link the hierarchies (from activity to navigation_menu_items). – Sanzi Aug 18 '16 at 11:00
  • So add perform(scrollTo() before calling check() method – Toan Tran Van Aug 18 '16 at 11:02
-1

I didnt code in Android in some time so bear with me. why are you referring to the string instead of the item itself ?

you should reference the id of the item itself not its string

 public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_log_in) {
        Log.i(PRESS_TAG, "lOG IN");
        callLogInActivity(mainView);

    } else if (id == R.id.nav_settings) {
        Log.i(PRESS_TAG, " Settings");
        callSettingsActivity(mainView);}
    return true;
}

Xml

   <item
            android:id="@+id/nav_settings"
            android:icon="@drawable/ic_menu_manage"
            android:title="@string/nav_settings_title" />
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Pc hobbit
  • 96
  • 1
  • 11
  • I am new so I am still inexperienced about many topics. I am writing the test case in a different java class, not in Fragment. (Sorry, should've mentioned that). Thus, calling *onNavigationItemSelected* is not possible. – Sanzi Aug 18 '16 at 10:57
  • okay I think I understand your question better now. Are you trying to fill your menu with the fetched items from the API ? or are you trying to logout? here's how you can reference different objects in Android: [link](https://developer.android.com/guide/topics/resources/accessing-resources.html) See also : [link] (http://stackoverflow.com/questions/1983548/eclipse-java-values-in-r-string-return-int) – Pc hobbit Aug 19 '16 at 03:23
  • I filled my menu with fetched items which includes 'log out'. & then I'm trying to click the "log out' for testing purpose. – Sanzi Aug 21 '16 at 04:54