Appium is a powerful open-source tool for automating mobile application testing across various platforms like Android and iOS. It allows developers to run tests on native and hybrid mobile applications using the same codebase, making it a highly versatile framework.
This article will guide you through running Appium tests on real Android devices, including setup, writing test scripts, and understanding the importance of real device testing for accurate results.
What is Appium?
Appium is an open-source framework that allows us to conduct automation testing of mobile applications on different platforms like Android, iOS, and Windows.
It automates the testing for:
- Native Mobile Applications that are written using iOS, Android, or Windows SDKs
- Mobile Web Applications that can be accessed using mobile phone browsers such as Safari, Chrome, or the in-built native browser applications for android devices
- Hybrid Mobile Applications that have a native wrapper around the web view
Appium is a cross-platform testing framework that is flexible, enabling you to write the testing code against multiple platforms such as iOS, Windows, and Android using the same API. This means you can use the same code for iOS that you have written for Android, saving lots of time and effort.
Benefits of Appium
Below are the reasons why Appium is important:
- Cross-Platform Testing: Appium allows testing on Android, iOS, and Windows using a single API, saving time and effort by reusing code across platforms.
- Language Flexibility: It supports multiple languages, such as Java, Python, Ruby, JavaScript, and C#, enabling developers to work in their preferred language.
- No App Modification Needed: Appium allows testing of native, hybrid, and mobile web apps without altering the app code, making it convenient for production testing.
- Real and Virtual Device Support: It supports real devices and emulators, providing the flexibility to test apps under different conditions.
- Open-Source with Active Community: Free and open-source, Appium is continuously updated by a large community, ensuring it remains reliable and cutting-edge.
- CI/CD Integration: Easily integrates with CI/CD tools like Jenkins, enabling automated mobile testing as part of the development pipeline.
- Feature-Rich: Supports gestures, element interactions, and multiple orientations, covering various mobile testing needs.
How to Run Appium Tests on Android Devices
Running Appium tests on Android devices requires a combination of tools, configuration steps, and an understanding of how the Appium server communicates with Android devices using Java-based test scripts.
Before writing test scripts, you should understand what tools are involved and what each one does:
- Appium Server: The Appium server handles communication between your test scripts and the Android device. It exposes an HTTP API based on the WebDriver protocol that your client code interacts with.
- Java: Appium is built using Node.js, but it supports multiple programming languages. Java is widely used for writing test scripts and integrates well with testing frameworks like TestNG.
- TestNG: A testing framework used with Java for grouping test methods, managing execution, and generating reports.
- Appium Java Client Library: This library lets your Java test scripts talk to the Appium server. It provides wrappers over WebDriver interfaces to interact with Android elements.
- Android SDK & ADB Tools: The Android SDK includes tools such as ADB (Android Debug Bridge), which are essential for device communication, installing apps, capturing logs, and more.
- Environment Variables: Make sure the JAVA_HOME and ANDROID_HOME environment variables are configured. Also ensure the platform-tools directory is added to your system’s PATH so you can run ADB from any terminal.
Below is a step-by-step guide to running Appium tests on Android devices.
Step 1: Install the Required Tools
Before you can run Appium tests, make sure the following tools are installed on your system:
- Appium Server: Install Appium globally using npm:
npm install -g appium
- Appium Doctor (optional, but helpful): Validates your system setup:
npm install -g appium-doctor appium-doctor
- Java Development Kit (JDK): Required for both Appium and Android tools. Download from Oracle or OpenJDK sources.
- Android SDK: Install it using Android Studio or the command-line tools. Make sure to include platform-tools (for ADB) and emulator images if needed.
- TestNG: Add TestNG to your project using Maven or download the JAR file directly.
- Appium Java Client: Add it via Maven. For example:
<dependency> <groupId>io.appium</groupId> <artifactId>java-client</artifactId> <version>7.5.1</version> </dependency>
Step 2: Enable Developer Mode on Your Android Device
To run tests on a physical Android device, you need to enable USB debugging:
- Open Settings on the Android device.
- Tap About phone.
- Tap Build number 7 times to unlock Developer Options.
- Go back to Settings > Developer options, and enable USB debugging.
- Connect the device using a USB cable. Run adb devices in your terminal to make sure it’s detected.
Step 3: Define Desired Capabilities
Desired capabilities are key-value pairs that define how Appium should start and configure the session. These settings tell Appium what kind of device to use, which app to launch, and which automation engine to use.
Some important capabilities for Android:
- deviceName: Name of the connected device or emulator (example: “Pixel_5”).
- platformName: Always set this to “Android” for Android testing.
- appPackage: The application’s package name (e.g., “com.android.chrome”).
- appActivity: The activity that should be launched (e.g., “com.google.android.apps.chrome.Main”).
- automationName: For Android, use “UiAutomator2” as the preferred automation engine.
- noReset: Optional, but prevents resetting app state between test runs.
- udid: If multiple devices are connected, provide the device’s unique ID.
- chromedriverExecutable: Required if testing Chrome and Appium’s default ChromeDriver is incompatible with the version on the device.
In the example below, the configuration sets up a real Android device (or emulator), opens the Chrome browser, and uses the UiAutomator2 automation engine. It also includes the noReset flag to prevent clearing app data between sessions.
DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability("deviceName", "Pixel_5"); capabilities.setCapability("platformName", "Android"); capabilities.setCapability("appPackage", "com.android.chrome"); capabilities.setCapability("appActivity", "com.google.android.apps.chrome.Main"); capabilities.setCapability("automationName", "UiAutomator2"); capabilities.setCapability("noReset", true);
If you’re testing on a physical device and multiple devices are connected, you should also include the udid capability to target a specific device.
If you’re testing web content in Chrome and the default ChromeDriver is not compatible with the device’s Chrome version, you’ll also need to set the chromedriverExecutable capability to point to a compatible ChromeDriver binary.
Step 4: Write and Execute Test Scripts Using the Appium Java Client API
After setting up the tools and configuring the capabilities, you can write test scripts to automate your Android app. Your script should:
- Establish a connection to the Appium server
- Launch the app on the device
- Interact with UI elements
- Validate outcomes
Here’s a basic example using TestNG and Appium Java Client:
import io.appium.java_client.MobileElement; import io.appium.java_client.android.AndroidDriver; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.By; import org.openqa.selenium.support.ui.WebDriverWait; import org.openqa.selenium.support.ui.ExpectedConditions; import org.testng.Assert; import org.testng.annotations.BeforeTest; import org.testng.annotations.AfterTest; import org.testng.annotations.Test; import java.net.MalformedURLException; import java.net.URL; public class AppiumExample { AndroidDriver<MobileElement> driver; WebDriverWait wait; @BeforeTest public void setup() throws MalformedURLException { DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability("deviceName", "Pixel_5"); capabilities.setCapability("platformName", "Android"); capabilities.setCapability("appPackage", "com.android.chrome"); capabilities.setCapability("appActivity", "com.google.android.apps.chrome.Main"); capabilities.setCapability("automationName", "UiAutomator2"); capabilities.setCapability("noReset", true); driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities); wait = new WebDriverWait(driver, 10); } @Test public void testChromeNavigation() { driver.get("https://www.browserstack.com/"); String pageTitle = driver.getTitle(); Assert.assertEquals(pageTitle, "Most Reliable App & Cross Browser Testing Platform | Browserstack"); MobileElement startTrialButton = driver.findElement(By.xpath("//a[text()='Start Free Trial']")); startTrialButton.click(); wait.until(ExpectedConditions.presenceOfElementLocated(By.id("signupModal"))); } @AfterTest public void tearDown() { if (driver != null) { driver.quit(); } } }
Why use Real Android Devices for Appium Tests?
Although emulators are useful in the early stages of development, real Android devices provide the most accurate representation of how your app will perform in real-world conditions. Emulators often fail to replicate critical factors such as network throttling, battery performance, or actual hardware configurations, which can impact the end-user experience.
Testing on real Android devices offers several advantages:
- Accurate user experience simulation: Ensures the app functions as expected under real conditions.
- Variety of devices: Test on various devices, including multiple Android OS versions, screen sizes, and hardware configurations.
- Better performance: Real devices offer faster and more reliable testing than emulators, leading to more precise results.
Why Use BrowserStack App Automate for Appium Testing
BrowserStack’s Real Device Cloud allows you to run automated mobile app tests on real Android devices, ensuring that your tests reflect true user experiences.
With seamless integration of Appium, you can automate your tests on various Android devices without needing an in-house device lab.
Benefits of using BrowserStack for Appium Testing:
- Real Devices: Test on thousands of real, physical Android devices, ensuring accurate, real-world results.
- Cross-Device Compatibility: Easily test on multiple devices, OS versions, and screen sizes to ensure consistent app performance.
- Geolocation Testing: Simulate various geographic locations for location-based app functionality testing.
- Network Simulation: Test your app under different network conditions, such as 2G, 3G, or 4G.
- Push Notifications: Test how your app handles push notifications in real user conditions.
- Advanced Debugging Tools: Access a range of logs and video recordings for effective debugging.
- Instant Access: Sign up for free and get instant access to the devices you need for testing, with no setup required.
Detailed Troubleshooting for Appium Tests on Android Devices
When running Appium tests on Android devices, various challenges can arise due to environment misconfigurations, dependencies, device settings, and even network issues. Here are some of the most common issues you might face, along with detailed solutions.
Device Not Detected by Appium
This is one of the most frequent issues you will encounter. Sometimes, Appium cannot detect an Android device, even though it is connected and running correctly.
Possible Causes and Solutions:
1. USB Debugging Not Enabled
Make sure that USB debugging is enabled on your Android device. Without it, Appium will not be able to communicate with the device. If it’s not enabled, you will see an empty list or “no devices detected.”
Solution: Go to Settings > About Phone, tap Build number seven times, then go to Developer options and enable USB debugging.
2. Incorrect ADB Setup
Sometimes, ADB (Android Debug Bridge) may not be properly set up or configured. If your device is not recognized, it could be because ADB is not correctly installed or running.
Solution: Run the following command in your terminal to verify that ADB can detect your device:
adb devices
This command should list the Android device you are connected to. If your device doesn’t appear, try restarting the adb server:
adb kill-server adb start-server
Also, make sure you have the correct USB drivers installed for your device.
3. Device Not Authorized
If you are using a physical Android device, it may require authorization for USB debugging. Once you connect the device via USB, you should see a prompt on the device to allow USB debugging with your computer. If you don’t see this prompt, ensure you allow USB debugging authorization.
4. Platform Tools Mismatch
You could encounter device detection issues if your Appium version is incompatible with the Android platform tools or ADB version.
Solution: Ensure that both your Appium and Android SDK versions are up to date. You can update Appium via npm and upgrade Android SDK tools via Android Studio or the command line.
Appium Server Not Starting
Sometimes, the Appium server fails to start. The error message might indicate that it’s unable to bind to the specified port (the default is 4723) or that the Appium node is not available.
Possible Causes and Solutions:
1. Port Already in Use
The most common reason Appium doesn’t start is that the default port (4723) is already being used by another process.
Solution: You can specify a different port when starting Appium by using the -p flag:
appium -p 4725
If you don’t specify a port, Appium will default to port 4723.
2. Conflicting Processes
If there are existing instances of Appium or other automation tools (like Selenium, another Appium server instance, etc.) running on the same system, they may interfere with starting a new Appium session.
Solution: Kill all processes that could be conflicting. On Linux or macOS:
killall node
Use the Task Manager to end any Node.js processes or Appium instances on Windows.
3. Node.js Installation Issues
Appium relies on Node.js, and if it is not installed correctly or is outdated, it can cause problems starting Appium.
Solution: Check your Node.js installation:
node -v
Ensure you are running a supported version of Node.js (Appium recommends LTS versions). If needed, reinstall Node.js.
Session Timeout or Connection Refused
If your Appium tests frequently timeout or experience connection issues, it could be because the server connection between your test script and the Appium server is unstable.
Possible Causes and Solutions:
1. Appium Server Down
Ensure that the Appium server is running when executing your tests. If the server is not started, the WebDriver will not be able to communicate with the device.
Solution: Run the Appium server manually in your terminal before running your tests, or ensure that the server starts automatically when needed.
2. Incorrect Appium Server URL
Double-check your test script’s URL to connect to the Appium server. The default URL is typically http://127.0.0.1:4723/wd/hub. If your Appium server is hosted on a different port or address, make sure to update the URL in your script.
Solution: Correct the URL in your test script, like so:
driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
3. Firewall or Network Restrictions
Sometimes, a firewall or other network-related restrictions may block the communication between the test script and the Appium server.
Solution: Ensure that there are no firewall rules preventing Appium from running on the specified port.
Appium Session Crashes or Unexpected Behavior
Appium sessions can sometimes crash or fail during the execution of tests, especially if there is a mismatch in the desired capabilities or an issue with the Appium server configuration.
Possible Causes and Solutions:
1. Incorrect Desired Capabilities
Incorrect or missing desired capabilities can cause the Appium server to fail to create a session or crash the session. Ensure that all the required capabilities are set correctly.
Solution: Review your capabilities thoroughly. For instance, the deviceName, platformName, appPackage, and appActivity must match the device and app you are testing. If you are unsure, you can use Appium’s Inspector or Android’s adb shell commands to get the correct values.
2. Appium Version Compatibility
Appium updates frequently, and newer versions may introduce breaking changes that can affect compatibility with the Android platform. Always check that you’re using a compatible version of Appium with your Android SDK and device version.
Solution: Regularly check the Appium changelog for breaking changes and update your version of Appium as necessary.
3. Outdated Android Drivers
Ensure that you are using the latest Android driver for Appium, such as UiAutomator2, which supports modern Android devices. Using outdated drivers like Selendroid may cause unexpected crashes and errors.
Solution: Always set the automationName to “UiAutomator2” for modern Android testing.
Conclusion
You can also leverage a plethora of debugging options for Appium testing on BrowserStack such as Text logs, Network Logs, Device Logs, Appium Logs, Visual logs, and Video Recordings of tests. Explore Debugging Options on BrowserStack App Automate
Since users demand high-functioning and engaging apps, Appium testing is an absolute requirement before releasing any apps. By running Appium tests on real Android devices, testers can ensure that apps are working as expected in real user conditions. Run as many tests as possible on as many real Android devices as possible to offer a consistently optimal user experience.
Useful Resources for Appium
Tutorials
- How to perform Parallel Test Execution in Appium?
- Appium Visual Testing: The Essential Guide
- How to run Appium iOS Tests on Real Devices?
- How to perform Debugging in Appium
- How to Run Your First Appium Test Script
- How to report bugs in Appium UI Testing?
- How to run Appium Tests on macOS?
- XPath in Appium: Tutorial
- How to Analyze Appium Logs
- How to perform Drag and Drop using Appium
- How to test mobile app in Landscape or Portrait mode using Appium
- How to Run Same Script in Multiple Devices using Appium
- How to change Time Zones for Mobile App Testing using Appium
- How to Perform Localization of Mobile Apps using Appium
- What is Appium Inspector? (Benefits & How to Use it?)
- How to run Appium tests on Android devices
- How to scroll down to an element in Appium
- How to Download and Install Appium
- How to set up your Appium Grid
- How to test Biometric authentication using Appium?
- How to use touch actions in Appium?
- How to Automate a Real E2E User Flow involving App and Browser with Appium
- How to Inspect Element using UIAutomatorViewer in Appium
- How to Test Flutter Apps Using Appium Automation
- Understanding Appium Desktop: Tutorial
- Appium Tutorial for Mobile Application Testing
- React Native and Appium Tutorial
- Understanding FindElements in Appium
Best Practices, Tips, and Tricks
- Appium Best Practices Every Developer Must Know
- Effective Locator Strategies in Appium
- Top Appium Commands every Developer must know
- Desired Capabilities in Appium
- Overcoming Top Challenges faced in Appium Automation
Getting Started with
- Getting Started with Appium and NUnit framework
- WebdriverIO Tutorial: Getting started with Test Automation using Selenium and Appium
- Appium with Python: Getting Started with App Automation Testing
- Appium with Java: Getting Started to Run Automated Tests
- Test Windows Desktop App using Appium-Compatible WinAppDriver
Differences and Comparisons