Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

like image 451
HillHacker Avatar asked Jun 16 '15 12:06

HillHacker


People also ask

How do you scroll in Espresso test?

You can use SWIPE to scroll to the bottom of the screen: Espresso. onView(ViewMatchers. withId(R.

How do you scroll on recycler view Espresso?

To interact with RecyclerViews using Espresso, you can use the espresso-contrib package, which has a collection of RecyclerViewActions that can be used to scroll to positions or to perform actions on items: scrollTo() - Scrolls to the matched View, if it exists.

How do you swipe Espresso?

As you may know the latest Espresso release contains new left and right swiping actions: swipeLeft() and swipeRight() . They both are really useful when you'd like to swipe between activity fragments, tab layouts or any other UI elements. You can use it as any other view action: onView(withId(R.


2 Answers

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> 

like image 100
piotrek1543 Avatar answered Sep 18 '22 06:09

piotrek1543


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

like image 27
Pablo Avatar answered Sep 22 '22 06:09

Pablo