Head Start Espresso Android UI Test— From Scratch
Being a Software Developer and especially into mobile I always felt missing out something of not doing UI Testing but till when? Either it’s interviews, or any events, or regression bugs I always came across same topic and asked same question repeatedly to myself — When will I start writing UI Test cases?
and Here’s the day 🤓 But I must tell you if you’re reading this then Do it Right Now — Do Not Skip It!
Let’s start now
UI Test I believe you know the meaning from the name But again I’m re-iterating it:- It means to test the UI of any feature / or any screen
But just to ease the process of writing UI Test cases we have some frameworks available, Espresso is one of those
What is Espresso?
The Espresso testing framework provides a set of APIs to build UI tests to test user flows within an app. Espresso allows developers to automate the testing process so that it can be tested at scale and much more efficiently.
- Language Supported — Java and Kotlin
- By Google — For Android
- To write automated UI tests
I’ll directly dive in to Espresso instead of writing what dependencies we need cause that can be easily found on Google :)
Components of Espresso
Lil Confused? Don’t worry I’ll make it easy for you, Here you go:-
- onView(), onData(): Entry point to interactions with views
- ViewMatchers: allows you to find a view in the current view hierarchy. Simply it refers to “find something”.
- ViewActions: allows you to perform actions on the views. Simply it refers to “do something”.
- ViewAssertions: allows you to assert the view state. Simply it refers to “check something”.
Cleared, No? No worries Let’s think in layman’s term with an example:-
USE-CASE:-
You’ve built a Login Screen, which usually contains email id/phone number, password and a sign in button
Now what could be the possible UI Test cases?
- Activity is launched?
- If email Id is in wrong format then validation message is displayed or not?
- If password is not strong then validation message is displayed or not?
- If both emailId and password is in correct format then SignIn button got enabled or not?
- On click of SignIn button Home screen activity/Intentional activity is displayed or not
Thats it! This is same we have to do in UI Testing, Think of above use case steps and go one by one:-
- Firstly we have to write code for launching the activity
- Then we have to find EmailId View where onView() and ViewMatchers comes into picture, then we have to type in Email Id Input text where ViewActions comes into picture and once done, then we have to find another textview of validation message, post that check if that view’s visibility is shown or not where ViewAssertions comes into picture— Simple? and same goes for Password / Sign in Button
Too Much Theory? No worries we’ll do that practically But Let’s Quickly go-through with the steps to write Espresso
I believe that you might have got a clear picture as of now. Let’s do see some Examples:-
Example of Test cases:-
Here you can see 2 basic Test cases:- One is for MainActivity Launch and second is for Launch the MainActivity, Find View with id Fab, and then check if that View is present/displayed in current screen or not
Simple? This is how we can write very basic UI Test Cases
Need More Detailed Example? Look into this below:-
Let’s see what we have done in above example/test case:-
- Firstly launch MainActivity
- Find Fab View and Perform Click Action on that
- After that Find EditText View named edit_word and perform typing in that
- After that Close Keyboard
- Then Find Button View with name button_save and Perform Click Action on that
I hope that you’ve got great idea, Let’s go more deeper and learn how we can Test Recyclerview, Interesting ?
Recylerview Testcases:-
Again this is also so simple, I’ll explain it very quickly
- Launch Activity
- Find View (Note:- Here View is Recyclerview)
- Perform Action of Scroll here and find a Row with Text “Oreo”
- Can also perform Click Action by using same click() function
If Present then Test Case is Passed otherwise Failed — Yohoooooooo! 🤩
Espresso One More Use Case
Do you want something more interesting? No worries I’ve test cases for menus also.
Test cases for Menus in Android
This is how you can write test cases for Menus. Process followed is almost same from launching the activity to find the view by id or by text.
Note:- If you want to click or find overflow menu then espresso have inbuilt api called openActionBarOverflowOrOptionsMenu() which requires application context.
This is the Beauty of using Espresso, we’ve plenty of functions/Api’s available to use to make ui testing process much easier.
Do you want to go more deeper or want to try out by yourself? No Worries that too I Sorted it out
Here’s my Github Link for Espresso — Go and Check it out!
I hope you enjoyed reading this article and got some good insights.
Read my one of the popular and growing article which can be helpful for you:-
Android App Coding Best Practices
Share this blog to spread the knowledge | Do Follow and CLAP if you Liked it! Happy Coding
Watch my Youtube video on UI Testing at CodingWithPuneet
Add me on Linkedin and Twitter for some interesting updates 🤓