Android 未解析使用Espresso测试Toast消息

Android 未解析使用Espresso测试Toast消息,android,android-espresso,toast,Android,Android Espresso,Toast,我不能用浓缩咖啡测试祝酒词。有很多问题和答案与之相关,但我无法解决这个问题 测试代码 class ToastMatcher extends TypeSafeMatcher<Root> { @Override public boolean matchesSafely(Root root) { int type = root.getWindowLayoutParams().get().type; if ((type == WindowM

我不能用浓缩咖啡测试祝酒词。有很多问题和答案与之相关,但我无法解决这个问题

测试代码

 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()));

    }
类ToastMatcher扩展了TypeSafeMatcher{
@凌驾
公共布尔matchessesafely(根){
int type=root.getWindowLayoutParams().get().type;
if((type==WindowManager.LayoutParams.type_)){
IBinder windowToken=root.getDecorView().getWindowToken();
IBinder appToken=root.getDecorView().getApplicationWindowToken();
if(windowToken==appToken){
返回true;
//表示此窗口不包含在任何其他窗口中。
}
}
返回false;
}
@凌驾
公共无效说明(说明){
description.appendText(String.valueOf(R.String.message_login_successful));
}
}
@试验
public void BTNLOGINClickWithPassingUserName和Password()引发异常{
onView(withId(R.id.etUsername)).perform(clearText());
onView(withId(R.id.etUsername)).perform(typeText(userName));
onView(带id(R.id.etPassword)).perform(键入文本(密码));
onView(withId(R.id.btnLogin)).perform(click());
//onView(使用文本(R.string.Message_login_successful)).inRoot(使用DecorView(而不是(mActivityRule.getActivity().getWindow().getDecorView()))。检查(匹配项(isDisplayed()));
//onView(使用文本(R.string.Message_login_successful)).inRoot(使用DecorView(不是(is(mActivityRule.getActivity().getWindow().getDecorView())))。检查(匹配项(isDisplayed()));
onView(带有文本(R.string.message\u login\u successful)).inRoot(新ToastMatcher())
.检查(匹配项(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}
android.support.test.espresso.nomatchingvieweexception:在层次结构中找不到匹配的视图:与资源id中的字符串匹配:[message\u login\u successful]值:成功登录
如果目标视图不是视图层次结构的一部分,则可能需要使用Espresso.onData从以下AdapterViews之一加载它:android.widget.GridView{52a6eb90 VFED.VC..F……60112-884904#7f0a007c应用程序:id/dashMenu}
视图层次结构:
+>DecorView{id=-1,可见性=可见,宽度=1080,高度=1920,有焦点=真,有焦点=真,有窗口焦点=真,可点击=假,已启用=真,有焦点=假,有焦点=假,有焦点=假,布局请求=假,被选择=假,布局参数=WM。布局参数{(0,0)(fillxfill)ty=1 fl=#1810100 pfl=0x8 wanim=0x10302a1},tag=null,root是布局请求=false,has input connection=false,x=0.0,y=0.0,子计数=1}
这个问题怎么解决呢?我总是没有意见 层次结构是否匹配


这句话对我有用

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(not(isDisplayed())));
测试id Toast包含特定的文本消息

onView(withText(R.string.mssage)).inRoot(new ToastMatcher())
.check(matches(withText("Invalid Name"));

我已经试过了。但是无法解决我的问题@BhavikMakwana Toast没有与您拥有的另一个类关联。is()方法在not(is(mActivityRule.getActivity()…)上是多余的,在view上是相同的问题(使用text(R.string.message_login_successful)).inRoot(使用decorview(not(mActivityRule.getActivity().getWindow().getDecorView())。检查(匹配项)(isDisplayed());尝试实现custom matcher.sure。我们必须在这里设置什么。如果我们使用CustomMatcher description.appendText(“is toast”);我总是遇到这个问题,层次结构中找不到匹配的视图:资源id中的字符串
onView(withText(R.string.mssage)).inRoot(new ToastMatcher())
.check(matches(withText("Invalid Name"));