I am not able to test the Toast Message using the Espresso.There are many question as well as answer associated to it but i am not able to Solve the issue.
TestingCode
class ToastMatcher extends TypeSafeMatcher<Root> {
@Override
public boolean matchesSafely(Root root) {
int type = root.getWindowLayoutParams().get().type;
if ((type == WindowManager.LayoutParams.TYPE_TOAST)) {
IBinder windowToken = root.getDecorView().getWindowToken();
IBinder appToken = root.getDecorView().getApplicationWindowToken();
if (windowToken == appToken) {
return true;
//means this window isn't contained by any other windows.
}
}
return false;
}
@Override
public void describeTo(Description description) {
description.appendText(String.valueOf(R.string.messsage_login_successful));
}
}
@Test
public void btnLoginClickWithPassingUserNameAndPassword() throws Exception {
onView(withId(R.id.etUsername)).perform(clearText());
onView(withId(R.id.etUsername)).perform(typeText(userName));
onView(withId(R.id.etPassword)).perform(typeText(passWord));
onView(withId(R.id.btnLogin)).perform(click());
// onView(withText(R.string.messsage_login_successful)).inRoot(withDecorView(not(mActivityRule.getActivity().getWindow().getDecorView()))).check(matches(isDisplayed()));
// onView(withText(R.string.messsage_login_successful)).inRoot(withDecorView(not(is(mActivityRule.getActivity().getWindow().getDecorView())))).check(matches(isDisplayed()));
onView(withText(R.string.messsage_login_successful)).inRoot(new ToastMatcher())
.check(matches(isDisplayed()));
}
Issue i got
android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with string from resource id: <2131689608>[messsage_login_successful] value: Logged In Successfully
If the target view is not part of the view hierarchy, you may need to use Espresso.onData to load it from one of the following AdapterViews:android.widget.GridView{52a6eb90 VFED.VC. .F...... 60,112-884,904 #7f0a007c app:id/dashMenu}
View Hierarchy:
+>DecorView{id=-1, visibility=VISIBLE, width=1080, height=1920, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=WM.LayoutParams{(0,0)(fillxfill) ty=1 fl=#1810100 pfl=0x8 wanim=0x10302a1}, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1}
How can this issue be solved.I am always getting No views in hierarchy found matching ?
This Statement works for me.
or use Custom Matcher to achieve this
Test if the Toast Message is Displayed
Test if the Toast Message is not Displayed
Test id the Toast contains specific Text Message