Picture by Joseph James: Waving farewell ✨

Simplifying Android UI Testing with the Page Object Model and Kakao

Joseph James (JJ)
3 min readJul 16, 2023

--

Introduction

In the world of Android app development, delivering a seamless and intuitive user interface (UI) is paramount. UI testing plays a crucial role in ensuring that the app’s UI functions as intended across various devices and scenarios. However, UI testing can quickly become complex and challenging to maintain, especially as the app grows in size and complexity. To address these challenges, the Page Object Model (POM) in combination with the Kakao Library provides a powerful and efficient approach to UI testing in Android applications. In this blog post, we will dive into the world of UI testing with the POM and explore how integrating the Kakao Library can simplify and enhance the testing experience.

Understanding the Page Object Model (POM)

The Page Object Model is a design pattern that promotes the separation of test logic from UI details. It involves creating dedicated classes, known as page objects, that encapsulate the interactions, elements, and assertions related to specific UI screens or components. The POM offers several benefits, including improved code maintainability, reusability, and readability of UI tests.

What’s Kakao?

The Kakao Library is a popular UI testing framework for Android that provides a fluent and expressive API for interacting with UI elements. It offers a higher-level abstraction compared to traditional UI testing frameworks like Espresso or UI Automator. With Kakao, you can write concise and readable tests by leveraging its intuitive syntax and extensive set of built-in functions.

Implementing the POM with Kakao

To implement the POM with Kakao, you start by creating page objects that represent different UI screens or components in your app. These page objects encapsulate the UI elements, interactions, and assertions specific to their respective screens. By utilizing the Kakao Library’s API within these page objects, you can write clean and modular test code that is easy to understand and maintain. The POM with Kakao promotes reusability, as page objects can be shared across multiple tests, ensuring consistent and efficient UI testing. Let’s dive into an example to make things much clear.

Consider that we are having a Login screen to test,


private const val TAG_USERNAME = "username"
private const val TAG_PASSWORD = "password"

internal class LoginScreen(
semanticsProvider: SemanticsNodeInteractionsProvider
) : ComposeScreen<LoginScreen>(semanticsProvider = semanticsProvider) {

private val userNameTextInput: KNode
get() = child {
hasTestTag(TAG_USERNAME)
}

private val passwordTextInput: KNode
get() = child {
hasTestTag(TAG_PASSWORD)
}

private val signInButton: KNode
get() = child { hasText("Sign In") }

fun expectAllScreenComponents() {
userNameTextInput.assertIsDisplayed()
passwordTextInput.assertIsDisplayed()
signInButton.assertIsDisplayed()
}

fun inputUserCredentials() {
fillUserName()
fillPassword()
}

private fun fillUserName() {
userNameTextInput.performTextInput("username")
}

private fun fillPassword() {
passwordTextInput.performTextInput("password")
}

fun clickSignInButton() {
signInButton.performClick()
}
}

internal fun onLoginScreen(
composeTestRule: SemanticsNodeInteractionsProvider,
function: LoginScreen.() -> Unit
) = ComposeScreen.onComposeScreen(composeTestRule, function)

In this context, the screen class provides a comprehensive overview of the components within the Login UI. Essentially, we expose specific functions that enable various actions, which can be combined to form a cohesive test. Now, let’s explore how we can seamlessly integrate this into the test class.


internal class SignInTest : TestCase() {

@get:Rule
val composeTestRule = createComposeRule()

@Test
fun testSignIn() {
composeTestRule.setContent {
SignInScreenComposable() // Rendering signIn screen composable
}

onLoginScreen(composeTestRule) {
expectAllScreenComponents()
inputUserCredentials()
clickSignInButton()
}

// This is the screen which is show imediately after the signin process.
onMainScreen(composeTestRule) {
expectAllScreenComponents()
}
}
}

If we consider TDD (Test-Driven Development), implementing the Page Object Model (POM) for UI tests greatly simplifies the task for code reviewers. By examining the tests alone, individuals can easily grasp the entire workflow. The readability that this library offers is truly remarkable, as demonstrated in the aforementioned example.

Conclusion

By adopting the Page Object Model with the Kakao Library for UI testing in Android applications, developers can simplify their testing efforts, improve test maintainability, and enhance overall test code quality. The POM promotes a modular and reusable approach to UI testing, while Kakao provides a user-friendly API and powerful features for seamless interaction with UI elements. Together, they empower developers to create robust and reliable UI tests that align with best practices. Incorporating the POM and Kakao into your testing workflow is a step towards ensuring a delightful user experience in your Android applications.

--

--

Joseph James (JJ)
Joseph James (JJ)

No responses yet