How to check toolbar title in android tool test?

I found a great YT Advanced Android Espresso testing toolkit. I took the code from there with a slight change in my needs.

import static android.support.test.InstrumentationRegistry.getInstrumentation; import static android.support.test.espresso.Espresso.onView; import static android.support.test.espresso.action.ViewActions.click; import static android.support.test.espresso.assertion.ViewAssertions.matches; import static android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom; import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; import static android.support.test.espresso.matcher.ViewMatchers.withChild; import static android.support.test.espresso.matcher.ViewMatchers.withId; import static android.support.test.espresso.matcher.ViewMatchers.withParent; import static android.support.test.espresso.matcher.ViewMatchers.withText; import static org.hamcrest.core.AllOf.allOf; ... @Test public void checkToolbarTitle() { String toolbarTitile = getInstrumentation().getTargetContext().getString(R.string.my_bus_stops); onView(allOf(isAssignableFrom(TextView.class), withParent(isAssignableFrom(Toolbar.class)))).check(matches(withText(toolbarTitile))); } 

Unfortunately, this does not work for me. Test failed with:

 android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: (is assignable from class: class android.widget.TextView and has parent matching: is assignable from class: class android.widget.Toolbar) 

What is wrong with it? How can I test it differently?

+11
source share
4 answers

DECISION

The method is fine. As Chiu-chi Chan wrote in her lesson, you can "pinpoint this particular point of view." BUT you need to be sure to import the correct toolbar:

 import android.support.v7.widget.Toolbar; 

instead:

 import android.widget.Toolbar; 
+9
source

This works for me:

 onView(allOf(instanceOf(TextView.class), withParent(withId(R.id.toolbar)))) .check(matches(withText("toolbarTitile"))); 
+23
source

If you are using an ActionBar and not a toolbar, use this:

 onView(allOf(instanceOf(TextView.class), withParent(withResourceName("action_bar")))) .check(matches(withText("My ActionBar title"))); 

Note: to quickly add import for these methods, place the blinking cursor on the unresolved method, and then run the Android Studio command ➔ Help ➔ Find action ➔ find "show intention action" ➔ click the result parameter ➔ A pop-up window will appear ➔ click on "Import static method ..." You can also assign a keyboard shortcut to "Show Intent Actions." More details here . Another way is to enable "Add unambiguous imports on the fly" in the settings.

+5
source

I don’t remember if I wrote it myself, or found it somewhere, but here is how I check the toolbar headers:

 public static Matcher<View> withToolbarTitle(CharSequence title) { return withToolbarTitle(is(title)); } public static Matcher<View> withToolbarTitle(final Matcher<CharSequence> textMatcher) { return new BoundedMatcher<View, Toolbar>(Toolbar.class) { @Override public boolean matchesSafely(Toolbar toolbar) { return textMatcher.matches(toolbar.getTitle()); } @Override public void describeTo(Description description) { description.appendText("with toolbar title: "); textMatcher.describeTo(description); } }; } 

This works with all cases. Example statement: onView(withId(R.id.toolbar)).check(matches(withToolbarTitle("title")));

+3
source

Source: https://habr.com/ru/post/1246136/


All Articles