Testing plays a crucial role in the development of an app and, done well, can save an enormous amount of time. Repeating tests by hand is expensive and time-consuming, which is why test automation frameworks were created. Appium is one of the most popular test automation frameworks in the mobile world. It is an open-source framework for native, hybrid and mobile web apps, and it is very well documented. Appium is a powerful framework that lets you write tests independently of your app. Tests can be written in Java, Objective-C, JavaScript (Node), PHP, Python, Ruby, C#, Clojure or Perl, using the Selenium WebDriver API and language-specific client libraries.
How do you use Appium?
Appium comes with three tools:
Appium server
The Appium server is an HTTP REST API responsible for detecting and communicating with test devices. Appium supports both emulators and physical devices. Today, Appium is integrated into GitLab CI, AWS, Bitrise and other cloud applications. If needed, you can also use the API to write your own integration from scratch.
Appium desktop
Desktop is an app for Mac, Windows and Linux that wraps the power of the Appium automation server in a clean, flexible UI.
Appium Inspector
Appium inspector is a tool for inspecting the elements of a mobile app. When writing mobile tests, you need to identify UI elements by certain IDs. With Appium inspector you can find those IDs without needing the source code.
Once the Appium server is running, you can communicate with the API through one of the programming languages mentioned earlier, or through Appium inspector.
Appium Desired Capabilities: what are they?
Before you start testing, you need to understand what Appium's Desired Capabilities are. This is essentially a JSON document that the Appium client sends to the server whenever a new automation session is requested. The document contains crucial information about how your session should run. Think of the device you want to use, which app should be launched, whether the app should be removed first and which browser you want to test. Appium supports almost all emulators, physical devices and browsers.
Configuring for Android and iOS
For an Android emulator (such as a simulated Samsung Galaxy A40), you specify the platform details, the device name and the UDID. Android also requires a path to your APK file via the "app" property, or two specific properties that define the app package and the app's launch activity (as defined in your AndroidManifest.xml).
On iOS, physical devices first have to be provisioned. In the JSON configuration you use specific properties for this, such as xcodeOrgId and xcodeSigningId. Instead of the bundleId, you can also use the "app" property here to point directly to an IPA file. A typical configuration looks like this:
JSON
{
"appium:platformName": "Android",
"appium:platformVersion": "11",
"appium:deviceName": "Samsung Galaxy A40",
"appium:udid": "******",
"appium:appPackage": "com.coffeeit.appium_example",
"appium:appActivity": "com.coffeeit.appium_example.MainActivity",
"appium:noReset": "false",
"appium:fullReset": "false"
}
What do "noReset" and "fullReset" do?
New Appium users are often confused by the keys "noReset" and "fullReset". These properties tell Appium to clear the app data or remove the app completely before a test starts. This is very useful, for example, when you want to test an app's onboarding flow repeatedly from a clean slate.
- If
noResetis set totrue, all app data is cleared. - If
fullResetis set totrue, the app is removed and reinstalled (this is only possible if a path to an APK/IPA has been provided).
Personally, I always clear the app data, because you can't blindly rely on the state of an app. Clearing the app data gives a test a better chance of passing.
Accessibility IDs
When writing tests, we need to interact with UI elements. There are several ways to do this. UI elements can be identified by:
- ID
- Accessibility ID
- XPath: The nested path of an element
- Class name: The class name of the element
- Tag name
- And various other Android/iOS-specific methods
The best way is to use accessibility IDs. Simply because accessibility IDs require no code changes and can be kept in sync across several apps. So if you have an Android and an iOS app, you can (depending on how much they differ) use the same test for both apps. That means you don't have to create a separate test for Android and another for iOS. You can also use if statements in your code to perform UI actions that only apply to iOS or only to Android.
Writing tests with Kotlin
Now that we understand what Appium's Desired Capabilities are and have a basic understanding of how to identify UI elements, we can start writing tests. In this blog post I use Kotlin.
Step 1: Import dependencies
First, we need to import a few required dependencies into our project:
Kotlin
dependencies {
implementation("io.appium:java-client:8.0.0")
implementation("org.seleniumhq.selenium:selenium-java:4.1.4")
implementation("org.testng:testng:7.5")
testImplementation("org.testng:testng:7.5")
}
Step 2: Create the test class
For this example, I am testing a simple to-do app I made. Create a new class in src/test/kotlin called TodoTasksTest that looks like this:
Kotlin
import io.appium.java_client.AppiumBy
import io.appium.java_client.android.AndroidDriver
import org.openqa.selenium.WebDriver
import org.openqa.selenium.remote.DesiredCapabilities
import org.openqa.selenium.support.ui.ExpectedConditions
import org.openqa.selenium.support.ui.WebDriverWait
import org.testng.annotations.AfterTest
import org.testng.annotations.BeforeTest
import org.testng.annotations.Test
import java.net.URL
import java.time.Duration
class TodoTasksTest {
private lateinit var driver: WebDriver
private lateinit var waiter: WebDriverWait
@BeforeTest
fun setup() {
// Setup the desired capabilities
val capabilities = DesiredCapabilities()
capabilities.setCapability("platformName", "Android")
capabilities.setCapability("deviceName", "Samsung Galaxy A40")
capabilities.setCapability("platformVersion", "11")
capabilities.setCapability("udid", "******")
capabilities.setCapability("appPackage", "com.coffeeit.appium_example")
capabilities.setCapability("appActivity", "com.coffeeit.appium_example.MainActivity")
capabilities.setCapability("noReset", "false")
capabilities.setCapability("fullReset", "false")
driver = AndroidDriver(URL("http://127.0.0.1:4723/wd/hub"), capabilities)
waiter = WebDriverWait(driver, Duration.ofSeconds(10L))
}
@Test
fun testAddTask() {
// wait for the element to be visible and click on it
waiter.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("AddTask"))).click()
// if accessibility ids are not defined, we can identify elements in different ways
val elements = driver.findElements(AppiumBy.className("android.widget.EditText"))
elements[0].sendKeys("Some title")
elements[1].sendKeys("Some description")
// wait for the element to be visible and click on it
waiter.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("SaveTask"))).click()
}
@Test
fun testAddTaskBackButton() {
// wait for the element to be visible and click on it
waiter.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("AddTask"))).click()
// wait for the element to be visible and click on it
waiter.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("Back"))).click()
}
@AfterTest
fun teardown() {
driver.quit()
}
}
In the example above, we start by setting up our capabilities. Then two tests are defined: one adds a new task with a title and description, while the other presses the back button when trying to add a task. In the last function we end the automation session.
The longest step here is setting up the capabilities and preparing the test device. The tests themselves only take a few seconds.The next time we want to test something, we simply start the Appium server, connect our device and run our tests. But what if you are not a developer? Can you still use Appium to write tests?
Introduction to TestProject
Appium is a free, open-source framework designed to automate testing as much as possible, and it does that very well. However, you still need to be a developer to write the tests.
Fortunately, commercial test automation frameworks offer online environments where you can test apps without writing code. You can prepare test data, record tests and integrate them into cloud solutions such as GitLab CI or AWS.
In 2015, an open-source initiative started to make these premium features available to the world for free. In April 2018, the first version of this online test environment was released under the name TestProject.TestProject is a free, end-to-end test automation platform for web, mobile and API testing that makes smart use of Appium. It is a very powerful platform that can be used completely free of charge.
Key considerations
Although TestProject claims to be open source, in reality only its SDK and plug-ins are. The online environment itself is closed source. This means you cannot run a local instance of the online dashboard, and an active internet connection is required to use it.
How do you use TestProject?
Using TestProject is very simple:
- Register an account and log in.
- Follow the installation wizard and confirm your email address.
- Set up the TestProject Agent. This agent runs an Appium server in the background and communicates directly with your test devices and emulators.
- Once configured, you land in the test environment, where a default test project is created automatically to get you started.
Navigating the TestProject dashboard
At the top left of the dashboard you see your current project and can switch to other projects. The tabs below manage your project-specific data:
Tests & Jobs
TestProject divides the work into tests and jobs. Tests simply follow a specific flow of steps, while jobs consist of one or more tests linked to a specific device. Jobs can be triggered manually or scheduled.
Elements
In the Elements tab you can define UI elements in advance. This makes writing several tests much easier, because you can simply refer to them later without writing code again.
Applications & Data Sources
- Applications: Define the specific apps you want to test.
- Data Sources: Store external files (such as a CSV file) to pave the way for data-driven tests and jobs.
Parameters & Devices
- Parameters: Define key/value parameters at project level (such as login details) to reuse them across tests.
- Devices: Authorise your test hardware. When the agent recognises a new device, a pop-up appears asking for authorisation.
Advanced test & step configuration
When you open a test, all related information is shown. You can record steps natively (this requires a local TestProject agent to be running) or create steps manually.
Global versus step-specific settings
The cog icon at the top left opens the global test settings. Here you can configure:
- How fast the test should run.
- What happens if a step fails.
- Whether screenshots should be taken at every step.
Although these rules apply globally, you can also override them at step level in TestProject by expanding the advanced options of a specific individual step.
Inside the Steps tab
Each step in a test has a specific action to perform. TestProject offers a wide range of built-in options and supports add-ons.Add-ons are very handy when you need to interact with native UI elements outside your app, and anyone in the community can create and share them.
Platform reports and integrations
Clear reports
TestProject can automatically generate visually appealing, detailed test reports. They explain the test run from start to finish, highlighting the exact test data used and the actions performed.Summaries and full reports are easy to share with stakeholders. If your test is configured to take screenshots at every step, those images are linked directly in the full report.
Third-party integrations
TestProject integrates natively with platforms such as Sauce Labs and BrowserStack, and supports custom webhooks for success or failure notifications.Although TestProject agents can run inside Docker containers, keep in mind that Docker-based agents currently do not support local hardware device detection.
API & GitLab CI pipelines
TestProject has an extensive API that makes it easy to trigger tests and jobs remotely. For example, you can write your own custom integration pipeline in GitLab CI for testing Android. This sequence runs a series of jobs and only closes the pipeline once all tasks have completed successfully:
YAML
# Minimal OS image with Java and Android 31 build tools pre-installed
image: mreichelt/android:31
variables:
# Colors for terminal output
Color_Off: '\e[0m'
Red: '\e[0;31m'
Green: '\e[0;32m'
BRed: '\e[1;31m'
BGreen: '\e[1;32m'
BBlue: '\e[1;34m'
# TestProject variables
TP_API_URL: "https://api.testproject.io/v2"
TEST_RETRIES: 2
TP_API_KEY: "SOME_API_KEY"
TP_PROJECT_ID: "some_project_id"
TP_APP_ID: "some_app_id"
TP_APK_FILE_NAME: "app-debug.apk"
TP_JOBS: "job_id_1 job_id_2"
before_script:
- apt-get --quiet update --yes
- apt-get --quiet install jq --yes
stages:
- build
- upload
- test
# Build project
assembleDebug:
interruptible: true
stage: build
script:
- ./gradlew -Dorg.gradle.jvmargs=-Xmx2048m assembleDebug
artifacts:
untracked: true
paths:
- app/build/outputs/
# Upload the APK to TestProject
uploadAPK:
interruptible: true
stage: upload
script:
# Get AWS url from TestProject to upload to
- 'UPLOAD_DATA=$(curl -s -S -X GET "${TP_API_URL}/projects/${TP_PROJECT_ID}/applications/${TP_APP_ID}/file/upload-link" -H "accept: application/json" -H "Authorization: ${TP_API_KEY}")'
- echo $UPLOAD_DATA
# Parse the upload url
- UPLOAD_URL=$(echo $UPLOAD_DATA | jq -r ".url")
- UPLOAD_METHOD=$(echo $UPLOAD_DATA | jq -r ".method.Method")
- echo $UPLOAD_URL
- echo $UPLOAD_METHOD
# Upload the APK to the upload url
- curl "${UPLOAD_URL}" --upload-file app/build/outputs/apk/debug/app-debug.apk
# Publish the APK to the TestProject app
- 'UPLOAD_CONFIRM_DATA=$(curl -X POST "${TP_API_URL}/projects/${TP_PROJECT_ID}/applications/${TP_APP_ID}/file" -H "accept: application/json" -H "Authorization: ${TP_API_KEY}" -H "Content-Type: application/json" -d "{ \"fileName\": \"${TP_APK_FILE_NAME}\" }")'
.avif)



