5

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 ?

2 Answers 2

2

This Statement works for me.

import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.RootMatchers.withDecorView;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;

onView(withText(R.string.TOAST_STRING)).inRoot(withDecorView(not(getActivity().getWindow().getDecorView()))).check(matches(isDisplayed()));

or use Custom Matcher to achieve this

public 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;
    }

Test if the Toast Message is Displayed

onView(withText(R.string.mssage)).inRoot(new ToastMatcher())
.check(matches(isDisplayed()));

Test if the Toast Message is not Displayed

onView(withText(R.string.mssage)).inRoot(new ToastMatcher())
.check(matches(not(isDisplayed())));

Test id the Toast contains specific Text Message

onView(withText(R.string.mssage)).inRoot(new ToastMatcher())
.check(matches(withText("Invalid Name"));
Sign up to request clarification or add additional context in comments.

6 Comments

I have tried this .But not Able to solve my issue @BhavikMakwana The Toast is not associated with Another class
You have is() method is redundant at not(is(mActivityRule.getActivity()...)
same issue onView(withText(R.string.messsage_login_successful)).inRoot(withDecorView(not(mActivityRule.getActivity().getWindow().getDecorView()))).check(matches(isDisplayed()));
try to implement custom matcher.
sure .What we have to set in here. if we used CustomMatcher description.appendText("is toast"); I always get this issue No views in hierarchy found matching: with string from resource id
|
2

I'm having the same issue, using Android 11. With every other Android Version your code just works fine (for me). Unfortunately I could not yet find any solution. Not even UIAutomator seems to detect toasts on Android 11 (as suggested by this post).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.