Espresso Testing and Screen Robots: Getting Started
In this Espresso testing tutorial you’ll learn how to create UI tests with Espresso and how to leverage Screen Robots to make them clear and maintainable. By Victoria Gonda.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Espresso Testing and Screen Robots: Getting Started
25 mins
Writing Screen Robots
As the last two test prove, test writing can be repetitive. You can pull a lot of this duplication out to reuse. One great way to do this is by using Screen Robots.
Screen Robots pull out some of these tasks into helper functions. This makes your tests more expressive, readable and easier to update your if something in the UI changes. For example, if an ID changes, instead of updating it in a bunch of separate tests, you can update it in the one robot.
You can think of a robot as a middle man between your view and a test: It serves the same purpose as a presenter in the MVP architectural pattern. The view itself knows nothing about the test and the test knows nothing about the implementation of the view, it only knows what actions it needs to perform on a view.
By using robots you can easily separate those concerns which gives you the benefit of changing the robot only if your view changes.
Start by creating a class that extends ScreenRobot
. This can be an inner class to your test added to the end of your file outside your test or in a neighboring file. You can make this decision based on if you will want to reuse the robot other places.
As this robot is only used in this test, this project adds it as an inner class to MainActivityTest
:
class CalculatorScreenRobot : ScreenRobot<CalculatorScreenRobot>() {
}
ScreenRobot
, which this class inherits, is another file that’s already defined for you in the app. Take a look at ScreenRobot.kt
if you’re curious. It has some helpful methods you’ll use such as enterTextIntoView()
, clickOkOnView()
and checkViewHasText()
.
Next, write some robots! Start with a simple one to verify the tip amount. Add this to CalculatorScreenRobot
:
fun verifyTipIsCorrect(tip: String): CalculatorScreenRobot {
return checkViewHasText(R.id.textTip, tip)
}
The interface for a robot returns a robot so you can keep chaining actions and assertions together. Here you only use one of the helpers to check that a view has some specific text, then return the ScreenRobot result, this
. If you look into the method, you’ll see a check very similar to the one you’re replacing with it.
To take advantage of this chaining, add an action to enter a check amount and click the 👍 button. Add this to CalculatorScreenRobot
:
fun inputCheckAmountAndSelectOkayButton(input: String):
CalculatorScreenRobot {
return enterTextIntoView(R.id.inputAmount, input)
.clickOkOnView(R.id.buttonOkay)
}
Here you’re chaining the helper functions together to enter text and click on the button.
Using Screen Robots
Now that you have them written, you can replace some of your test code with robots! Replace the body of whenOkayButtonIsPressedAndAmountIsFilledTipIsSet()
with this:
ActivityScenario.launch(MainActivity::class.java)
withRobot(CalculatorScreenRobot::class.java)
.inputCheckAmountAndSelectOkayButton("11")
.verifyTipIsCorrect("1.98")
You initiate using your CalculatorScreenRobot
by using withRobot()
. After that, you can chain your actions and assertions together. See how much more declarative this is! Reading and understanding what is happening is so much easier.
If you run the tests, the result should be the same.
You can use these robots in another place. Replace the body of whenOkayButtonIsPressedAndRoundSwitchIsSelectedAmountIsCorrect()
with the following:
ActivityScenario.launch(MainActivity::class.java)
withRobot(CalculatorScreenRobot::class.java)
.clickOkOnView(R.id.switchRound)
.inputCheckAmountAndSelectOkayButton("11")
.verifyTipIsCorrect("2.00")
Here, you added .clickOkOnView()
from the base class to the chain to turn on that switch. Everything else is the same.
Can you start to see the possibilities in these robots? You can make your robots as simple as clicking a single button or as complex as filling out a complicated form. Use them for whatever makes the most sense in your app to make things reusable and readable.
Where to Go From Here?
Wow! You learned so much in this tutorial. You learned not only how to write Espresso tests, but also how to use Screen Robots to make them shorter and easier to read. You can download the final result using the Download Materials button at the top or bottom of this tutorial.
Itching to practice this more and try out some of the other robot options? As a challenge, you can finish testing this screen. Here are some ideas of what to test next:
- The rest of the views display on launch.
- The result each of the three buttons: Tip, Total and Percentage, display when:
- The input is empty.
- The input is filled and rounding is off.
- The input is filled and the rounding is on.
Sure, you could extract most of this and move it into Unit Tests, but then what would you practice on? You can find a solution to the challenge using the Download Materials button at the top or bottom of the screen.
Play around with test robots to see if they work well for you. It takes time and practice to develop the intuition to know which robots will be most worthwhile and how to write long lasting, maintainable tests.
You can keep on learning in these places, too:
- The Espresso Guides from Google are helpful in understanding how to test your UI well.
- You can learn how to keep your app tested using Test-Driven Development with our tutorial on the topic.
- Take a deeper look into Screen Robots in Using Screen Robots with Espresso with Annyce Davis.
For even more testing practice, check out our book, Android Test-Driven Development by Tutorials.
We hope you found this helpful! If you have any comments or questions, feel free to join us in the forum below.