Appium has become a widely used tool for automating mobile application testing. Its ability to test both Android and iOS apps using a single API makes it a preferred choice for developers and testers alike. Whether you are new to mobile test automation or just getting started with Appium, writing your first Appium test script can feel like a big step.
In this guide, you will learn how to write and run your first Appium test script for both Android and iOS platforms. We will also cover practical Appium script examples for Android and iOS, along with essential debugging tips to help you troubleshoot issues effectively.
What Is an Appium Test Script?
An Appium test script is a set of instructions written in a programming language such as Java, Python, or JavaScript. These scripts interact with a mobile app’s user interface to perform actions like clicking buttons, entering text, or verifying specific conditions.
The main purpose of these scripts is to automate the testing process, saving time while ensuring consistency and accuracy in test execution.
Prerequisites for Writing an Appium Test Script
Before you start writing your first Appium test script, make sure the following components are properly set up:
- Appium installed on your machine
- A mobile device or emulator configured for testing
- Java Development Kit (JDK), Android SDK, or Xcode (for iOS)
- A suitable IDE such as IntelliJ IDEA, Eclipse, or Visual Studio Code
Having these prerequisites in place will ensure a smooth setup and execution process.
Writing Your First Appium Test Script for Android
Let’s begin with a simple Appium script example for Android. The following script launches the default calculator app on an Android device.
Step 1: Set Desired Capabilities
Desired capabilities are key-value pairs that tell Appium about the environment it should interact with, such as the platform, device, and application details.
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.options.UiAutomator2Options;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.net.URL;
public class AppiumTest {
public static void main(String[] args) {
// Set Desired Capabilities
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "My Android Device");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("platformVersion", "11.0");
capabilities.setCapability("appPackage", "com.google.android.calculator");
capabilities.setCapability("appActivity", "com.android.calculator2.Calculator");
try {
// Launch the app on Android device
AndroidDriver driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
// Add your test steps here
} catch (Exception e) {
e.printStackTrace();
}
}
}In this example, the desired capabilities define the device name, platform version, and the calculator app’s package and activity details. These settings allow Appium to correctly identify and launch the app.
Step 2: Run the Script
To run the script, first ensure that the Appium server is active. Open your terminal and type:
appiumOnce the server is running, execute your script. The calculator app should launch on your connected Android device or emulator.
Writing Your First Appium Test Script for iOS
Now, let’s look at a similar Appium script example for iOS. The overall process is quite similar, with a few differences in configuration.
Step 1: Set Desired Capabilities for iOS
import io.appium.java_client.ios.IOSDriver;
import io.appium.java_client.ios.options.XCUITestOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.net.URL;
public class AppiumTest {
public static void main(String[] args) {
// Set Desired Capabilities for iOS
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "iPhone Simulator");
capabilities.setCapability("platformName", "iOS");
capabilities.setCapability("platformVersion", "14.0");
capabilities.setCapability("app", "<path_to_your_app>.ipa");
try {
// Launch the app on iOS simulator
IOSDriver driver = new IOSDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
// Add your test steps here
} catch (Exception e) {
e.printStackTrace();
}
}
}For iOS, you need to configure XCUITest options and specify details such as the device name, platform version, and the application path. This example uses an iOS simulator, but the same approach can be applied to real devices with slight adjustments.
Step 2: Run the Script
Just like Android, make sure the Appium server is running before executing the script. Once executed, the specified app will launch on the simulator or connected device.
Appium Test Script Basics
When writing Appium test scripts, keep the following core elements in mind:
- Desired Capabilities: These define the testing environment and are essential for successful execution
- Driver Initialization: The driver acts as a bridge between your script and the mobile device
- Test Actions: These include interactions such as tapping, typing, swiping, and validating UI elements
Understanding these fundamentals will help you write more effective and maintainable test scripts.
Appium Test Script Debugging
Debugging is often one of the most challenging aspects of working with Appium. When a test script fails, you may encounter errors such as ElementNotFoundException or SessionNotCreatedException.
Here are some practical tips to help you debug effectively:
- Check Appium Server Logs: Review logs for errors related to device connection or script execution
- Use Appium Inspector: This tool allows you to inspect UI elements and identify accurate locators
- Validate Element Locators: Ensure your locators are correct. For dynamic elements, consider using explicit waits
Taking a structured approach to debugging can save significant time and effort.
Conclusion
Writing your first Appium test script for Android or iOS is a valuable step toward mastering mobile test automation. It enables you to automate repetitive testing tasks, improve test coverage, and ensure your applications perform reliably across different devices.
By following these Appium script examples for Android and iOS, along with the fundamental concepts and debugging techniques discussed above, you will be well-equipped to create reliable, scalable, and maintainable automation tests.