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

HillHacker picture HillHacker · Jun 16, 2015 · Viewed 30k times · Source

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}"

Answer

piotrek1543 picture piotrek1543 · Dec 8, 2015

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>