ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Follow publication

Kakao - how to make UI testing great again

--

We at Agoda pay a lot of attention for different kinds of testing of our software. It helps us to ensure code stability and to early discover possible issues.
Automation testing on Android right now is a little personal hell for all of us. Google provided us with Espresso, several extensions to it, UI automator and more. The tools are great, they do their job, but they do it in the ugly way. When you need to test something more complex than clicking a button, your test code transforms into an unreadable mess. Just take a look:

This looks unreadable to me. We find it very hard to maintain almost 1000 automation tests for our Android app with this kind of syntax. Then Kotlin was announced as a fully supported Android development language at
Google I/O 2017. That’s when we thought “Hey, we may try to migrate our tests to Kotlin!”

But simply migrating our test code base doesn’t solve the issue with the code readability, because we are still tied to Espresso. Fortunately, Kotlin provides us with a lot of tools for building DSLs. With that in mind, we imagined how our UI tests should look like. The above test should look like this in Kotlin:

Pretty neat, isn’t it? Much more readable and understandable than Espresso. And in most cases more compact. Did we achieve this syntax? Sure we did!

Meet Kakao - simple Kotlin DSL for Android AT on Espresso

Kakao provides a fluent way for writing UI tests by abstracting your Activities/Fragments or Views by introducing Screen class.

To start using Kakao, you need to add Kotlin support to your project. You can find all info on that on developer.android.com. Then you simply need to declare your view hierarchy using classes provided by the library. All these classes (KView, KTextView, KButton) are just simple empty classes which implements different interfaces: Actions and Assertions. We also have special class that helps us build your view matcher in DSL style. The class is ViewBuilder and is used in KViews’ constructors.

After defining your view hierarchy, you can access your Screen instance and it’s KViews with the help of invoke operator and perform actions or assertions on them:

We have already migrated a lot of our test code with Kakao and we feel very satisfied with the results. The library also has support of more complex views, such as RecyclerView, ListView, WebView, etc. It has flexible approach to defining custom view classes with different combinations of actions/assertions, with nested views and inner logic.

We decided to go public with this tool, so that other developers can start making their UI tests with Kotlin without the need to stick to old Espresso way of writing tests.

You can grab Kakao by adding dependency to your build.gradle file:

androidTestCompile 'com.agoda.kakao:kakao:1.0.0'

Join us on GitHub, test out Kakao and lets make automation testing great again! Happy testing!

--

--

Published in ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Responses (3)