A Step-by-Step Guide to Test Automation with Appium

Software testing is critical in ensuring any application’s success in the digital world. With an ever-growing number of devices and operating systems, guaranteeing flawless functionality across multiple platforms can be daunting. This is where Appium comes to the rescue.

Appium is an exceptional open-source project and ecosystem designed to streamline UI automation across various app platforms. Whether you’re developing apps for mobile operating systems like iOS, Android, or Tizen or targeting web browsers such as Chrome, Firefox, and Safari, Appium has got you covered. Its capabilities don’t stop there – it extends to desktop environments like macOS and Windows and TV platforms including Roku, tvOS, Android TV, and Samsung.

In this detailed Appium tutorial for Android and iOS, we delve into the critical significance of software testing in today’s digital era. We explore the latest Appium 2.0 version within this context, spotlighting its cutting-edge features and enhancements that further augment UI automation across multiple platforms. Focusing on mobile OS, web browsers, desktop, and TV platforms, we uncover Appium’s robust architecture, installation process, and how it empowers developers to ensure impeccable application functionality.

The Evolutionary Progress of Appium

Appium has emerged as a powerful automation framework that simplifies UI testing across various app platforms. Originally conceived in 2011 by Dan Cuellar as “iOS Auto,” an iOS-specific testing framework, its scope expanded to accommodate the rise of Android. Inspired by Selenium WebDriver, Cuellar introduced “Appium” in 2012, extending WebDriver principles to mobile app testing with support for popular programming languages.

Initially focused on iOS automation using WebDriver protocol and Apple’s UI Automation library, Appium quickly adapted to support Android. A significant funding boost in 2013 allowed the project to be entirely rewritten using Node.js, spearheaded by Appium pioneer Jonathan Lipps. The result was an updated version with functionalities that needed to be added.

Appium 2.0’s vision aimed to create an infinitely extensible ecosystem for automation, facilitating adaptation to the dynamic app industry. Its 2023 release empowered developers to create Appium drivers for new platforms, encouraging collaboration through third-party plugins. The Appium CLI tool simplifies the installation of drivers and plugins from the Appium ecosystem. Today, Appium remains a thriving open-source project, continuously evolving to meet the diverse needs of software testing and establishing itself as a reliable solution for UI automation.

Why is Appium Mobile Testing Popular?

With its features and benefits, Appium has earned immense popularity among developers and testers worldwide, establishing itself as the go-to solution for efficient and reliable app testing.

  • Cross-Platform Compatibility: Appium mobile testing comes with exceptional cross-platform compatibility allows testing on various platforms, such as iOS, Android, Tizen, web browsers (Chrome, Firefox, Safari), desktop (macOS, Windows), TV (Roku, tvOS, Android TV, Samsung), and more. Teams can write test scripts once and execute them seamlessly across multiple platforms, saving time and effort.
  • Popular Programming Languages Support: The Appium mobile testing framework supports widely-used programming languages like Java, Ruby, Python, and more. Testers and developers can leverage their existing coding skills and frameworks, making the transition to Appium smooth and straightforward.
  • Free and Open Source: Being an open-source framework, Appium enables users to access and modify the source code as needed, promoting innovation and ensuring transparency. The fact that Appium is free of cost makes it accessible to organizations of all sizes and budgets.
  • Compatibility with Testing Frameworks: Appium seamlessly integrates with popular testing frameworks like TestNG, JUnit, Pytest, Cucumber, and more, providing testers with a familiar testing environment.
  • Robust and Extensible Ecosystem: Appium’s architecture allows easy extensibility and customization. With Appium 2.0, anyone can create and share Appium drivers for new platforms, continuously expanding the framework’s capabilities. Introducing plugins empowers developers to extend and modify Appium’s functionalities, promoting innovation and adaptability.
  • Native and Web Application Support: Appium excels in automating both native and web applications, providing a comprehensive set of features to handle diverse testing scenarios. This versatility makes Appium a valuable tool for testing different types of applications.

What Does the Appium Architecture Entail?

Appium’s architecture follows a client/server model, with the server built on the Node.js platform, featuring a REST API, and implementing Selenium WebDriver. The Appium framework bridges a test script and a mobile application running on a physical device or an emulator/simulator. The Appium Server accepts the commands from the test script via the REST API and uses WebDriver to convert them into the appropriate automation actions for the mobile platform.

The architecture comprises three main components: Appium Client, Appium Server, and the End Device.

  • Appium Client: The Appium client comprises language-specific libraries or SDKs that empower developers to create test automation scripts for mobile applications using Appium. In multiple programming languages, these client libraries offer methods for locating elements, interacting with UI components, executing gestures, and validating expected behaviors. Testers can also define desired capabilities through the client libraries to configure the testing environment.
  • Appium Server: The Appium server serves as a crucial intermediary within the framework, enabling the automation of mobile applications. It bridges the gap between the test script and the mobile application, whether running on a physical device or an emulator/simulator. Via a REST API, the server receives commands from the test script and translates them into automation actions specific to the targeted mobile platform. Leveraging the powerful features of Selenium WebDriver, the Appium server communicates with the mobile application, enabling operations like element identification, UI interaction, gesture simulation, and behavior validation. The Appium server provides a unified interface, enabling cross-platform mobile testing, as test scripts can be developed in various programming languages and executed seamlessly across diverse mobile platforms.
  • End Device: In the context of Appium, the “end device” refers to an emulator, simulator, or real device connected to the server where automated tests are executed. These devices play a pivotal role in carrying out the testing procedures and validating the functionality and performance of mobile applications.

These key components form the foundation of Appium’s architecture, facilitating efficient and reliable mobile application testing across various platforms and devices.

Appium’s Functionality on Android and iOS Platforms

Appium’s functionality on Android and iOS platforms is achieved by utilizing the Mobile JSON Wire/W3C Protocol. This protocol facilitates the transformation of test commands into REST API requests by the Appium client libraries. The Appium server then transmits these requests to the connected Android device or emulator.

Android

In Appium Android, the crucial bootstrap.jar files come into play. These files enable the device to execute the commands using automation frameworks like Selendroid or UI Automator. Subsequently, the test results are returned to the Appium server, relaying an HTTP response containing relevant status codes to the Appium client. With the right Appium Android tutorial, you can explore how this mechanism allows testers to analyze the outcomes of their Android automation tests effectively.

iOS

Similarly, Appium leverages the JSON wire protocol for iOS devices, interacting with them through Apple’s UIAutomation API. Within iOS devices, the WebDriverAgent.app files are instrumental in interpreting the received test commands. Utilizing XCUITest, the WebDriverAgent.app effectively executes requests on iOS devices, enabling seamless automation and testing of iOS applications.

What are the Prerequisites for Appium Automation?

Setting the foundation for the Appium automation framework requires essential prerequisites to ensure a seamless mobile app automation journey:

1. Programming Language Proficiency: A fundamental understanding of at least one programming language (Python, Java, JavaScript, etc.) is crucial to effectively working with Appium.

2. Access to Mobile Devices: Appium necessitates access to physical mobile devices or emulators/simulators to perform automation testing.

3. Appium Server Installation: To interact with mobile devices and execute automation scripts, installing the Appium server on your system or accessing remote Appium servers like HeadSpin is essential.

4. Application for Testing: To begin automation testing with Appium, you will need the mobile application you intend to test.

5. Platform-Specific SDK: Depending on the platform you wish to automate (Android or iOS), installing the respective Software Development Kit (SDK) on your machine is necessary.

6. Appium Client Library: Depending on your chosen programming language, you must include the Appium client library in your project to facilitate automation.

7. Appium Inspector: For element identification, location on the screen, property viewing, and generating automation code snippets for your mobile application, you will need Appium Inspector.

8. Device or Emulator Configuration: Configuring additional settings on the device or emulator may be required. This could involve enabling developer mode, USB debugging, or granting necessary app permissions.

9. Optional Development Environment: For optimal utilization of Appium, consider setting up a development environment tailored to your chosen programming language.

By addressing these prerequisites, you lay a solid groundwork for seamless and successful mobile app automation with this Appium testing tutorial.

How to Execute Your First Appium Automation Test with HeadSpin

HeadSpin offers the option of running Appium tests on real devices available in the HeadSpin cloud if you can access the devices. HeadSpin’s UI-integrated Appium Inspector lets you inspect the application’s view hierarchy, determine its selectors, and even generate scaffold Appium code.

1. Appium Inspector

Appium Inspector is a popular feature of Appium, used to identify UI elements in mobile apps for automation script development. With HeadSpin’s integrated Appium Inspector, you can develop scripts directly from the device’s Remote-control UI. The Inspector attaches automatically when launching an Appium session, providing a browser-based script development experience. 

Refer to the HeadSpin documentation for further information – How To Use the HeadSpin integrated Appium Inspector to view element hierarchy

2. Appium Server URL

The Appium Server URL is the address or location where the Appium Server is running. It usually follows the format of http://<host>:<port>/wd/hub, where <host> represents the hostname/IP address of the machine running the Appium Server, and <port> represents the port number on which the server is listening.

● How to get the Appium Server URL from HeadSpin

a) Access the HeadSpin UI’s Device List

In your web browser, log in to the HeadSpin user interface. Access the list of remote devices in your account.

b) Click the ‘More Actions’ button to access the automation configuration

c) Select automation configuration to view basic automation configuration with web_driver_url (Appium server URL) under Device Configuration.

You can also find the device_id(udid) from the above config.

3. Test Script

The Appium tool has clients in Python, Java, Ruby, java scripts, etc. We can use any Appium-supported programming language to write the test script. 

Here we are using Python code as an example. Before we move forward, please ensure that you have python3 installed on your system.

● Install Appium Client

The Appium Python Client is the official Python client for Appium, available on PyPI as “Appium-Python-Client.” It includes the Selenium Python Binding as well. 

a) Run the below command to install the Python Appium client.

pip install Appium-Python-Client

This example uses Python’s built-in unittest module, though you can use any Python test framework you want. Save the below code as test.py.

import unittest

# Import Appium Python Client

from appium import webdriver

from appium.webdriver.common.appiumby import AppiumBy

class TestAppium(unittest.TestCase):

def setUp(self):

# URL required to Connect with the Appium Server(remote/local)

appium_server_url = “http://<host>:<port>/wd/hub” 

device_id = “XXXXXXXXX”

# Desired capabilities to communicate with appium server

capabilities= {

“udid”: device_id,

“automationName”: “uiautomator2”,

“appPackage”: “com.android.settings”,

“appActivity”: “com.android.settings.Settings”,

“platformVersion”: “14.4”,

“platformName”: “Android”

}

# Webdriver initialization

self.driver = webdriver.Remote(command_executor=appium_server_url, desired_capabilities=capabilities)

def tearDown(self):

if self.driver:

# Terminate the session

self.driver.quit()

def test_find_battery(self):

# locating element

el = self.driver.find_element(by=AppiumBy.XPATH,value=’//*[@text=”Battery”]’)

# Automation command(click on the element)

el.click()

if __name__ == ‘__main__’:

unittest.main()

b) The test script will involve initializing the Appium web driver with specified capabilities, launching the settings app, locating and interacting with UI elements, and quitting the driver.

Note #1

the 

Great! Let’s give it a shot. Before running the test, ensure that you have an active Appium server running in a separate terminal session.

However, if you are using a device from HeadSpin’s cloud, you don’t have to worry about the server setup. The Appium server will already be running over the host where the device is connected.

c) Run the script test.py using python3

python3 test.py

Before running the test, ensure you have an active Appium server running. If it’s a device from the HeadSpin cloud, the Appium server will already be running on the host where the device is connected.

Following these steps and running the Python test script, you can perform your first automation test with HeadSpin on real devices available in the HeadSpin cloud.

Troubleshooting Common Errors in Appium Automation: Steps to Resolve Issues

Troubleshooting common errors in the Appium automation tool is crucial for ensuring smooth test execution. Here are some typical errors and their troubleshooting guidance:

Element Identification Problems

  1. Verify your automation script’s element locator strategy (XPath, ID, class name, etc.).
  2. Utilize Appium Inspector or similar tools to inspect the element hierarchy and validate its attributes.
  3. Ensure the element is within the visible viewport or perform scrolling if needed.
  4. Address dynamic loading or timing issues by implementing appropriate waits or synchronization techniques.

Timing and Synchronization Issues

  1. Implement explicit waits to ensure elements are present, visible, or interactable before performing actions.
  2. Adjust implicit wait timeouts to allow sufficient time for elements to load.
  3. Use synchronization techniques, such as Thread.sleep() or time.sleep(), based on the programming language.

Appium Server and Client Compatibility

  1. Verify compatibility between the Appium server and client libraries (e.g., Appium Java client, Appium Python client).
  2. Upgrade or downgrade the client libraries to match the server version if needed.

By following these troubleshooting steps, you can efficiently resolve common errors and ensure a successful Appium automation process.

A Comparison of Two Automation Frameworks — Appium and Selenium

Selenium and Appium are prominent automation frameworks serving distinct testing needs in the software testing realm. Let’s compare these two frameworks to understand their key differences, strengths, and ideal use cases, helping you make an informed decision based on your testing requirements.

Target Platforms

  • Appium: The Appium mobile testing framework supports Android and iOS platforms. Moreover, it has expanded its capabilities to encompass other mobile operating systems and popular Smart TV platforms, allowing users to create custom driver modules for dedicated devices. This framework facilitates seamless interactions with apps and websites, enabling testing on real devices or simulators/emulators.
  • Selenium: Focused primarily on web application testing, Selenium empowers automation across various web browsers like Chrome, Firefox, Safari, and Internet Explorer.

Test Application Types

  • Appium: Ideal for testing mobile applications, Appium provides native support for automating interactions with native, hybrid, and mobile web applications.
  • Selenium: Well-suited for testing web applications across different browsers, Selenium enables automation of web elements and user actions like clicking buttons, filling forms, and navigating between pages.

Programming Languages

  • Appium: Supporting multiple programming languages such as Java, C#, Python, Ruby, JavaScript, and more, Appium offers flexibility in choosing the language that aligns with your existing testing infrastructure or your team’s expertise.
  • Selenium: Like Appium, Selenium provides support for various programming languages, catering to diverse development environments and preferences.

Automation Approach

  • Appium: Simulating user interactions specific to mobile devices, Appium facilitates actions like tapping buttons, swiping, multi-finger gestures, etc. It employs the WebDriver protocol to interact with mobile apps, ensuring a seamless testing experience across different mobile platforms.
  • Selenium: Utilizing the WebDriver API, Selenium automates web browsers, allowing user actions such as clicking buttons, entering text, submitting forms, and navigating web pages. It presents a comprehensive set of functions to interact with web elements and perform various testing tasks.

Native and Hybrid Applications

  • Appium: Offering built-in support for automating native, hybrid, and mobile web applications, Appium directly interacts with the UI elements of mobile apps. This capability ensures accurate and reliable testing for mobile-centric projects.
  • Selenium: While Selenium doesn’t inherently provide native support for automating native mobile applications, integrating Selenium WebDriver with Appium enables the automation of web views within native or hybrid applications, ensuring a comprehensive testing approach for hybrid projects.

By understanding these Selenium Appium distinctions, you can make a well-informed decision on Appium Selenium to align with your specific testing needs, whether mobile or web application testing. 

The Process of Setting Up a Local Appium Server on Your System

If you don’t have a cloud service like HeadSpin, you may need to set up a local Appium server. Setting up Appium Server on Windows, Linux, or Mac involves several steps to ensure a proper installation. Here is a general guide for each operating system: 

1. Install Node.js

● On Windows

To get the latest version of Node.js, download as well as install it from the official website (https://nodejs.org). 

● On Linux

Open the terminal and run the following commands to install Node.js using the package manager:

sudo apt update

sudo apt install nodejs npm

● On Mac OS

a) Open the terminal and run the following command for Homebrew installation:

/bin/bash -c “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)”

b) To install Node.js using Homebrew, run the following command in your terminal:

brew install node

2. Install Appium

Open the Terminal/command prompt and run the following command to install Appium globally:

npm install -g appium

# if you are facing issues, use sudo with the command on linux/mac.

sudo npm install -g appium

3. Install an Appium Driver and Its Dependencies

To perform any meaningful tasks with Appium, having at least one Driver installed is essential. Without a Driver, Appium lacks the capability to automate any actions. Fortunately, a vast Ecosystem of drivers and plugins can be leveraged to enhance the functionality of Appium.

● Installation using Appium’s extension CLI interface

By utilizing Appium’s Extension CLI, you can delegate the management of drivers and plugins to Appium. You can instruct Appium to install, update, or remove specific extensions through CLI commands. Here are some examples illustrating how you can install different drivers using the CLI:

appium driver install uiautomator2

appium driver install xcuitest

appium driver install chromium

Extensions will manage these installed packages. $APPIUM_HOME/node_modules/.cache/appium/extensions.yaml.

4. Install Appium Doctor

Appium Doctor is a helpful tool to check if all the dependencies required by Appium are installed correctly. Run the following command to install it:

npm install -g appium-doctor

# if you are facing issues, use sudo with the command on linux/mac.

sudo npm install -g appium-doctor

5. Install JDK

Download as well as install the Java Development Kit (JDK) from the Oracle website (https://www.oracle.com/java/technologies/javase-jdk14-downloads.html).

6.Install Android SDK

Download as well as install Android Studio from the official website (https://developer.android.com/studio). Launch Android Studio and go through the setup wizard to install the necessary components, including the Android SDK.

7. Set Java and SDK Environment Variables

  • Set the JAVA_HOME environment variable to the JDK installation path. Additionally, add the JDK’s “bin” directory to the system’s PATH variable.
  • Set the ANDROID_HOME environment variable to the SDK installation path. Additionally, add the SDK’s “tools” and “platform-tools” directory to the system’s PATH variable.

8. Verify Installation

Run `appium-doctor` in the Terminal/command prompt to check if all the dependencies and configurations are correctly set up. Address any issues reported by Appium Doctor.

appium-doctor

# Start appium by running the following command:

appium

Appium 2.0: An Advancement Towards the Future of Automation

Appium 2.0 significantly advances cross-platform mobile testing, ushering in a transformative era. With a strong focus on collaboration, adaptability, and user experience, Appium 2.0 introduces groundbreaking features to enhance mobile app testing. Let’s explore its major goals and advancements, shaping a more seamless and efficient testing experience.

‍1. Major Goals/Features

● Empowering Developers with the Appium Driver Ecosystem

Appium 2.0 places great importance on community-driven development. Its driver ecosystem empowers developers and testers to create and share Appium Drivers for diverse platforms. This collaborative approach not only fosters a sense of community but also ensures broad platform compatibility and extensive test coverage.

● Seamless Integration with the Plugin Ecosystem

Appium 2.0 embraces adaptability and extensibility through its robust plugin ecosystem. By developing Appium Plugins, users can effortlessly integrate Appium with other tools and systems, tailoring testing approaches to meet specific requirements effectively.

● Enhanced Documentation for Improved User Experience

Appium 2.0 acknowledges the value of comprehensive and up-to-date documentation. Significant enhancements have been made in this area, implementing a new documentation framework that ensures easy navigation and access to relevant information. With streamlined and reliable documentation, users can leverage Appium 2.0 with confidence and clarity.

● Streamlining for Efficiency and Relevance

Appium 2.0 prioritizes agility and relevance in mobile testing. The framework has undergone meticulous pruning to optimize performance, removing outdated and incompatible features. The result is a leaner and more efficient Appium experience, allowing users to focus on essential testing aspects and minimizing unnecessary complexities.

● Introducing the Independent Appium Inspector with Enhanced Features

Appium 2.0 introduces an independent Appium Inspector with exciting new features. Testers can now simulate complex user interactions involving multiple fingers, enhancing their ability to effectively identify and troubleshoot UI elements.

2. New Platform Drivers to Explore

Here are some of the new drivers that are ready to use with Appium 2.0. 

● Roku

This is an Appium 2.x driver produced by HeadSpin for the automation of Roku channels (in the Roku world, “channel” means “app”).

How to install:

appium driver install –source=npm @headspinio/appium-roku-driver

● Tizen TV

HeadSpin produced the Appium Tizen TV Driver, a test automation tool for Samsung Tizen TV devices. It works with Tizen apps developed using the web-style framework (not the “native” C++-based apps). This driver is designed to be used with Appium; it doesn’t do anything on its own.

How to install:

appium driver install –source=npm appium-tizen-tv-driver

● LG WebOS TV

This is an Appium 2.x driver produced by Headspin to automate LG WebOS TV apps.

How to install:

appium driver install –source=npm appium-lg-webos-driver

● Chromium

This is an Appium driver for Chromium-based browsers (like Chrome). It is already possible to automate Chromium browsers with Chromedriver. This Appium driver uses Chromedriver under the hood.

No need to download specific versions of Chromedriver manually. This driver takes care of downloading a version of Chromedriver appropriate for the version of the browser under test. But using this driver has some advantages, including: 

  1. Automate Chromium browsers using the Appium server you already have simply by including this Appium driver.
  2. No need to download specific versions of Chromedriver manually. This driver takes care of downloading a version of Chromedriver appropriate for the version of the browser under test.
  3. Take advantage of the ecosystem of Appium plugins and features (e.g., image analysis) not available via Chromedriver.

How to install:

appium driver install chromium

3. New Appium Plugins to Explore

Here are some new Appium plugins ready to use with Appium 2.0.

● AltUnity

This is an Appium 2.x plugin for AltUnity Tester produced by HeadSpin. This makes AltUnity Tester completely compatible with Appium so that an Appium user doesn’t need to learn the AltUnity API or download any extra clients or tools. 

How to install:

appium plugin install –source=npm appium-altunity-plugin

● Images

This is an official Appium plugin designed to facilitate image comparison, visual testing, and image-based functional testing.

How to install:

appium plugin install –source=npm images

● Execute Driver

This is an Appium 2.x plugin for the advanced Execute Driver Script feature, which allows you to send in a WebdriverIO script to have it executed completely on the server instead of command-by-command from the client.

How to install:

appium plugin install –source=npm execute-driver

How Do HeadSpin’s Advanced Appium Capabilities Empower Organizations to Conduct Seamless Test Automation?

HeadSpin, a leading mobile app testing platform, offers comprehensive support for Appium and its latest version, Appium 2.0. Leveraging HeadSpin’s capabilities with Appium provides testers and developers with a robust and seamless testing experience across various platforms. Here are some key highlights of HeadSpin’s Appium capabilities:

  1. Cloud-Based Testing: HeadSpin’s extensive global device infrastructure provides access to a vast collection of real devices, enabling cloud-based testing with Appium. This allows testers to execute automation scripts on a diverse set of devices without the need for physical hardware, reducing infrastructure costs and testing time.
  2. Integrated Appium Inspector: HeadSpin’s user interface is integrated with Appium Inspector, simplifying element identification and script development for mobile apps. Testers can efficiently inspect the view hierarchy, determine selectors, and generate scaffold Appium code directly from the HeadSpin cloud.
  3. Scalability and Performance Testing: With HeadSpin’s powerful infrastructure, testers can test scalability and performance using Appium. By simulating thousands of concurrent users across various network conditions and geographies, they can assess app performances under real-world scenarios.
  4. End-to-End Testing: HeadSpin supports end-to-end testing scenarios with Appium, allowing testers to integrate mobile app testing with web application testing seamlessly. This unified approach ensures comprehensive coverage and consistency in test results.
  5. Extensive Device and OS Coverage: HeadSpin’s device inventory spans various manufacturers, models, and operating systems, giving testers access to many devices for the Appium testing tool. This ensures compatibility testing across various devices, enhancing app reliability and user experience.

The Next Steps

Appium, with its cross-platform automation capabilities, has emerged as a vital tool for testers and developers worldwide. Its latest version, Appium 2.0, offers transformative advancements that streamline testing processes and foster community-driven development.

When combined with HeadSpin’s unparalleled capabilities, Appium becomes an even more powerful asset for mobile app testing. With access to an extensive device inventory, cloud-based testing, and integrated Appium Inspector, HeadSpin empowers testers to thoroughly learn Appium for conducting efficient and scalable automation testing.

Embrace the benefits of using Appium with HeadSpin to achieve optimal testing results and deliver high-quality, reliable apps to your users.

Article Resource:

This article was originally published on:

https://www.headspin.io/blog/appium-automation-testing-a-step-by-step-guide