Android :: android.support.test.espresso.NoMatchingViewException:找不到层次结构中的视图匹配

时间:2018-03-09 11:28:36

标签: java android

这是我第一次运行浓缩咖啡而且我遇到了这个问题

  

问题::   android.support.test.espresso.NoMatchingViewException:找不到匹配的层次结构中的视图:(带有id:edu.gatech.seclass.sdpguessit:id / login_page_username_field并带有文本:是“输入用户名”,而父级Child的位置为1的Child父级中的位置0,ID为:16908290,并在屏幕上显示给用户)

这是我的代码:

package edu.gatech.seclass.sdpguessit.ui;


import android.support.test.espresso.DataInteraction;
import android.support.test.espresso.ViewInteraction;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.LargeTest;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;

import static android.support.test.InstrumentationRegistry.getInstrumentation;
import static android.support.test.espresso.Espresso.onData;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.Espresso.pressBack;
import static android.support.test.espresso.Espresso.openActionBarOverflowOrOptionsMenu;
import static android.support.test.espresso.action.ViewActions.*;
import static android.support.test.espresso.assertion.ViewAssertions.*;
import static android.support.test.espresso.matcher.ViewMatchers.*;

import edu.gatech.seclass.sdpguessit.R;

import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.hamcrest.core.IsInstanceOf;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.anything;
import static org.hamcrest.Matchers.is;

@LargeTest
@RunWith(AndroidJUnit4.class)
public class LoginActivityTest {

    @Rule
    public ActivityTestRule<LoginActivity> mActivityTestRule = new ActivityTestRule<>(LoginActivity.class);

    @Test
    public void loginActivityTest() {
        ViewInteraction editText = onView(
                allOf(withId(R.id.login_page_username_field), withText("Enter Username"),
                        childAtPosition(
                                childAtPosition(
                                        withId(android.R.id.content),
                                        0),
                                1),
                        isDisplayed()));
        editText.check(matches(withText("Enter Username")));

        ViewInteraction button = onView(
                allOf(withId(R.id.login_button),
                        childAtPosition(
                                childAtPosition(
                                        withId(android.R.id.content),
                                        0),
                                2),
                        isDisplayed()));
        button.check(matches(isDisplayed()));

        ViewInteraction button2 = onView(
                allOf(withId(R.id.create_player_button),
                        childAtPosition(
                                childAtPosition(
                                        withId(android.R.id.content),
                                        0),
                                3),
                        isDisplayed()));
        button2.check(matches(isDisplayed()));

    }

    private static Matcher<View> childAtPosition(
            final Matcher<View> parentMatcher, final int position) {

        return new TypeSafeMatcher<View>() {
            @Override
            public void describeTo(Description description) {
                description.appendText("Child at position " + position + " in parent ");
                parentMatcher.describeTo(description);
            }

            @Override
            public boolean matchesSafely(View view) {
                ViewParent parent = view.getParent();
                return parent instanceof ViewGroup && parentMatcher.matches(parent)
                        && view.equals(((ViewGroup) parent).getChildAt(position));`enter code here`
            }
        };
    }
}

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

试试这个:

onView(withId(R.id.login_page_username_field)).check(matches(withText("Enter Username"));

答案 1 :(得分:0)

我在我的应用程序中显示了浮动操作按钮的ID时遇到了同样的问题,但在Espresso层次结构中却没有。

我结束了使用Android Studio中提供的 espresso测试记录器(单击“运行”,然后单击“记录Espresso测试”,然后按照您希望测试的方式浏览应用程序。 ..) 然后,您可以复制结果。

它缺乏优雅,但却能解决问题。