On 9th August 2016, Google held an event in my beloved country, Indonesia. The event was Google for Indonesia. It showed the latest tech of Google to help developers to improve their products. All Google speakers delivered a great knowledge to all audiences.
There is one great tech that stole my interest. It was Espresso Test Recorder. This is a new feature that comes in Android Studio 2.2. As we know Espresso is a library for UI instrumentation testing. With Espresso Test Recorder, we are able to write the espresso test script without writing it!
Sounds impossible? Well, nothing is impossible, remember? 😉
All we need to do is just record the test directly on the device or emulator. Then, the test script will be automagically generated. This, will save our time significantly in writing the test. And more important, this will make your QA Engineer’s happy 😏😁😂
So, how do we use this Espresso Test Recorder? Before running this Espresso Test Recorder we need to:
- Update Android Studio version to 2.2. Version 2.2 is still in beta. The latest beta version for now (28 August 2016) is Android Studio 2.2 Beta 2.
- Add Espresso dependencies to your app. You could find it here.
- Run the Espresso Test Recorder on device or emulator with Android API level 19 (4.4 Kit Kat) and higher. We will get an error if the targeted device or emulator is lower than Kit Kat.
That’s all! The prerequisite is simple, right? Now, we are ready to run the Espresso Test Recorder.
Let’s say we already implemented the prerequisite on a simple app called Email Validation. This app will validate email that input by user. If the email is valid it will show “Valid Email Format”. If the email is invalid it will show “Invalid Email Format”.
Once the app is ready, follow these steps to use Espresso Test Recorder:
- Run the Espresso Test Recorder by clicking menu Run -> Record Espresso Test.
- Then, select deployment target. Let’s choose Genymotion Nexus Kit Kat device for example. Click OK.
- Record the test. Once the Espresso Test Recorder is running, it will show a window to record the test we do on the device or emulator.
- Try testing an invalid email. Tap the email edit text, type any invalid email format, and then click validate. Those activities are recorded by Espresso Test Recorder. The class type along with its ID will be shown while we doing the action on the device.
- Add assertion as we expected by clicking “Add Assertion”. This will obtain screenshot of the current screen from the device or emulator.
- Pick the view element for the assertion. When we input invalid email, we expect that it will show red “Invalid Email Format” text. So, we pick the red text. Assert it with “exists”. Click “Save and Add Another” to add another assertion.
- Assert again with “text is” and type “Invalid Email Format”. Click “Save Assertion” to finish adding the assertion.
- Finally, click “Complete Recording” to finish the recording.
- Then we will be asked for the test class name to generate the test script. Since the activity name is MainActivity, let’s name it MainActivityTest.
- The generated test script will be like this. You could customise it if you need it.
- Run the test, by right clicking on the class name, then choose “Run MainActivityTest”.
Woohoooo! Espresso Test Recorder is awesome, right? Now, we don’t need to spend a lot of time in writing the instrumentation test anymore. Just record it and let Espresso Test Recorder do the rest for you! \:D/