Begin your Android automation testing using Appium, JUnit5, and Kotlin in 5 minutes on Windows or Linux!

Benjamin LASCHKAR
7 min readSep 1, 2022

--

Automatic regression testing is almost mandatory when you are developing an application with many features. It consists of testing a functionality over time and making sure it does not break.

At Baracoda we have a strong culture of testing and each team is already writing unit tests (extensively) and UI tests maintained by the dev teams. Those tests are using mock and simulator.

In order to save time, avoid repetitive tasks, and increase efficiency and productivity, the QA team also started to create automation tests but with real devices. Those tests are end to end without any mock.

These tests are intrinsic to the overall quality of the application because if they are implemented using continuous integration tools (such as Jenkins or CircleCi) in order to create nightly, it allows every team to see regressions very quickly so they can prevent many bugs in the long run.

However, this process is complex to set up because you need the right tools to automatically interact with the application on a multitude of different devices, and creating these tests is also time-consuming and requires regular maintenance.

We use Appium together with JUnit 5 and Kotlin to achieve that goal.

Kotlin is a first-class citizen at Baracoda, we have a lot of experts! It is a concise, safe, and really modern programming language. In this project, we use a JVM so it can work with any OS and use any java libraries.

JUnit is a framework for unit testing. With it, it is easy to manage a rich suite of tests, especially regression tests. JUnit has become a standard for testing, it is supported by all IDE and by a great community.

Appium is a test automation framework, particularly used for mobile automation. It can simulate any gesture or any command on any platform. It is built on the idea that you don’t need to rebuild your app to test it and is very easy to set up.

In this article, we are going to see how to configure a project to start to automate tests of your android application. We are going to take the Google Android calculator as an example and we are going to test a simple operation with it. With automation we want to test : 8 x 5 = to see if it’s equal to 40!

This project is available here. But if you really want to fully understand it, follow this quick tutorial!

Let’s set up this project together!

You must have the Android SDK: https://developer.android.com/studio

You also need to download Appium, you can get it here: https://appium.io/

There is also a very fast way to get it if you have npm:

npm install -g appium

Create a project, select Kotlin and Gradle, here I am using Intellij IDEA as an IDE :

In the build.gradle we add those dependencies to have Appium and JUnit :

Now we are creating a new Kotlin Class called AppiumSetupin the test folder.

The purpose of this class is to set up which device and which application we want to test. It is going to be a base class for all test classes.

Everything in this class is going to be in a companion object because all method and attribute are statics.

We need to set up an appium driver and all appropriate capabilities to interact with an Android app.

All capabilities are defined here: https://appium.io/docs/en/writing-running-appium/caps/

To make it simple we are going to define only those 4:

1 ) App Package: Package of the app.

2 ) Activity Name: Full path to Activity that we want to test, i.e. com.package.SampleActivity.

You can get both of them by doing an `adb logcat | grep LAUNCHER` and launching the activity.

Below you can see the package marked as green and the activity marked as red for the Google calculator application.

3 ) Automation Name: Automation engine that appium is using.

4 ) Platform Name: Mobile OS platform that appium is using.

Then update all capabilities using the @BeforeAll of JUnit.

To avoid any error with multiple launches don’t forget to quit the driver at the end, it is needed to be done in the @AfterAll of JUnit because this method is launched after all tests, even if a test failed.

Let’s see how we can get the iD of what we want to click on!

In order to use any button with appium and simulate a gesture, we need to get the ID of what we want to click on.

You need a tool. Appium inspector (for windows or mac) or UIAutomator (Linux) to get them!

If you are on Linux you can find UIautomator in android_sdk_path/tools/bin/UIautomator

Linux users

Download Google Calculator on the Google Play Store with your Android device.

Plug your Android device into your computer and verify that you are connected with adb by doing an adb devices

Launch UIAutomator and click on the “Device Screenshot” button.

UiAutomator is taking an “xml screenshot” of your device so if you have the google calculator open you are going to have this :

Now if you click on any element you can get the resource-id of that element.

Here we can see that the resource-id of the button 8 is “com.google.android.calculator:id/digit_8

Mac or Windows User

Very similar to Linux but we use AppiumInspector.

Plug your Android device into your computer and verify that you are connected with adb by doing an adb devices .

Launch the Google calculator app on your device and run the appium server.

Either with the command line appiumif you are using npm or with the GUI and the button “Start Server”.

Then launch AppiumInspector and enter the following parameters:

“platformName” : “Android”
“automationName” : “UiAutomator2”

Also, set up the remote path with: /wd/hub

And click on start session.

If you click on any element you can get the resource id of that element.

Here we can see that the resource id of the button 8 is “com.google.android.calculator:id/digit_8

Now let’s implement a simple test for the google calculator application!

Consider that we have all the ids that we need to make a simple multiplication test : [5], [x], [8], and [=]

A good practice is to create another file called ViewIds.ktwhere we define all those const values:

You can define classes to group your tests, in this example, the class MultiplicationTests is the class where all tests of multiplication are done.

Finally! We can create a test where we are checking that a simple multiplication: 5 x 8 is 40!

We are using two functions .findElement(By.id(X)) and .click() to make the operation.

Then we store the result that is appearing on the TextView in the variable result. In this case, there is only one TextView on the screen so we don’t have to specify the one we are looking.

And finally, we assert that this variable is equal to 40 otherwise the test fails.

Perfect! Let’s launch our first test!

To do that ensure 2 things:

  • Your device is connected with adb to your computer and you can see it with adb devices .
  • Your appium server is running.

Now that everything is configured you can run this test. On the first run, it is going to install “Appium Settings” on your device and the test will be launched right after.

You can click on the green icon near the test to launch it or launch it via the command line with :

./gradlew test

And voila! As expected 8 x 5 equals 40!

A JUnit report has been generated, you can find it under : .\build\reports\tests\test\classes\MultiplicationTests.html

Congratulations, you successfully launched your first appium test!

The full project is available here.

Thanks to Baracoda

--

--

No responses yet