Setting Up Appium in Android and Testing a Login Screen Using Kotlin

Gokul Balakrishnan
4 min readMay 31, 2024

--

In this blog post, we will walk through the process of setting up Appium in Android for mobile app testing and then write a sample code for testing a login screen using Kotlin.

Setting Up Appium in Android

Appium is an open-source tool widely used for mobile app testing due to its flexibility, support for multiple programming languages, and cross-platform capabilities. Here are the steps to set up Appium with Android Studio:

Pre-requisites

  • Node.js installed
  • Android Studio installed

Step 1: Install JDK

  1. Download and install the latest JDK installer for your operating system from the Oracle website.
  2. Set the JAVA_HOME environment variable to the JDK installation directory.
  3. Add the JDK bin directory to your system’s PATH environment variable.

Step 2: Setup Emulator with Android Studio

  1. Once Android Studio is installed, open it and follow the setup wizard to install the Android SDK (Software Development Kit) through More Actions > SDK Manager.
  2. Install the Android version that you’d like to install on your emulator under SDK Platform.
  3. Add the Android SDK directory to your system’s ANDROID_HOME environment variable.
  4. To set up an Android Virtual Device (AVD), open Android Studio, click on the More Actions > Virtual Device Manager button, and proceed with the virtual device creation by selecting the hardware and system image.
  5. Run the emulator.

Step 3: Setup Appium

  1. Open a terminal window (or command prompt on Windows) and run the following command to install Appium using NPM.
npm install -g appium
  1. Install Appium Doctor.
npm install -g appium-doctor

Appium Doctor is a command-line tool that helps you diagnose and fix common Appium installation issues.

Sample Code for Testing a Login Screen Using Kotlin

Now that we have set up Appium, let’s write a sample code for testing a login screen using Kotlin. Here is a simple login form example:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.tutorialkart.myapplication.MainActivity">

<LinearLayout
android:id="@+id/ll_main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="#444444"
android:padding="25dp"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:textColor="#6dffbf"
android:padding="30dp"
android:text="Login"/>

<EditText
android:id="@+id/et_user_name"
android:hint="User Name"
android:textColor="#6bfff7"
android:textColorHint="#52afaa"
android:textAlignment="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

<EditText
android:id="@+id/et_password"
android:hint="Password"
android:textColor="#6bfff7"
android:textColorHint="#52afaa"
android:textAlignment="center"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="25dp"
android:orientation="horizontal">

<Button
android:id="@+id/btn_reset"
android:text="Reset"
android:textAllCaps="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<Button
android:id="@+id/btn_submit"
android:text="Submit"
android:textAllCaps="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</android.support.constraint.ConstraintLayout>

The Appium test code is typically not added directly into the Android app’s file structure. Instead, it’s usually part of a separate project or module for testing.

However, if you want to keep the test code in the same project, you can create a separate module in your Android project specifically for your Appium tests. Here’s how you can do it:

  1. In Android Studio, go to File > New > New Module.
  2. Select Java or Kotlin Library and click Next.
  3. Give the module a name, like “appiumTests”, and select Kotlin for the language.
  4. Click Finish.

Now, you have a separate module where you can add your Appium test code. You can create a new package under this module’s src/main/kotlin (or src/main/java if you chose Java) directory and add your test classes there.

Remember, you’ll need to add the necessary dependencies for Appium and testing in the build.gradle file of this new module. Also, since these tests are not unit tests or instrumented tests, they won’t be run with the usual Gradle tasks like test or androidTest. You’ll need to run them separately with your test runner.

Please note that this is just one way to organize your test code, and the exact organization can depend on your specific needs and preferences. It’s also common to have a completely separate project for your Appium tests. This can be beneficial as it keeps your test code separate from your app code, and it can be run independently of your app’s build process.

here is a sample Appium test code in Kotlin for the given login screen:

import io.appium.java_client.MobileElement
import io.appium.java_client.android.AndroidDriver
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.openqa.selenium.remote.DesiredCapabilities
import java.net.URL

class LoginScreenTest {
private lateinit var driver: AndroidDriver<MobileElement>

@Before
fun setUp() {
val capabilities = DesiredCapabilities()
capabilities.setCapability("deviceName", "Your Device Name")
capabilities.setCapability("udid", "Your Device UDID")
capabilities.setCapability("platformName", "Android")
capabilities.setCapability("platformVersion", "Your Android Version")
capabilities.setCapability("appPackage", "com.tutorialkart.myapplication")
capabilities.setCapability("appActivity", "MainActivity")
capabilities.setCapability("noReset", "true")

driver = AndroidDriver(URL("http://127.0.0.1:4723/wd/hub"), capabilities)
}

@Test
fun testLoginScreen() {
val userNameField = driver.findElementById("et_user_name")
val passwordField = driver.findElementById("et_password")
val submitButton = driver.findElementById("btn_submit")

userNameField.sendKeys("admin")
passwordField.sendKeys("admin")
submitButton.click()

// Add your assertions here
}

@After
fun tearDown() {
driver.quit()
}
}

Please replace "Your Device Name", "Your Device UDID", and "Your Android Version" with your actual device name, UDID, and Android version. This code will open the app, fill the username and password fields with “admin”, and then click the submit button. You can add your assertions after the button click to verify the behavior of your app. Remember to start the Appium server before running this test. Happy testing! 😊

--

--

Gokul Balakrishnan

Experienced Android Developer with a demonstrated history of working in the information technology and services industry. Skilled in Java,Kotlin,C#