If you are a Mobile Apps Test Engineer, you cannot overlook the very reliable Google Product i.e. Espresso.
Espresso is an automatic UI testing or as we call it “hands free testing” framework. This is not entirely true as you still have to use your hands to write a code. Once you’ve written the code, Espresso will execute that using advance essentially. It will do all the tasks for you such as clicks on button. It will also swipe and do various things that a normal user does and then verify the test too. So that way, Espresso is pretty exciting.
- There are many other UI testing libraries out there, but Espresso is special because it is a first party library i.e. google wrote it under the android testing support library.
- It does automatic synchronization of the test action with the app UI which means, Espresso will watch the UI thread which is the thread that renders all the pixels on the screen. So while the pixels render on screen Espresso will sleep for you, until the UI thread is idle.
- Easy to set up, fast execution.
- The API is relatively small, yet open for customization.
- Hands free testing
Here is how we can get going with Espresso.
Adding Espresso to your project
- First of all make sure you have Android Support Repository installed
- Add the following dependencies to your application build. gradle file.
- Finally, specify the test instrumentation runner in default config in build. gradle file.
Configure Device Settings
Turn off animations on your test device — To avoid unexpected results, turn off animations from Settings by opening Developer options and turning all the following options off:
- Window animation scale
- Transition animation scale
- Animator duration scale
Main Components of Espresso
Espresso has Three Main Components:
- ViewMatchers– allows you to locate a view in the current view hierarchy (eg. Find Something)
- ViewActions– allows you to interact with views(eg. do something)
- ViewAssertions– allows you to assert the state of a view.(eg. check something)
Now, let’s find out how these components find their place. This is what I call ‘the formula’.
My first Espresso test
In this example, I take one edit text and one button. Now, we have to ensure that on onButtonClick(), the input string matches with the given string.
Here is my code –
By using ActivityTestRule, espresso framework launches the activity under test before each test method annotated with @Test and before any method annotated with @Before. In my case, HelloWorldDemo is my LauncherActivity.
The method under annotation @Before basically execute before the @Test Method executes.
This is the Test case with Annotation @Test
Well, all set now. Right click on your Test and Select Run. Following Screen will come as a result Screen.
- Hadoop Cluster Verification (HCV)
Verification scripts basically composed of idea to run a smoke test against any Hadoop component using shell script. HCV is a set of artifacts developed to verify successful implementation of…
- Mobile App Development and Continuous Delivery – SonarQube (2/7)
The next step in the process of setting your Mobile DevOps environment is to get hold of your code quality. I prefer SonarQube in the DevOps tool chain to evaluate…
- Don’t be a data hog! – Designing Android Applications
There are tons of mobile apps out there in the market, each specializing in their own area. There are a plenty of apps built on a single idea, around the…
- A Know-how of Timesheet Management via Chatbot
SAP Timesheet Management using Chatbot is an application, which helps users in managing their time entries quickly and efficiently in real-time through mobile phones. Chatbot is an AI-powered computer program…
What is ReactJS? ReactJS is a front-end library developed by a collaboration of Facebook and Instagram. It enables users to create interactive, stateful, and reusable UI components. In 2003, Facebook…
- Data Ingestion Platform(DiP) – Real time data analysis – Flink Streaming
This blog is an extension to that and it focuses on using Flink Streaming for performing real time data ingestion. The previous blog DiP (Storm Streaming) showed how we can leverage the power of…