How to check Toolbar title in android instrumental test?

espresso check activity title
espresso onview
how to run espresso test in android studio
android espresso tutorial
kotlin espresso tutorial

I found great instrumental test tutorial on YT Advanced Android Espresso. I took code from there with small adjustment to my needs.

import static;
import static;
import static;
import static;
import static;
import static;
import static;
import static;
import static;
import static;
import static org.hamcrest.core.AllOf.allOf;


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)));

Unfortunatelly it does not work for me. Test failed with: 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 in other way?

This works for me:

onView(allOf(instanceOf(TextView.class), withParent(withId(

java - How to check Toolbar title in android instrumental test?, This is how we look for the view to verify the title: It is a TextView; It has a parent that is a Toolbar. This works, but it depends on the inner  How to set action bar title text alignment also known as gravity in center,left,right in android app. Some times when application developer building android app then there are a specific requirement comes that show simple one word app text on action bar and some times user says middle of action bar, some times left side of action bar and some times right side of action bar.


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:


instead of:

import android.widget.Toolbar;

Espresso: Match Toolbar Title, to verify a fragment's state and interactions in both unit and instrumentation tests. inflater.inflate(R.layout.fragment_title_screen, container, false) For example, when using NavigationUI with views in your fragment, such as with a Toolbar  The InstrumentationTestRunner is the base test runner for Android tests. This test runner starts and loads the test methods. Via the instrumentation API it communicates with the Android system. If you start a test for an Android application, the Android system kills any process of the application under test and then loads a new instance.

If you're using an ActionBar, not a Toolbar, use this:

        .check(matches(withText("My ActionBar title")));

Note: To add 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 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 Intention Actions". More info here. Another way is to enable "Add unambiguous imports on the fly" in the Settings.

Test Navigation, A color to apply to the title string. Inherited XML attributes. From class android.​view.ViewGroup  Check your Toolbar version number and copy it easily for reporting issues. "Sieste ocho" music by Suerte. Listen to more at

I don't remember if I wrote this myself, or if I found it somewhere, but this is how I check toolbar titles:

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) {
        public boolean matchesSafely(Toolbar toolbar) {
            return textMatcher.matches(toolbar.getTitle());

        public void describeTo(Description description) {
            description.appendText("with toolbar title: ");

This works with all cases. Example assertion: onView(withId("title")));

android.widget.Toolbar, Add a Toolbar to an Activity; Use App Bar Utility Methods. In its most basic form, the action bar displays the title for the activity on one side and  This android toolbar example will explain how to implement the toolbar, change the theme colors, set title and place the toolbar on the top or bottom end of the view

Set up the app bar, Espresso is a very powerful UI testing framework for Android. Chiu-Ki Chan describes several onView(ViewMatcher) .perform(ViewAction) .check(​ViewAssertion);. ViewMatcher. Espresso With Toolbar title generates a custom Matcher. I also have UI-less Instrumentation (to test the Database). I am not  In this android toolbar example, I will be explaining to you how to use Toolbar in your application. So, if you are confused about using the toolbar in your android application, then this is the right place for you. 1 What is Toolbar in Android? What is Toolbar in Android? The toolbar was introduced from Android Lollipop when Material Design

Advanced Android Espresso, In the activity set the title for the CollapsingToolbarLayout to "Test" 3. Instrumentation$ onView​(isAssignableFrom(CollapsingToolbarLayout.class)). check(matches(  In this video I will show you guys how to test a song for copyright on you tube and also find out what a copyright owner can do to your video if you decide to upload the video with the copyrighted

dragthor/android-test-kit, On Android UI tests, we use the instrumentation mechanism to execute tests. the device name inside the Devices tab, open the screen you want to inspect, 2​. open a contextual menu toolbar in the all to-dos list and create a layout dump. To use this custom matcher, we change our helper function to look for the Toolbar itself, and check that it has the expected title. Notice that we pass is (title) to withToolbarTitle, which takes a text matcher rather than a string. That way we can use other matchers such as startsWith and endsWith .

  • This did not work for me. It was the import as Grzegorz Bielanski posted. instanceOf could not be resolved for me.
  • Just wondering why are you down-voting?! It works for most of the cases (see amount of votes) and the fact that it doesn't work for you doesn't make this answer incorrect.
  • How do I know it works for "most of the cases". My experience is that what works on Android changes from version to version, and a relevant answer today is not sure to be relevant tomorrow.
  • @gr envoy How do you know that your experience and point are valuable for this answer? Just don't downvote if it doesn't work for you, leave a comment. You should appreciate people effort first.
  • @denys I up voted this answer. This case worked for me. And I came across this answer when I was having a similar problem. And the fact that this answer still works more than a year on. Casts doubt on GR Envoy assumption that it could break tomorrow.