How to scroll down the screen in the android espresso test? I need to validate the text present on the screen

36,648

Solution 1

I don't know how this layout looks like, but to use

Just perform a ViewActions.scrollTo() before your assertion:

onView(withText("Launch"))
         .perform(ViewActions.scrollTo())
         .check(ViewAssertions‌​.matches(isDisplayed()));

you need to have ScrollView as your main view in structure.

If you have already LinearLayout, RelativeLayout, you have to change it to like this:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

Solution 2

You can use SWIPE to scroll to the bottom of the screen:

Espresso.onView(ViewMatchers.withId(R.id.recyclerView)).perform(ViewActions.swipeUp());

For me its working nice.

Pablo

Solution 3

There is a solution to click in a button in a scroll view

onView(withId(R.id.onBottomOfScrollView)).perform(scrollTo(), click());

Espresso: how to scroll to the bottom of ScrollView

Solution 4

Just perform a ViewActions.scrollTo() before your assertion:

onView(withText("Launch")).perform(ViewActions.scrollTo()).check(ViewAssertions‌​.matches(isDisplayed()));
Share:
36,648
Bhupendra Singh Rautela
Author by

Bhupendra Singh Rautela

Updated on January 29, 2020

Comments

  • Bhupendra Singh Rautela
    Bhupendra Singh Rautela over 4 years

    I am performing the below test to validate the text on screen , The text is present on the view but need to page scroll to see the text manually.

     onView(withText("Launch")).check(ViewAssertions.matches(isDisplayed()));
        onView(withText("January 2010")).check(ViewAssertions.matches(isDisplayed()));
    

    Following error is coming, However the text is present on the view but need to page scroll to see the text manually.

    android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'is displayed on the screen to the user' doesn't match the selected view. Expected: is displayed on the screen to the user Got: "TextView{id=2131361941, res-name=project_details_label_tv, visibility=VISIBLE, width=249, height=41, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=4.0, y=24.0, text=Status, input-type=0, ime-target=false, has-links=false}"