Hello UI testing in Android

Junaid Ahmad
2 min readSep 18, 2021

--

In this article we will be looking into the Button UI automation using Kakao DSL. Please check my article for the basics of UI testing in Android.

As you can see in below picture, we have a button which prints `Hello World` text on the screen on click event. So let’s get started…

Setup

Add the following dependency to the module build.gradle file

androidTestImplementation 'io.github.kakaocup:kakao:3.0.4'

Create Screen Class

We will need to write a class MainActivityScreen.kt inside androidTest directory to get the references of actual UI views to perform some necessary assertions and events.

import io.github.kakaocup.kakao.screen.Screen
import io.github.kakaocup.kakao.text.KTextView

class MainActivityScreen : Screen<MainActivityScreen>() {

private val textViewTitle = KTextView {
withId(R.id.textView_title)
}

private val textViewDescription = KTextView {
withId(R.id.textView_description)
withText(R.string.print_hello_description)
}

private val buttonPrintHello = KButton {
withId(R.id.button_printHello)
withText(R.string.button_print_hello_text)
}
}

You can see we are getting the references of views from our UI with the help of Kakao DSLs KTextView, KButton

Let’s also add some assertions to the `MainActivityScreen` class.

import io.github.kakaocup.kakao.screen.Screen
import io.github.kakaocup.kakao.text.KTextView

class MainActivityScreen : Screen<MainActivityScreen>() {

...
fun assertInitialStateOfMainScreen() {
textViewTitle.isVisible()
textViewTitle.hasEmptyText()
textViewDescription.isVisible()
buttonPrintHello.isVisible()
buttonPrintHello.isEnabled()
}

fun performClickOnHelloButton() {
buttonPrintHello.click()
}

fun assertFinalStateOfMainScreen() {
textViewTitle.isVisible()
textViewTitle.hasText(R.string.hello_word_title)
textViewDescription.isVisible()
buttonPrintHello.isVisible()
buttonPrintHello.isEnabled()
}
}

assertInitialStateOfMainScreen() verifies whether our UI is in default state.

performClickOnHelloButton() performs click event on button

assertFinalStateOfMainScreen() verifies whether our UI is in expected state.

Write UI Test

Now we will consume the screen class we created in step 2 to automate button click event by writing a UI test. Let’s create a class MainActivityInstrumentationTest.kt inside androidTest directory to write our UI test.

import androidx.test.core.app.ActivityScenario
import io.github.kakaocup.kakao.screen.Screen.Companion.onScreen
import org.junit.Before
import org.junit.Test

class MainActivityInstrumentationTest {

@Before
fun setup() {
ActivityScenario.launch(MainActivity::class.java)
}

@Test
fun printHelloTest() {
onScreen<MainActivityScreen> {
assertInitialStateOfMainScreen()
performClickOnHelloButton()
assertFinalStateOfMainScreen()
}
}
}

MainActivity is being launched before every test case. And inside the test method we are calling the screen class methods inside the scope of onScreen<ScreenClass>{...}

LET’S RUN THE TEST 😎

Here is the Github repo link android-button-ui-test

--

--

Junaid Ahmad
Junaid Ahmad

Written by Junaid Ahmad

Android Engineer @Callsign — I write about new things in Android.