user3624146
user3624146

Reputation: 369

Android::android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching

This is the first time I am running the espresso and I am stuck at this issue

Issue:: android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: (with id: edu.gatech.seclass.sdpguessit:id/login_page_username_field and with text: is "Enter Username" and Child at position 1 in parent Child at position 0 in parent with id: 16908290 and is displayed on the screen to the user)

This is my code below:

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`
            }
        };
    }
}

How can I solve this?

Upvotes: 1

Views: 5935

Answers (2)

TechDija
TechDija

Reputation: 11

I had the same problem with the Id of a Floating Action Button, which was showing on my app, but not in the Espresso hierarchy.

I ended using the espresso test recorder provided in Android Studio (click on Run, then Record Espresso Test, and just navigate in your app the way you would like your test to...) Then you can copy the result.

It lacks elegance, but it does the trick.

Upvotes: 1

yu wang
yu wang

Reputation: 293

Try this:

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

Upvotes: 0

Related Questions