Using inRoot to target non-default windows
onView(withText("South China Sea")) .inRoot(withDecorView(not(is(getActivity().getWindow().getDecorView())))) .perform(click());
But, I think this is not cool.
We can create a custom matcher.
Toast window has the WindowManager.LayoutParams.TYPE_TOAST.
The followinf code is sample.
/** * Matcher that is Toast window. */ public static Matcher<Root> isToast() { return new TypeSafeMatcher<Root>() { @Override public void describeTo(Description description) { description.appendText("is toast"); } @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) { // windowToken == appToken means this window isn't contained by any other windows. // if it was a window for an activity, it would have TYPE_BASE_APPLICATION. return true; } } return false; } }; }
Hi, thanks for the post!, really good. One question, why do you think the first approach is not cool?
ReplyDeletethe first approach is not checked "TYPE_TOAST". it mean that window type is not checked.
ReplyDeletewe should create a custom matcher,
Interesting approach to read..Thank you for sharingAndroid Training
ReplyDeleteI am getting this android.support.test.espresso.NoMatchingRootException: Matcher 'is toast' did not match any of the following roots:
ReplyDeleteWhat should I do?
If you are using kotlin, this would resolve it
DeleteonView(withText(R.string.invalid_phone_number)).inRoot(ToastMatcher().apply {
matches(isDisplayed())
}
Using this way the assertion will always pass, even if there is no toast.
DeleteThis comment has been removed by the author.
DeleteToasts have a period of visibility and during testing it could happen that the assert explained is run when the toast has already dissapeared, depending on android's device. How do you manage these situations?
ReplyDeleteWish you are always happy and lucky. Hope you have more good articles.
ReplyDeletegiảo cổ lam giảm cân
giảo cổ lam giảm béo
giảo cổ lam giá bao nhiêu
giảo cổ lam ở đâu tốt nhất
In this particular article, you will see a summary, satisfy browse this post. kaffebryggare
ReplyDeletecan you write matcher class in kotlin
ReplyDeletehh
ReplyDelete