我无法使用Espresso测试Toast消息。有许多问题以及与之相关的答案,但我无法解决问题。
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()));
}
问题我
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}
如何解决这个问题。我总是得到没有意见 层次结构找到匹配?
答案 0 :(得分:0)
本声明适合我。
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()));
或使用Custom Matcher实现此目的
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;
}
测试是否显示Toast消息
onView(withText(R.string.mssage)).inRoot(new ToastMatcher())
.check(matches(isDisplayed()));
测试是否未显示Toast消息
onView(withText(R.string.mssage)).inRoot(new ToastMatcher())
.check(matches(not(isDisplayed())));
测试ID Toast包含特定的文本消息
onView(withText(R.string.mssage)).inRoot(new ToastMatcher())
.check(matches(withText("Invalid Name"));
答案 1 :(得分:0)
我在使用 Android 11 时遇到了同样的问题。对于其他所有 Android 版本,您的代码都可以正常工作(对我而言)。 不幸的是,我还没有找到任何解决方案。甚至 UIAutomator 似乎也无法检测到 Android 11 上的 toast(如 this post 所建议的那样)。