App & Browser Testing Made Easy

Give your users a seamless experience by testing on 3000+ real devices and browsers. Don't compromise with emulators and simulators

Get Started free
Home Guide How to scroll to an element in Espresso?

How to scroll to an element in Espresso?

By Gaurav Prabhakar, Community Contributor -

The process of swiping down a page/app to access the different sections that are not visible on the current screen is called scrolling. This swiping gesture to reach a particular element is called scrolling to an element. This guide explains in detail the steps to scroll to an element in Espresso. 

Espresso allows you to scroll to an element using two types of lists: Adapter views and Recycler views.

Espresso scroll RecyclerView

RecyclerViewActions.scrollTo() method matches against the ItemView of the ViewHolder, which is inflated in onCreateViewHolder () of the adapter. And so as for the scrollTo () method to figure, you should supply a matcher that uniquely identifies the ItemView.

It returns a ViewAction which scrolls RecyclerView to the view matched by viewHolderMatcher. This approach is employed by RecyclerView.ViewHolder to seek out the target view. It will create one ViewHolder per item type and bind adapter data to the ViewHolder.


Working with RecyclerView

RecyclerView list ensures when the user scrolls off a screen, it recycles the elements in an efficient and effective manner.

To work with RecyclerView, let us use the espresso-contrib package in our app’s Gradle dependencies.


The dependency supports the below actions:

Scrolling in an exceedingly RecyclerView

  • scrollTo() – Scroll to matched view
  • scrollToHolder() – Scroll to matched view holder
  • scrollToPosition() – Scroll to the precise position

Performing an action on the element

  • actionOnItem() – //Perform view action on matched view
  • actionOnHolderItem() – //Perform view action on a matched View holder
  • actionOnItemAtPosition() – //Perform a view action on a view at a selected position

How to scroll to an element in Espresso?

In this section, you will find steps to scroll to elements in an app using Espresso.

Let us use onView to find our RecyclerView


Then, let’s use scrollTo method in RecyclerViewActions


Below is what the complete test looks like:


    public ActivityScenarioRule<MainActivity> activityRule =

            new ActivityScenarioRule<>(MainActivity.class);


    public void scroll_to_bottom() throws InterruptedException {


       int count = getRecyclerViewCount(withId(; // No of items in Recycler View

        Log.d(“Recycler Cnt”, String.valueOf(count));

        for(int i=0;i<count;i++){






Now let‘s scroll to a fixed position


    public void scroll_to_specific_position() throws InterruptedException {


        // scrolling to position 20





The below code clicks and scrolls every item of the Recycler View


    public void scroll_nclickItem() throws InterruptedException {

        int count = getRecyclerViewCount(withId(; // No of items in Recycler View

        // click and scroll all items of the recycler view

        for(int i=0;i<count;i++){


            onView(withId(, click()));






Execute Scroll and Click EspressoTest in BrowserStack

To execute the test in Espresso, run the following command:

curl -u “<username>:<access-key>” \

-X POST “” \

-d ‘{“class”: [“Test-Class-Name”], “devices”: [“Samsung Galaxy S8-7.0”], “app”: “bs://<hashed appid from Step 1>”, “testSuite”: “bs://<hashed testID from Step 2>”}’ \

-H “Content-Type: application/json”

//Test-Class-Name for the above github project


The test results are available on the command-line interface, as well as the App Automate dashboard. You have now run your first Espresso test on BrowserStack App Automate.

Run Scroll To Element Test in Espresso 

To do this, create a test configuration in Android Studio.

  • Open Run menu > Edit Configurations
  • Add a new Android Tests configuration
  • Choose a module
  • Add a specific instrumentation runner:
  • Add your class name

Once the configuration is set up connect your device or select the emulator and directly run the configuration.


Executing Scroll and Click Test in Espresso with BrowserStack

For more accurate results, it is recommended to test on real devices, taking real user conditions into account. 

Run Espresso Tests on Real Devices

With BrowserStack’s real device cloud you can run Espresso Toast Message on real devices by following the steps below:

Step 1: To run your test cases on BrowserStack you need two .apk files.

First one is your main app .apk and another is your androidTest .apk file.

To generate both the apks, run the below command

  • To create a .apk file for the app run the following command in the terminal:

./gradlew assembleDebug

  • To create a .apk file for the test classes in the terminal run the following command:

./gradlew assembleAndroidTest 

Find both .apk files under your project folder: <project-folder>\build\outputs\apk. In the .apk folder you will find two files:

  • app-debug.apk
  • app-debug-androidTest.apk

These are the files that you will upload to the cloud when you create your execution plan.

Step 2: Get your access key from the BrowserStack using the below command 

curl -u “<username>:<access-key>” -X POST “” -F “file=@/path/to/app/file/Application-debug.apk”


Please note the App URL (bs://<hashed appid>) returned in the response of this call:

{“app_url”:“bs://<hashed appid>”}

Step 3: Upload the app by running the below command on your terminal

curl -u “<username>:<access-key>” -X POST “” -F “file=@/path/to/test/file/Application-debug-test.apk”


Please note the Test Suite URL (bs://<hashed testID>) returned in the response of this call:

{“test_url”:“bs://<hashed testID>”}

The Test ID returned in the response will be used to execute your test.

Note: Go through the BrowserStack Docs for detailed information on Espresso

Run Espresso Tests on Real Devices



Automation Testing Mobile App Testing

Featured Articles

Getting Started with Espresso – Android UI Automation

How to test Toast Message using Espresso?

App & Browser Testing Made Easy

Seamlessly test across 20,000+ real devices with BrowserStack