How to write script in Espresso ?

Updated 16 April 2019


As Espresso is an emerging automation tool for application testing but how to understand the script that’s the main issue because after recording the test we start playing it then what we see that its too slow and giving error in each and every part of the test script because the reason is that espresso records necessary things as well as the unnecessary things also .so for that we have to edit the test script and make it simple so that it will run the recorded script smoothly .

Following are the tips for writing scripts in Espresso :-

1. When start recording test the time we delay to perform any action then it will also record that like:-

So, when you play that script then  this code will make a long delay because of the try catch exception.

Solution :-

Remove try catch block set the time as required. like:-

2. When you got “single click” error that means that suppose if you want to click the particular button but that is not properly displaying on the screen.This error occur whenever “Scroll to” is used. According to espresso whatever thing displayed to the user on screen then only it will perform any action otherwise it will generate error like this :-

Solution :-

Instead of using Scroll to() use swipe up(),reason is that when using swipe up() and swipe down() it will scroll the page to the desired location so thereafter the location will be completely visible in screen .

Here,”buttons-id/layout-id” should be the layout which is currently visible on the screen and then it will scroll the screen to the position where you want to perform the action.

3. In espresso if you want to perform any action on any buttons, text field , menu drawer or layout then in three ways you can locate them like

4. To select item from list on particular position in two ways.

In this we can locate  the position where we have to select the item here “10” is the item’s position and “recycler_view_id” is the id of that recycler view .

In this we can locate  the position where we have to select the item here “1” is the item’s position and “adapterview” is the id of that adapter view .

5. Sometimes while editing text in text field and we close the keypad then it will record the script like this:-

So,here while running this script it will try to close the keypad but the keypad is opened only while recording the script not while running the script so it will generate error and the solution for this that remove that “closesoftkeyboard()” then it will run perfectly. Otherwise error will be like this :-

6. Another problem is “withtext” and “replace text” the problem is while recording the test when we clear some already existing text and adding new text so be sure that the text which is already exist that should exist always because while running the script it will search that text in “withtext” then it will replace with new text entered by the user like:-

In this “” is already existing text then while running the script it will search this text and when that text not found then it will through error .so,if there is no text i,e “withtext” then you should remove “withtext” like this:-

So while removing “withtext” it will not search for that text it will directly replace it with new one.


Next I’ll post much more basics of Espresso as well as the errors with solutions .








. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


  • Shubham Agarwal
  • Start a Project

      Message Sent!

      If you have more details or questions, you can reply to the received confirmation email.

      Back to Home