Android Espresso – assert text on screen against string in resources

Use this function:

private String getResourceString(int id) {
    Context targetContext = InstrumentationRegistry.getTargetContext();
    return targetContext.getResources().getString(id);
}

You just have to call it with the id of the string and perform your action:

String myTextFromResources = getResourceString(R.string.main_ent_mil_new_mileage);
onView(allOf(withId(R.id.my_text_on_screen), withText(myTextFromResources))
    .check(matches(isDisplayed()));

*EDIT for new Espresso version:

With new version of Espresso, you should be able to call directly the string resource with a ViewMatcher. So first, I recommend to try directly this import

import static android.support.test.espresso.matcher.ViewMatchers.withText;

And then in the code:

withText(R.string.my_string_resource)

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)