Hi All, In this article, I will explain Espresso Framework which is targeted at developers, who believe that automated testing is an essential part of their development lifecycle. That’s why this automation framework requires mobile app’s source code. I suggest you get into the basics of Android development process before getting into the deep with Espresso.

Espresso’s biggest advantage compared to Appium is its speed. Espresso runs the automation script very fast as test code injects in the source code.

Let’s start with Espresso automation.

First of all, you need to get application’s source code and install Android Studio into your environment. When you import your mobile app source code into the Android Studio, we are ready to go.

Step 1: Dependency Management

Android applications use Gradle as a dependency manager. I am familiar with Maven but it’s not hard to get used to Gradle’s syntax after a few google search.  You need to add those dependencies into dependency part of the gradle file.

After that you need to add instrumentationRunner info to your android->default config part of the Gradle file.

Then you need to synchronise your project to download dependencies to your local environment.

Step 2: Setup of the Espresso Test

You need to add your Activity class’ declaration into your test class as Rule annotation. By this way, the test will know which activity on it will work.

CalculatorActivity is our main page. So we give this object to ActivityTestRule object as a parameter. Now we are ready to work on that activity. In case you need more than one page to work on you should add all of them to your test class as given below. Espresso wants you to set which unit testing framework you will use. That’s why it’s crucial to add below annotation at the top of your test class name.

Step 3: Writing Test Code

You should learn the Hamcrest Matchers to develop your test code. Hamcrest is a framework for software tests. Hamcrest allows checking for conditions in your code via existing matchers classes.

Here you can find Onur Baskirt’s article on Hamcrest Matchers.

I worked on a calculator example and have a performOperationFunction which takes 4 arguments. The first argument is operation id (multiplication, subtraction,etc…), following two arguments are numbers and the last one is result.

Let’s take a look at the function to learn Hamcrest Matchers.

onView = It allows you to work on the specified activity giving on @Rule

withId = It allows you to find a component with its id, there are many functions like withText, withTagKey, withHint, etc…

R.id = It allows you to find any component like textbox, button, spinner etc located on given activity. Component id’s can be found in the Activity as they are declared on that class.

Perform = It allows you to call an action like type text, click, long click, clear text. There are many events you can call. In the example above I have used typeText to enter data, click to click a button.

Check = This method allows you to perform assertions. It might take many arguments described below

Matches = This is the assertion class that might take many arguments to control the state or the value of a component. In the example above it takes a withText method that controls the value of the specific component.

You might want to check if the component is now visible, clickable of checked on the activity, then use matches(doesNotExist()), matches(isClickable()), matches(isChecked()).There are much more checkers

In this example, you will see a closeSoftKeyboard method which is a built-in method to control keyword visibility. As you might know, if keyboard hovers the button you will interact, it might cause some problems that’s why I suggest you close it after the input operations.

Step 4: Running Your Tests

You might run your test on a real device or virtual device. I prefer using a real device. Thus, all you have to do is turning on the developer setting on your Android device. Developer settings location is specific to device or phone brand. You need to google it. After this setting, press the Play button and watch your tests runs like a charm. 🙂

GITHUB LINK: https://github.com/swtestacademy/SWTestAcademyEspresso