Android espresso NestedScrollView how to scroll bottom to bottom

I am trying to check with Espresso a NestedScrollView, but I have an error:

"The action will not be completed because the target view does not meet one or more of the following restrictions: at least 90 percent of the viewing area is displayed to the user.

I know that this error is due to the fact that Android does not detect the button that I want to click, that is, I need to scroll down to see the button. I also read that scrollTo () is not available for NestedScrollView, so I cannot use it. I guess I need to scroll to the bottom of the NestedScrollView to see the button, and I'm not sure about this, and don't know how to do it.

I want to click the button in red, but has no visibility

I want to click the button in red, but it has no visibility. I saw some stack questions and some lessons, but I can't figure out how to scroll the bottom of the nested.

Application Toolbar Code:

<?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> 

NestedScrollView Code:

 <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 button id is idBEventoNuevoRegistrar and the contentDescription is eventoNuevoRegistrar

Finally, in espresso, I tried the following.

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

This creates an error: assigned from the class: class android.widget.AdapterView corresponds to several views in the hierarchy.

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

This leads to an error: The action will not be completed because the target view does not meet one or more of the following restrictions: at least 90 percent of the viewing area is displayed to the user.

Also I tried:

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

This causes an error: android.support.test.espresso.AmbiguousViewMatcherException: 'assigned to class: class android.widget.AdapterView' corresponds to several views in the hierarchy

In the error log, I see 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 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.

+2
source share
3 answers

Edition:

Finally, I found a solution. The answer was made in the UiAutomator test.

Easier than:

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

Hope this helps.

- PREVIOUS ANSWER ---

After reading the @Be_Negative answer, I found that I need to create a new class for scrolling in NestedScrollView due to the fact that it is not a descendant of Scrollview.

Grade:

 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 an attempt: OnView (withId (R.id.fragment_evento_crear)) execute (ScrollToAction.betterScrollTo ()) ;.

He scratches again. After searching for information, I found the following: Scrolling to view has been attempted, but the view is not displayed

Finally, the solution was as follows:

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

2- Remove padding in NestedScrollView.

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

+2
source

I ran into the same problem and I really hope that the solutions provided by @ be-negative and @tachenko will work, but they did not. What worked for me just used swipeUp() instead of scrollTo() .

+2
source

Processing a NestedScrollView requires a special view action to circumvent the limitation in the scrollTo() built-in action.

This answer contains more information: Espresso NestedScrollView

+1
source

All Articles