我无法使用 Espresso 测试 Toast 消息。有很多与之相关的问题和答案,但我无法解决该问题。
测试代码
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}
How can this issue be solved.I am always getting No views in hierarchy found matching ?
最佳答案
此声明对我有用。
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()));
或使用自定义匹配器来实现此目的
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())));
测试 Toast 包含特定文本消息的 ID
onView(withText(R.string.mssage)).inRoot(new ToastMatcher())
.check(matches(withText("Invalid Name"));
关于android - 使用 Espresso 测试 Toast 消息未解决,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47092927/