2

I want test "Settings" option in toolbar. On click of "Settings" option, It will launch new activity and from toolbar title name I want to confirm is it launched successfully launched or not. Below is the code:- java code

@RunWith(AndroidJUnit4.class)
@SmallTest
public class MainActivityTest {

    @Rule
    public ActivityTestRule<MainActivity> mainActivityActivityTestRule = new ActivityTestRule<>(MainActivity.class);

    @Before
    public void  intialize(){

    }

    @Test
    public void testCanGoToSettings() {
        onView(withId(R.id.toolbar))
                .perform(navigateTo(R.id.action_settings));
        String expectedNoStatisticsText = InstrumentationRegistry.getTargetContext()
                .getString(R.string.action_settings);
        onView(withId(R.id.toolbarSettings)).check(matches(withText(expectedNoStatisticsText)));

    }

}

xml code

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/home_menu">
    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:title="@string/action_settings"
        app:showAsAction="never" />
</menu>

I am getting below error.

android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with id: com.xyz.android.control:id/toolbar

Kindly help, How I can implement this.

Karanbeer Kaur
  • 365
  • 4
  • 14

1 Answers1

3

I belive this answer is appropriated to this question: [https://stackoverflow.com/a/36656925/2688351][1].

In your case you should write something like this:

// Click menu
openActionBarOverflowOrOptionsMenu(getInstrumentation().getTargetContext());

// Choose item "Settings"
onView(withText(mActivity.getString(R.string.action_settings))).perform(click());

// Check that settings activity was opened
onView(withText(mActivity.getString(R.string.some_settings_item))).check(matches(isDisplayed()));
Community
  • 1
  • 1
Dmitriy
  • 487
  • 5
  • 15