In the previous sections, we picked locators from an Mobile App using Android Inspector.
Now we will write our first Appium test script to connect to an Android device and run a simple test.
Steps to Write Your First Script
1. Connect your Android device, make sure USB debugging is enabled.
2. Get the device ID using command: adb devices
3. Get the desired capabilities (refer Android Real Device Setup tutorial if required)
4. Start Appium Server
5. Launch Android Inspector to pick locators for the test.
6. Open IntelliJ IDEA (or any IDE you like), This tutorial uses Java + TestNG + Maven.
7. Create New Maven Project
8. Add dependencies in pom.xml
<dependencies>
<!-- Appium Java client -->
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>9.1.0</version>
</dependency>
<!-- Selenium (required by Appium Java client) -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.23.0</version>
</dependency>
<!-- TestNG for running tests -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.10.2</version>
<scope>test</scope>
</dependency>
</dependencies>
9. Create a new follow class and write this example code
Example Script – Android Driver Setup & Test
import io.appium.java_client.AppiumBy;
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import java.net.MalformedURLException;
import java.net.URL;
public class DriverSetup {
// Declare AndroidDriver
public static AndroidDriver driver;
@BeforeTest
public void setUp() throws MalformedURLException {
// Create DesiredCapabilities object
DesiredCapabilities caps = new DesiredCapabilities();
// Platform name - Android
caps.setCapability("platformName", "Android");
// Android OS version (Example: 14)
caps.setCapability("platformVersion", "14");
// Device name (check via 'adb devices')
caps.setCapability("deviceName", "emulator-5554");
// Replace with your device ID for real devices
// App package name of the app under test
caps.setCapability("appPackage", "com.swaglabsmobileapp");
// App main activity to launch
caps.setCapability("appActivity", "com.swaglabsmobileapp.MainActivity");
// Initialize AndroidDriver with Appium server URL
driver = new AndroidDriver(
new URL("http://127.0.0.1:4723/wd/hub"), caps);
System.out.println("Android driver initialized successfully.");
}
@Test
public void testOrderProduct() {
// Enter username
driver.findElement(AppiumBy.accessibilityId("test-Username"))
.sendKeys("standard_user");
// Enter password
driver.findElement(AppiumBy.accessibilityId("test-Password"))
.sendKeys("secret_sauce");
// Click Login
driver.findElement(AppiumBy.accessibilityId("test-LOGIN"))
.click();
// Open menu
driver.findElement(AppiumBy.xpath("//android.view.ViewGroup[@content-desc='test-Menu']/android.view.ViewGroup/android.widget.ImageView"))
.click();
// Click All Items
driver.findElement(AppiumBy.accessibilityId("test-ALL ITEMS"))
.click();
// Add a product to cart (Example: Sauce Labs Backpack)
driver.findElement(AppiumBy.xpath("//android.widget.TextView[@text='Sauce Labs Backpack']/following-sibling::android.view.ViewGroup[@content-desc='test-ADD TO CART']"))
.click();
// Open Cart
driver.findElement(AppiumBy.accessibilityId("test-Cart"))
.click();
System.out.println("Product added to cart successfully!");
}
@AfterTest
public void tearDown() {
if (driver != null) {
driver.quit(); // Close the driver after test execution
System.out.println("Android driver quit successfully.");
}
}
}
Test Script Explanation
@BeforeTest → Sets the desired capabilities and starts the AndroidDriver session.
@Test → Contains the actual steps to interact with the app (login → navigate → add product to cart).
@AfterTest → Closes the driver and ends the session.
Android Driver Setup Explanation
This is the core part of any Appium project; it defines how to connect to the Android device and launch the app under test. It is similar to getting the capabilities that were added in the Android Inspector JSON representation part. These settings are specified in Desired Capabilities
|
Capability
|
Purpose
|
How to Get the Value
|
|
platformName
|
Tells Appium which platform to test on (Android or iOS).
|
Always set to "Android" for Android testing.
|
|
platformVersion
|
Specifies the Android OS version on the device.
|
On the device: Settings → About phone or run command:
adb shell getprop ro.build.version.release
|
|
deviceName
|
Identifies the device.
|
Run command:
adb devices
|
|
appPackage
|
The unique package name of the app.
|
Run command: adb shell dumpsys window
|
|
appActivity
|
The entry activity that launches the app.
|
Run command: adb shell dumpsys window
|
|
new URL("http://127.0.0.1:4723/wd/hub")
|
The address of the Appium server your test will connect to.
|
Default for local Appium server: 127.0.0.1 with port 4723.
|