Kakao


Source link: https://github.com/agoda-com/Kakao

Kakao

Nice and simple DSL for Espresso in Kotlin

Introduction

At Agoda, we have more than 1000 automated tests to ensure our application's quality and give our best experience to the user. All of them are written with 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.

onView(allOf(withId(R.id.price_item), hasDescendant(withText("Standard Rate"))))

.check(matches(withEffectiveVisibility(Visibility.VISIBLE)));

This is an example just to check the visibility and you can see that it's not looking that good. As Agoda 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.

Benefits

  • Readability
  • Reusability
  • Extensible DSL

How to use it

Create Screen

Create your entity Screen where you will add the views involved in the interactions of the tests:

class FormScreen : Screen<FormScreen>(){

}

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.

Create KViews

Screen contains KViews, these are the Android Framework views where you want to do the interactions:

class FormScreen : Screen<FormScreen>(){

  val phone = KView {
 withId(R.id.phone)
}

  val email = KEditText {
 withId(R.id.email)
}

  val submit = KButton {
 withId(R.id.submit)
}
 
}

Kakao provides different types depending on the type of view:

  • KView
  • KEditText
  • KTextView
  • KButton
  • KImageView
  • KWebView
  • KCheckbox
  • KViewPager

Every KView contains matchers to retrieve the view involved in the ViewInteraction. Some examples of matchers provided by Kakao:

  • withId
  • withText
  • withContentDescription
  • withDrawable
  • withBackgroundColor

Like in Espresso you can combine different matchers:

val email = KEditText {

withId(R.id.email)
  withText(R.string.email) 
}

And you can use your custom matchers:

val email = KEditText {

withId(R.id.email)
  matches {
 MyCustomMatcher.matches(position) 
}
 
}

Write the interaction.

The syntax of the test with Kakao is very easy, once you have the Screen and the KViews defined, you only have to apply the actions or assertions like in Espresso:

val screen = FormScreen() screen {

  phone {

  hasText("971201771")
  
}

  button {

  click()
  
}
 
}

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:

val screen = FormScreen() screen {

  phone {

  assert {
 MyCustomAssertion.isThaiNumber() 
}

  
}

  button {

  act {
 MyCustomAction.clickOnTheCorner() 
}

  
}
 
}

Advanced

ListViews/RecyclersViews

Kakao offers an easy way to interact with your RecyclerViews and ListViews

Create the KListView/KRecyclerView

Inside your Screen create the KView matching with your view:

For KListView:

val list = KListView ({

builder = {
 withId(R.id.list) 
}
 
}
)

For KRecyclerView:

val myList = KRecyclerView ({

  builder = {
 withId(R.id.recycler_view) 
}
 
}
)

You can combine different matchers to retrieve your view.

Create KAdapterItem/KRecyclerItem

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

class Item(i: DataInteraction) : KAdapterItem<Item>(i) {

  val title = KTextView(i) {
 withId(R.id.title) 
}

  val subtitle = KTextView(i) {
 withId(R.id.subtitle) 
}

  val button = KButton(i) {
 withId(R.id.button) 
}
 
}

KRecyclerItem

class Item(parent: Matcher<View>) : KRecyclerItem<Item>(parent) {

  val title: KTextView = KTextView(parent) {
 withId(R.id.title) 
}

  val subtitle: KTextView = KTextView(parent) {
 withId(R.id.subtitle) 
}
 
}

The KView defined in the Item corresponds views used on the Item. You can assign the KItems to the KListView/ KRecyclerView like:

val recycler: KRecyclerView = KRecyclerView({

  withId(R.id.recycler_view) 
}
, itemTypeBuilder = {

  itemType(::Item) 
}
)

And finally your final interaction will be:

screen {

  recycler {

firstChild<TestRecyclerScreen.Item> {

 isVisible()

 title {
 hasText("Title 1") 
}

}

  
}
 
}

Kakao provides different accessors in the adapter:

  • childAt
  • firstChild
  • lastChild
  • childWith
Custom KViews

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.

class KMyView : KBaseView<KView>, MyActions, MyAssertions {

  constructor(function: ViewBuilder.() -> Unit) : super(function)
  constructor(parent: Matcher<View>, function: ViewBuilder.() -> Unit) : super(parent, function)
  constructor(parent: DataInteraction, function: ViewBuilder.() -> Unit) : super(parent, function) 
}

Setup

Maven

<dependency>
<groupId>com.agoda.kakao</groupId>
<artifactId>kakao</artifactId>
<version>1.0.1</version>
<type>pom</type> </dependency>

or Gradle:

repositories {

  jcenter() 
}
 dependencies {

  // For Gradle Version below 3.0.0
  androidTestCompile 'com.agoda.kakao:kakao:1.0.1'

// For Gradle Version 3.0.0 or above
  androidTestImplementation 'com.agoda.kakao:kakao:1.0.1' 
}

Contribution Policy

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.

Code of Conduct

Please refer to Code of Conduct document.

License

Kakao is open source and available under the Apache License, Version 2.0.

Thanks to

Resources

Android Library that provide simpler way to start the Activities with multiple arguments.

Field and method binding for Android Activity arguments, which uses annotation processing to generate boilerplate code for you, and:

Eliminate all putExtra and getXXXExtra methods. Allows you to forget about all keys that were used to pass agruments. Support flags and Intent provide.

ChipCloud creates a cloud of "chips" as per material specifications, as below

A fork from another library, it provides customizations for typeface, text size, vertical and horizontal spacing, all caps and gravity apart from different select modes and selected/deselected text and background colors which the original library provided. You can also update your labels in-place, if your use case requires it.

A Floating and Draggable KeyboardView for android that several EditText's can register to use it.

This is a lightweight Logger to file for Android. Logging to file is a common task and there is no good solution to do it effectively. This library does it for you.

Swipe-able view, which allows users to perform actions by swiping it to the left or right side.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes