Writing Automated Tests in Kotlin

Daniel Ekow Melfah
4 min readNov 29, 2021

Kotlin is a programming language that has been abstracted to the point that it can read like plain English. Kotlin helps a lot of devs tp easily refactor their whole codebase and maintain the code much more easily. Learning Kotlin is like learning any other programming language. You need to understand the syntax and also know some exceptions. Once you are comfortable using Kotlin, you’ll appreciate programming a lot more and write code with greater efficiency. You can get started by taking these exercises.

We’re going to go through writing android tests using Kotlin.

Prerequisites

1. Install the Android Studio IDE or Intellij IDE. One of the things to do to start,

2. Enable offline builds by clicking on the Toggle offline mode button.

3. Switch to feature/Clipboarde2e branch

4. Run npx react-native bundle — platform android — dev true — entry-file index.js — bundle-output android/app/src/main/assets/index.android.bundle — assets-dest android/app/src/main/res/

5. Open the ./android directory as a project in AS

6. Wait for Gradle to finish syncing (wait until the loading indicators on the bottom of the page go away)

7. Configure .env.local file

8. Click on Build variants on the left side of AS

Diving into the codebase

Once yarn is done, inside the project there is an android project. We need android studio pointing, not to the Clipboard project but android folder.

Once that is done, we can go through the app source directory and go deeper and deeper to find the test files. In the test files, there is the first test.

We want to start off with the Start ClipboardApp() function. We have the function so that different screens will use their own functions. This function contains all the desired capabilities for setting android testing.

For the SignInScreen(), we want to call from other screens, so that the code does not become duplicitous.

We wrote this function so that we don’t have to write it every single time. It looks a lot cleaner and a lot easier to quickly place things as we add new items to the screen. One thing with Kotlin is that if we don’t like the way it looks, we can generate it so it will read like English.

eg. val wasFound = waitForexist(waitTimeout)

if(!wasFound) throw Error(“Did not find button”)

We can also add our email and password to the .env file to override configs in the sign-in file. If we want to use DM1, we will use the DM1 env file.

We also need to check to make sure that the screen that we are on is actually the sign-in screen.

If want to have an object that we want to use, we have to make a list of helper methods that will give us specific things to do like enter text in a button.

In AS we have a play button next to tests classes to run all the tests. If you run it for a specific test, it plays the test for only the specific test.

NB: Unfortunately, RN libraries take quite a lot of time when building.

Tooling

Another tool we need in the initial set up is we need to get uiautomator viewer. uiautomator viewer gives us more information that makes sense in the native mobile mode. You can use uiautomator viewer on any screen.

Use uiautomator viewer to take a screenshot of any screen on android. After the screenshot is taken, you can view anything on that screen. Then you can use the information to write your tests. Let’s say you want to write your own view and screen you find the text or class using uiautomator viewer. Then find the packageName and when you are looking at this screen, it will give you the selectors for that screen.

NB: When using UIautomator viewer, usually, resource-ids are enough

Writing the first android test file

1. Duplicate any of the test files

2. Replace the test functionality with what is in the parent file

3. Make a new kotlin file class and make it an object

in the screen file,

object [ScreenName]: Screen(){

private val rootView get() = findViewGroup {descriptionContains()TODO()}

NB: You can use TO DOs to create a whole bunch of items

[actions]

}

4. Copy actions object into the new screen file [actions] section

5. Add object selectors

--

--