Nice and simple DSL for Espresso in Kotlin
In many projects we have large amount of automated tests to ensure our application's quality and give our best experience to the user. The most famous library for writing automated tests is Espresso from Google. Even though Espresso is working really well with our test, the code readability is quite low. Let's look at some of the examples of how we write the test.
This is an example just to check the visibility and you can see that it's not looking that good. As a developers, we want to improve not just our codebase quality, but also our implementation of tests as well. This is why we are introducing Kakao. The library that will make you enjoy writing automated tests like you are drinking a hot chocolate.
- Readability
- Reusability
- Extensible DSL
Create your entity Screen where you will add the views involved in the interactions of the tests:
Screen can represent the whole user interface or a portion of UI. If you are using Page Object pattern you can put the interactions of Kakao inside the Page Objects.
Screen contains KView, these are the Android Framework views where you want to do the interactions:
Kakao provides different types depending on the type of view:
- KView
- KEditText
- KTextView
- KButton
- KImageView
- KWebView
- KCheckbox
- KViewPager
- KSeekBar
- KSwitch
- and more
Every KView contains matchers to retrieve the view involved in the ViewInteraction. Some examples of matchers provided by Kakao:
- withId
- withText
- withContentDescription
- withDrawable
- withBackgroundColor
- and more
Like in Espresso you can combine different matchers:
And you can use your custom matchers:
The syntax of the test with Kakao is very easy, once you have the Screen and the KView defined, you only have to apply the actions or assertions like in Espresso:
Kakao provides multiple actions/assertions based on Espresso. Furthermore, you can combine them, just like the matchers. You can use your custom assertions or your custom actions too:
Kakao offers an easy way to interact with your RecyclerView and ListView
Inside your Screen create the KView matching with your view:
For KListView:
For KRecyclerView:
You can combine different matchers to retrieve your view.
Every adapter contains different Items, Kakao provides an easy way to define the different items of your adapter with KAdapterItem and KRecyclerItem. If your adapter contains multiple Items but your interactions in your tests only work with one is not required to create all of them.
KAdapterItem
KRecyclerItem
The KView defined in the Item corresponds views used on the Item. You can assign the KItems to the KListView/ KRecyclerView like:
And finally your final interaction will be:
Kakao provides different accessors in the adapter:
- childAt
- firstChild
- lastChild
- childWith
If you have custom Views in your tests and you want to create your own KView, we have KBaseView. Just extend this class and implement as much additional Action/Assertion interfaces as you want. You also need to override constructors that you need.
See Kakao-ext-clicks
If you need to add custom logic during the Kakao -> Espresso call chain (for example, logging) or if you need to completely change the ViewAssertion or ViewAction that are being sent to Espresso during runtime in some cases, you can use the intercepting mechanism.
Interceptors are lambdas that you pass to a configuration DSL that will be invoked before ViewInteraction, DataInteraction or Web.WebInteraction classes' perform and check calls happening from inside Kakao.
You have the ability to provide interceptors at 3 different levels: Kakao runtime, your 'Screen' classes and any individual KView instance.
On each invocation of Espresso function that can be intercepted, Kakao will aggregate all available interceptors for this particular call and invoke them in descending order: KView interceptor -> Active Screens interceptors -> Kakao interceptor.
Each of the interceptors in the chain can break the chain call by setting isOverride to true during configuration. In that case Kakao will not only stop invoking remaining interceptors in the chain, but will not perform the Espresso call. It means that in such case, the responsibility to actually invoke Espresso lies on the shoulders of the developer.
Here's the examples of intercepting configurations:
For more detailed info please refer to the documentation.
Maven
or Gradle:
Kakao is an open source project, and depends on its users to improve it. We are more than happy to find you interested in taking the project forward.
Kindly refer to the Contribution Guidelines for detailed information.
Please refer to Code of Conduct document.