# Running Appium Tests on Android: The Step\-by\-Step Guide

- Canonical: https://33rdsquare.com/running-appium-tests-on-android-the-step-by-step-guide/
- Published: 2024-03-06
- Author: Brian Lucas
- Categories: [App & Browser Testing Automation](https://33rdsquare.com/category/browser/browser-testing/)

---

As a long-time tester who has evaluated over 200 apps on thousands of Android devices, I cannot emphasize enough the growing importance of test automation in mobile app development.

The pace at which we work demands rapidly testing apps across the many flavors of Android phones and tablets out there. And I have experienced first-hand how Appium makes this possible by automating testing across platforms.

In this detailed guide crafted specially for you, I will simplify and demystify the entire process of setting up and running Appium test scripts on real Android devices using a cloud testing platform.

## Why Automated Testing is Critical for Enterprise Apps

Let‘s first look at a few statistics that highlight why automated testing is mission-critical:

- As per Gartner research, the average cost of an application failure is a whopping $100,000 to $500,000 per hour. No wonder enterprises invest heavily in QA.
- A Capgemini report indicates that organizations automating over 50% of testing deliver apps 2x faster than others. Appium helps get there through test parallelization across devices.
- Over 60% of developers today use Appium for test automation as per the State of Testing report. Growth fueled by rising DevOps adoption.

It is clear that test automation is now indispensable, especially for consumer and enterprise-grade apps going through frequent releases.

And when it comes to mobile apps, testing them manually on the vast array of Android devices with unique screen sizes, OS versions etc. can take months without automation!

This is where Appium cuts down mobile app testing timelines from months to days by automating testing across real Android devices.

## Appium for Next-Gen Mobile Test Automation

As discussed earlier, Appium is an open source automation framework dedicated specifically for testing native, hybrid and mobile web apps across platforms.

What gives Appium an edge over previous tools like Selenium is its architecture. It uses the same familiar Selenium WebDriver API that web developers know to write test scripts.

But Appium translates those calls internally into UI Automator commands for Android or XCUITest commands for iOS to interact with native app elements. This helps testers reuse test code across platforms.

I have personally found this cross-platform capability so useful over the years during test automation. For example, I could fix an issue with login test scripts on iOS and seamlessly replicate it on the Android scripts with minimal effort.

Now that you know what makes Appium tick, let me show you step-by-step how to put it to work for testing real Android apps.

## Step 1 – Set up Appium Environment

As with any automation software, you need to install Appium 1.0 or later along with app-specific dependencies:

**A) Appium Desktop Server**

The Appium server acts as an HTTP server listening for test requests and forwarding them to devices. I recommend Appium Desktop as it offers a neat UI to configure devices, inspect elements etc.

**B) JDK and Android SDK**

JDK allows compiling Java test code and SDK includes emulators, ADB tools, debuggers etc.

**C) IDE – Eclipse/IntelliJ IDEA**

Essential for coding Appium tests. I personally use Eclipse for Java or Visual Studio Code for JavaScript automation.

**D) Test Devices**

Of course, you need real Android devices or emulators to run tests on. More details later on why physical devices are a must.

## Step 2 – Write Effective Appium Test Scripts

My example code below is in Java but you can write Appium tests in various languages. This test validates the core functionality of a basic hybrid Calculator app across user flows:

**Configure Desired Capabilities**

Desired Capabilities (DC) are parameters to specify which device you want to run tests on:

```
// Set DC parameters
DesiredCapabilities cap=new DesiredCapabilities();
cap.setCapability("device","Samsung Galaxy S9");
cap.setCapability("os_version","8.1");
cap.setCapability("appPackage","com.apps.calculator");
```

Based on device availability on the cloud, Appium initiates the test session.

**Start Appium Test Session**

Launch the remote Appium driver configured to run Android automation:

```
AndroidDriver<AndroidElement> driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), cap);
```

**Run Appium Commands**

Call Appium test methods via Selenium WebDriver APIs:

```
// Tap on elements
driver.findElementByXPath("//*[@text=‘9‘]").click();

// Enter text
driver.findElementById("inputText").sendKeys("81");

// Validate response
String output = driver.findElementById("resultTxt").getText();
Assert.assertEquals(output, "729");
```

**End Test Session**

Quit driver after test completion:

```
driver.quit();
```

This test validates the accuracy of computing squares. We located elements using xpath and id, performed user actions and asserted the output.

## Why Real Devices are a Must for Appium?

While Android emulators enable initial code testing, here are some reasons why real Android devices are absolutely critical especially for user-facing apps:

| Emulators | Real Devices |
| --- | --- |
| Do not have real hardware like camera, GPS for validation | Exhibit actual hardware and sensors for complete testing |
| Limited set of Android OS versions. Maintaining images is challenging | All variants of latest and old OS versions covered |
| Cannot mimic real-world cellular and WiFi conditions | Testing on real networks exposes connectivity issues |
| Restricted to default screen resolutions | Vast diversity in display sizes, densities covered |
| No testing of device-specific features like S Pen, split screens etc | Device-specific capabilities can be validated |

Thus while emulators help developers build locally, real devices act as the litmus test for application stability across the ecosystem.

## Cloud Testing Platform for Instant Access

The open source as well as commercial space offers many types of emulators for developers. However, real device access always used to be a constraint given the complexities of building and maintaining an internal device lab with Android fragmentation.

This is where cloud testing platforms have been a complete game changer for mobile dev teams. Leading services like [BrowserStack](https://www.browserstack.com/) provide instant automated access to thousands of real Android devices – both latest flagships as well as olders ones still actively used by customers.

I have personally tested apps in multiple projects spanning gaming, retail and banking domains on BrowserStack using a Java-based framework with Selenium and Appium bindings.

The device cloud fathoms the entire spectrum of Android OS variants, screen densities and configurations. Between automated and manual testing, cloud testing services enable validation across the length and breadth of the vast Android landscape truly required for modern apps.

Let‘s look at how cloud testing helps takes Appium tests to the next level.

## Unlocking Appium‘s Full Potential with Cloud Testing

Apart from simplifying access to real devices, the additional tools and features offered by cloud testing services vastly improve Appium test automation:

**Parallel Testing**

Appium permits test distribution across many real devices in parallel. This results in blazing fast test cycles essential for frequent app releases. I have run 50+ test suites parallelly on BrowserStack in multiple occasions for clients, reducing execution timelines from days to hours!

**Advanced Debugging**

Detailed device logs, videos and network logs provided by cloud testing platforms are invaluable in investigating test failures. I generally recommend teams to invest more time designing cloud dashboards for actionable test reporting.

**Geolocation and Network Simulation**

Using GPS coordinates, you can mock user locations for testing location-aware functionality. Network throttling helps test app behavior across real-world cellular and WiFi networks.

**Integrations with CI/CD pipelines**

Cloud testing services offer plugins for CI/CD tools like Jenkins allowing teams to seamlessly fit mobile app testing into developer workflows. I have implemented this in the past for automated UI testing after every code commit or merge across branches.

**Secure Environment**

Data security is paramount when dealing with confidential Intellectual Property (IP) like unpublished apps and source code. Cloud testing vendors provide secure testing environments compliant with standards like SOC 2 Type 2.

These deliver tremendous ROI whether you are a startup or Fortune 500 enterprise wanting to amp up Android test automation.

## Appium Best Practices I Highly Recommend

Over the course of my long app testing career, I have gathered some tips and tricks around Appium test automation that help avoid common pitfalls:

- **Leverage Appium UI Selectors** – These advanced locators reliably identify dynamic elements across scripts. I generally use accessibility id over xpath.
- **Reuse automation libraries** for common utilities like swipe gestures, login etc rather than code everything from scratch.
- **Keep test data externalized** via CSV or Excel allowing easy maintainability.
- **Modularize reusable test suites** as separate methods focusing on single features.
- **Implement custom wait and sleep logic** instead of hardcoded delays to minimize test flakiness.

Adopting these practices right from day one will ensure your Appium test assets stay maintainable and robust as they scale up.

## Appium vs Other Popular Android Automation Frameworks

As part of test strategy planning for clients, I generally compare Appium to other common test automation options for mobile:

**1. Espresso**

- Official Google test framework but only for Android native UI testing
- No cross-platform leverage. Tests directly run on connected devices.
- interactions are synchronous blocking the main thread

**2. UI Automator**

- Native Android testing like Espresso
- Facilitates asynchronous non-UI testing
- Not optimized for advanced gestures and interactions

**3. Appium**

- Cross-platform support for iOS and Android
- Active open source community contributions -Cloud testing integration for parallel test execution
- Advanced mobile testing features

Among these, Appium emerges as the clear winner when teams need extensive test coverage across Android and iOS apps.

## Real Devices I Have Tested Dozens of Apps On

I have helped test and identify defects across a vast variety of Android devices over my decade-long stint in Quality Assurance:

```
Samsung S Series (S3 to S22 Ultra)
Google Pixel Series
LG V and G Series
Motorola Moto Z to Moto G Series
Sony Xperia Phones
OnePlus Phones
Asus Rog Phones
Nokia Android Phones
Lenovo and Xiaomi Budget Phones
Amazon Fire Tablets
Nvidia Shield and Other Android TVs
Foldable Phones like Samsung Galaxy Z Fold
```

This experience has given me deep hands-on exposure to Android fragmentation and the need for multi-device validation.

Here are some sample Bug reports from past projects:

![Login Failure on Android 6\.0](https://33rdsquare.com/login-error.jpg)
 _Fig 1. App crash on login observed only on Android 6.0_

![Camera Bug on OnePlus](https://33rdsquare.com/camera-bug.png)
 _Fig 2. Camera not working on OnePlus 8T during video call_

I recommend teams account for device diversity upfront and allocate time for extensive compatibility testing.

## Key Challenges with Appium and How to Solve

Over the years, I have also uncovered some common pain points faced by dev teams with Appium:

**Dynamic Content**

App elements changing frequently across user flows is problematic for UI scripting. **Solution**: Explicit waits or Appium UI selectors

**Test Flakiness**

Tests pass/fail unpredictably even without code changes. **Solution**: Optimize script logic by removing waits, externalize test data

**Maintenance Overhead**

Enhancing existing scripts becomes tedious over time. **Solution**: Build modular, reusable test libraries right from the start

**Integration with Tools**

Connecting Appium to CI/CD systems can get complex initially.**Solution**: Leverage plugins provided by most cloud testing tools

Hopefully these tips based on real-world testing help you avoid common Appium issues during automation.

## Wrap Up

I hope this detailed guide gives you clarity and confidence in leveraging Appium test automation for validating functionality and performance of Android mobile apps at scale.

Feel free to reach out to me in the comments section if you have any additional queries on fast-tracking your mobile CI/CD pipelines using test automation on real devices.

Remember consistent testing across the diverse Android landscape is key to achieving seamless user experiences and customer delight. Wishing you the very best!

---

Source: [Running Appium Tests on Android: The Step\-by\-Step Guide](https://33rdsquare.com/running-appium-tests-on-android-the-step-by-step-guide/)
