Espresso Quick Start Guide for Android UI Automation


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

javafx

Database Operations in JavaFX

By Onur Baskirt / Apr 1, 2016 / 56 Comments
Before started this section, please check the first article and learn How to Start JAVAFX! http://www.swtestacademy.com/getting-started-with-javafx/ At first, part of JavaFX tutorial series, we created a sample JavaFX project, designed the draft version of the UI and set up an...
extentreports

How to Write Smart XPath Locators

By Onur Baskirt / Sep 24, 2017 / 5 Comments
Hi all, in this tutorial, I will describe you how to write smart and non-brittle XPath locators. When we write our test scripts, we generally prefer to use id, name, class, etc. these kinds of locators. However, sometimes we could not...
extentreports

How to Write Effective CSS Locators

By Onur Baskirt / Oct 1, 2017 / 0 Comments
Hi all, in this tutorial, I will describe you how to write effective CSS locators to interrogate web elements for your automation projects. As a rule of thumb, your interrogation strategy should be in below order: First try to use...
rest assured

REST API Testing with Rest Assured

By Onur Baskirt / Mar 8, 2016 / 32 Comments
Outline In this post, I will explain what is API and API testing, what is the difference between SOAP and REST services, and how to test REST APIs with Rest Assured Library. What is API? API stands for Application Programming...
javafx

Getting Started with JavaFX

By Onur Baskirt / Mar 25, 2016 / 0 Comments
When I started to work in my current position, one of my task is to do manual operations for campaign products  every week. After the second week, I thought that I have to automate this task using a GUI based...
extentreports

Selenium-1: Quick Start to Automation with Selenium WebDriver & JAVA & JUnit & Maven & IntelliJ

By Onur Baskirt / Sep 8, 2015 / 26 Comments
Outline Selenium Webdriver is the most popular open source web test automation framework across the wide range of browsers and platforms. In this tutorial, you will learn how to do web test automation with Selenium Webdriver and the related tools....
extentreports

Selenium-11: Execute JavaScript with JavascriptExecutor

By Onur Baskirt / Jan 27, 2016 / 15 Comments
Outline Sometimes we cannot handle some conditions or problems with Webdriver, web controls don't react well against selenium commands. In this kind of situations, we use Javascript. It is useful for custom synchronizations, hide or show the web elements, change...
page object model

Page Object Model with C#

By Ege Aksoz / Jun 18, 2017 / 8 Comments
In the previous tutorial, we’ve taken the initial steps and entered the world of automated testing. We also wrote our first automated test. From this point on, since we are not just going to write one test, we need to...
extentreports

How to Select a Date From DatePicker Using Selenium

By Onur Baskirt / Aug 13, 2016 / 6 Comments
When you need to automate a airway, hotel, or similar websites you need to deal with Datepickers and some times it is a little bit cumbersome to select a specific date on the Datepicker or calendar.  In this post, I...

Getting Started with RobotFramework on Windows

By Onur Baskirt / Apr 25, 2016 / 12 Comments
What is RobotFramework? RobotFramework is a GENERIC test automation framework for acceptance testing and acceptance test-driven development (ATTD). What it means that you can do web, mobile, desktop and other test automation activities with related test libraries. These libraries can...
By | 2017-03-23T22:31:16+00:00 March 23rd, 2017|Mobile Automation, Test Automation|0 Comments

About the Author:

Canberk Akduygu
Canberk Akduygu is working as a Technical QA Architect on test automation and performance testing. Formerly, he worked at TEB as QA Project Lead, Ayrotek IBM Business Partner and Yapi Kredi Bank as Java Developer. You can also follow him on Linkedin.

Leave A Comment