Android - Espresso - scrolling to a non-list View item

前端 未结 3 1109
Happy的楠姐
Happy的楠姐 2021-01-02 05:08

Is there a general approach for scrolling to non-list View items that are not yet visible on the screen?

Without any precautions, Espresso will indicate that \"No V

相关标签:
3条回答
  • 2021-01-02 05:45

    According to the scrollTo JavaDoc, to use the code you specified ( onView( withId( R.id.button)).perform( scrollTo(), click()); ), the preconditions are: "must be a descendant of ScrollView" and "must have visibility set to View.VISIBLE". If that is the case, then that will work just fine.

    If it is in an AdapterView, then you should use onData instead. In some cases, you may have to implement the AdapterViewProtocol, if your AdapterView is not well behaved.

    If it is neither in an AdapterView nor a child of a ScrollView, then you would have to implement a custom ViewAction.

    0 讨论(0)
  • 2021-01-02 05:47

    If you have a view inside android.support.v4.widget.NestedScrollView instead of scrollView scrollTo() does not work.

    In order to work you need to create a class that implements ViewAction just like ScrollToAction but allows NestedScrollViews:

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

    extra tip and access the action like:

    public static ViewAction betterScrollTo() {
        return actionWithAssertions(new AllScrollViewsScrollToAction());
    }
    

    But with this scroll it does not trigger events from the layout managers.

    0 讨论(0)
  • 2021-01-02 06:03

    Code that worked for me is:

    ViewInteraction tabView = onView(allOf(
        childAtPosition(childAtPosition(withId(R.id.bottomControlTabView), 0), 1), 
        isDisplayed()));
    tabView.perform(click());
    tabView.perform(click());
    
    public 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));
                }
            };
        }
    
    0 讨论(0)
提交回复
热议问题