Android espresso NestedScrollView, how to scroll to bottom

Tachenko picture Tachenko · Sep 4, 2016 · Viewed 7.5k times · Source

I'm trying to test with Espresso a NestedScrollView but I am having an error:

"Action will not be performed because the target view does not match one or more of the following constraints: at least 90 percent of the view's area is displayed to the user."

I know that this error is because Android does not detect the button that I want to click, that is, I need to scroll to bottom in order to see the button. I also have read that scrollTo() is not available for NestedScrollView so I cannot use it. I guess that I have to scroll to bottom of the NestedScrollView to see the button, neither I am not sure of this nor I know how to do it.

I want to click in the button in red but it has not visibility

I want to click in the button in red but it has not visibility. I have seen several stack questions and some tutorials but I can not figure out how to scroll to bottom of the Nested.

The code of the app toolbar is:

<?xml version="1.0" encoding="utf-8"?>
        <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/main_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            tools:context="com.findandgo.activity.MenuPrincipal"
            >

            <android.support.design.widget.AppBarLayout
                android:id="@+id/appbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingTop="@dimen/appbar_padding_top"
                android:theme="@style/AppTheme"
                >


                <android.support.v7.widget.Toolbar

                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:background="?attr/colorPrimary"
                    app:layout_scrollFlags="scroll"
                    app:popupTheme="@style/AppTheme.AppBarOverlay"
                    xmlns:app="http://schemas.android.com/apk/res-auto"
                    xmlns:android="http://schemas.android.com/apk/res/android"/>



                    <android.support.design.widget.TabLayout
                    android:id="@+id/tabs"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom"
                    />

            </android.support.design.widget.AppBarLayout>


            <android.support.v4.view.ViewPager
                android:id="@+id/container"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_behavior="@string/appbar_scrolling_view_behavior" />`



        </android.support.design.widget.CoordinatorLayout>

The code of the NestedScrollView is:

<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment_evento_crear"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="fill_vertical"
    android:background="@drawable/fondo"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".Evento"
    >
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:nestedScrollingEnabled="false"
        android:padding="@dimen/size_20"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin">


        <com.findandgo.custom.CustomFontEditText
            android:id="@+id/idEtEventoNuevoNombre"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/idSpEventoNuevoCategoria"
            android:layout_alignStart="@+id/idSpEventoNuevoCategoria"
            android:layout_below="@id/tool_bar"
            android:hint="@string/sEventoNombre"
            android:textSize="@dimen/size_12"
            app:font="@string/font_name_source_amatic_regular" />

        ...

        <ImageButton
            android:id="@+id/idBEventoNuevoRegistrar"

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/idEtEventoNuevoDescripcion"
            android:layout_centerHorizontal="true"
            android:background="@drawable/btn_save"
            android:contentDescription="@string/eventoNuevoRegristar"
            android:textStyle="bold"
            android:visibility="visible" />


    </RelativeLayout>
</android.support.v4.widget.NestedScrollView>

As you can see, the id of the button is idBEventoNuevoRegistrar and the contentDescription is eventoNuevoRegistrar

Finally in espresso I have tried the following.

onData(withContentDescription(R.string.eventoNuevoRegristar)).check(matches(isDisplayed())).perform(click());

That produces the error:is assignable from class: class android.widget.AdapterView matches multiple views in the hierarchy.

onView(withId(R.id.idBEventoNuevoRegistrar)).check(matches(isDisplayed())).perform(click());

That produces the error: Action will not be performed because the target view does not match one or more of the following constraints: at least 90 percent of the view's area is displayed to the user.

Also I have tried:

onData(withId(R.id.idBEventoNuevoRegistrar))
                .check(matches(isDisplayed()))
                .perform(click());

That produces error: android.support.test.espresso.AmbiguousViewMatcherException: 'is assignable from class: class android.widget.AdapterView' matches multiple views in the hierarchy

In the error Log, I can see the NestedScrollView:

NestedScrollView{id=2131362239, res-name=fragment_evento_crear, visibility=VISIBLE, width=720, height=1118, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1}

And the button that I want to click:

AppCompatImageButton{id=2131362214, res-name=idBEventoNuevoRegistrar, desc=eventoNuevoRegristar, visibility=VISIBLE, width=128, height=128, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=296.0, y=1313.0}

Thanks in advance.

Answer

Tachenko picture Tachenko · Sep 20, 2016

EDITED:

Finally I found the solution. The answer was in UiAutomator Test.

As simple as:

 UiScrollable appViews = new UiScrollable(
            new UiSelector().scrollable(true));

 appViews.scrollForward();

I hope it's helps.

-- PREVIOUS RESPONSE---

After reading @Be_Negative answer I have found that I have to create a new class in order to scroll in NestedScrollView due to it is not a descendant of Scrollview.

The class is:

public final class ScrollToAction implements ViewAction {
    private static final String TAG = ScrollToAction.class.getSimpleName();

@SuppressWarnings("unchecked")
@Override
public Matcher<View> getConstraints() {
    return allOf(withEffectiveVisibility(Visibility.VISIBLE), isDescendantOfA(anyOf(
            isAssignableFrom(ScrollView.class), isAssignableFrom(HorizontalScrollView.class), isAssignableFrom(NestedScrollView.class))));
}

@Override
public void perform(UiController uiController, View view) {
    if (isDisplayingAtLeast(80).matches(view)) {
        Log.i(TAG, "View is already displayed. Returning.");
        return;
    }
    Rect rect = new Rect();
    view.getDrawingRect(rect);
    if (!view.requestRectangleOnScreen(rect, true /* immediate */)) {
        Log.w(TAG, "Scrolling to view was requested, but none of the parents scrolled.");
    }
    uiController.loopMainThreadUntilIdle();
    if (!isDisplayingAtLeast(80).matches(view)) {
        throw new PerformException.Builder()
                .withActionDescription(this.getDescription())
                .withViewDescription(HumanReadables.describe(view))
                .withCause(new RuntimeException(
                        "Scrolling to view was attempted, but the view is not displayed"))
                .build();
    }
}
public static ViewAction betterScrollTo() {
    return ViewActions.actionWithAssertions(new ScrollToAction());
}

@Override
public String getDescription() {
    return "scroll to";
}}

After trying: onView(withId(R.id.fragment_evento_crear)).perform(ScrollToAction.betterScrollTo());

It chrashes again. After looking for information, I found this: Scrolling to view was attempted, but the view is not displayed

Finally the solution was the following:

1- Create a class as @Be_Negative suguested and I put below.

2- Delete Padding in NestedScrollView.

3- Call: onView(withId(R.id.button)). perform(ScrollToAction.betterScrollTo());