How to check Toolbar title in android instrumental test?
This works for me:
onView(allOf(instanceOf(TextView.class), withParent(withId(R.id.toolbar))))
.check(matches(withText("toolbarTitile")));
If you're using an ActionBar, not a Toolbar, use this:
onView(allOf(instanceOf(TextView.class),
withParent(withResourceName("action_bar"))))
.check(matches(withText("My ActionBar title")));
Note: To quickly add the imports for these methods, put the blinking cursor on the unresolved method, then do Android Studio ➔ Help ➔ Find Action ➔ search for "show context action"
or "show intention action"
➔ click on the result option ➔ A popup window will appear ➔ click on "Import static method ..."
. You can also assign a keyboard shortcut to "Show Context Actions". More info here. Another way is to enable "Add unambiguous imports on the fly"
in the Settings.
SOLUTION
The method is fine. As Chiu-Ki Chan wrote in her tutorial you can "pinpoint that one particular view". BUT you have to make sure you imported proper Toolbar:
import android.support.v7.widget.Toolbar;
instead of:
import android.widget.Toolbar;
Here's an alternative (and more idiomatic approach):
onView(withId(R.id.toolbar)).check(matches(hasDescendant(withText(toolbarTitle))))