The world of mobile applications is growing by leaps and bounds, and so is the need to ensure seamless user experience across the different platforms and devices available in the market. Appium, an open-source test automation framework, has revolutionized the way developers and testers approach mobile application testing, enabling them to meet this critical need. It comes with features like cross-platform compatibility and intuitive design, which makes Appium the go-to solution for streamlining the testing process. Not only does it save time and resources but also helps in delivering high-quality mobile applications.
This comprehensive guide will provide you with a solid foundation in Appium, equipping you with the knowledge and skills to navigate the rapidly evolving mobile test automation landscape.
What is Appium?
Appium is an open-source test automation framework designed specifically for mobile applications. It empowers developers and testers to write and execute automated tests for iOS as well as Android applications. It leverages the WebDriver protocol, popularly used for web application testing, and extends its capabilities to be used for mobile application testing. Appium provides a set of APIs and a server that bridges the gap between the test scripts and the mobile devices or the emulators/simulators.
Appium provides cross-platform compatibility, which allows writing automation tests in various programming languages like Java, Python, Ruby, C#, etc. The tests can be run across multiple mobile platforms and hence save the time and effort required to maintain separate frameworks for each platform.
Appium has been in the market since 2012 and has gained widespread acceptance ever since. It is maintained by an open-source community and supported by companies like Sauce Labs, Open JS Foundation, etc.
History of Appium
Appium was first introduced by Dan Cuellar in 2011 by the name “iOSAuto”. It was that time when he recognized the need for a comprehensive mobile testing solution that could help scale execution as well as narrow down the gap between the different platforms and programming languages. He wrote a code in C# that used Selenium-style syntax to write sequential javascript commands. Before Appium’s introduction, each platform had separate tools and frameworks. Appium came in as a solution to simplify and streamline the testing process by providing a single, cohesive framework that could work across different platforms and devices.
Why is Appium Preferred Over Other Mobile Testing Tools?
Test automation with Appium comes loaded with advantages like
- Cross-Platform Compatibility: It is one of the primary advantages of Appium and its ability to support both Android and iOS platforms. Through it, developers and testers can maintain a single codebase for testing mobile apps across different operating systems.
- Multiple Language Support: Appium supports various programming languages like Java, Python, Ruby, C#, and more, providing flexibility to choose the preferred language to write test scripts.
- Easy to use: Appium is easy to learn and use, which means that developers and testers with no prior experience with mobile automated processes can adapt quickly.
- Open-source: Appium is free to use, and has an active community that keeps contributing to its development and maintenance. It is also vendor-neutral, which implies that it can be used to test applications developed by any company or organization.
- Integration with other testing tools: Appium can be easily integrated with tools like Selenium Grid for web testing, Jenkins for continuous integration, and TestNG or Cucumber for behavior-driven development.
- Community Support: Appium has extensive documentation and a vibrant community of developers and testers who actively contribute to its growth and maintenance.
Appium Architecture
Appium follows a client-server architecture, which separates the test execution from the application under test. The test script acts as the client that sends commands to the Appium server, and the server acts as an intermediary that translates those commands into platform-specific actions. These actions are executed on the target device or emulator/simulator.
Components of Appium
- Appium Server: The Appium server is the central component that acts as a bridge between the test scripts and the mobile device or emulator/simulator. It receives the commands from the test scripts, interprets them, and then communicates with the appropriate device driver to execute the actions on the mobile app.
- Test Script: The test script is the code written in the preferred programming language by the developer or the tester. It consists of the test cases and scenarios that are to be executed on the mobile app. These test scripts send commands to the Appium server using the webDriver protocol.
- Device Drivers: Appium uses platform-specific device drivers that help interaction with the mobile device or emulators/simulators. For Android, it uses Android Debug Bridge(ADB), while for iOS, it utilizes the XCUITest or UIAutomation frameworks.
Communication Flow
The communication flow in the Appium architecture follows the below steps:
- The test script sends the command to the Appium server using the WebDriver protocol.
- The Appium server translates the command into platform-specific actions.
- The Appium server then dispatches the translated actions to the appropriate device driver( ADB for Android or XCUITest/UIAutomation for iOS).
- The device driver interacts with the mobile device or emulator/simulator to perform the actions on the application.
- The result of the actions is sent back from the device driver to the Appium server.
- The Appium server translates this result into a WebDriver-compatible response and sends it back to the test script.
Prerequisites for Appium
To start using Appium testing, your system needs to be equipped with certain prerequisites. In this section, we will see how you can set your system up for mobile test automation with Appium.
System Requirements
- Operating System: Appium tests can run on Windows, macOS, or Linux operating systems. However, you will need a macOS machine if you wish to test iOS applications since Xcode is only available on macOS.
- Java Development Kit(JDK): Since we will be using Java for our article, you need to install Java in your system. The latest version can be downloaded from the official website.
- Node.js: Appium automation framework is built on top of Node.js, so you’ll need to have Node.js installed in your system. You can download it from the official Node.js website.
- Android SDK(for Android testing): If you need to test Android applications, you will require the Android SDK, which can be downloaded with Android Studio. It can be downloaded from the Android Studio website or directly from the Android SDK command-line tools.
- Xcode(for iOS testing): For iOS application testing, XCode should be installed on your macOS machine. It is an integrated development environment provided by Apple for iOS development.
Setting up the Environment
- Android Environment: Once you have installed the Android SDK, you will have to set up the Android environment by configuring the Android Virtual Device (AVD) Manager. It will help you create and manage Android emulators for testing purposes. Also, you may need to enable USB debugging on your physical Android devices for testing.
- iOS Environment: For testing iOS applications, you will require a valid Apple Developer account to access the necessary tools and resources. You will have to configure code signing and provisioning profiles so that you can deploy and test your iOS applications on real devices and simulators.
Setting up the System for Appium
To start using Appium automation you will have to configure some environment variables on your system-
- JAVA_HOME: Set the JAVA_HOME environment variable to point to the location of your JDK installation.
- PATH: Set the bin directories of the JDK and Android SDK (for Android app automation) to your system’s PATH variable. This enables you to run Java and Android tools from the command line.
Installation of Required Dependencies
- Install the Appium command-line tool globally using the npm (Node Package Manager) using the below command:
npm install -g appium
2.Appium Doctor: Appium Doctor is a diagnostic tool that checks if your Appium setup is done properly. If there is any missing component it provides feedback on it or any other issues. You can download Appium Doctor directly through the command line using the below command-
npm install -g appium-doctor
To check the Appium setup using Appium doctor, use the below command-
appium-doctor
Once done, if everything is installed properly, you will see results like below. If not, you can see the errors to be corrected. You can correct the errors(if any) referring to the suggested fixes.
3.Install any additional dependencies required by your chosen programming language for writing test scripts. For example, to write tests using Java, you need to install the “java-client” client library for Appium.
Launching the Appium Server
You can launch the Appium server through the command line interface. Use the below command in your system terminal or command prompt-
appium
Mobile Automation using Appium on Android and iOS Devices
To start using Appium mobile testing, you need to set up either Real Devices or emulators or simulators. Let us see how we can set them up for the Appium testing tool.
Setting up Real Devices
- Android Devices: To set up a real Android device for Appium test automation, you first need to enable USB debugging on the device. Once this is done, you can connect your device to the system and see it listed in the available devices list. You may need to install the appropriate device drivers on your computer to ensure proper communication between Appium and the physical device.
- iOS Devices: To test real iOS device applications, you need to have a valid Apple developer account. You will also need to configure code signing and provisioning profiles to deploy and test your iOS applications on real devices.
- Using the TestGrid Real Device Cloud: TestGrid offers a wide range of real devices on its cloud. You can select from several real devices with different configurations through it. It can be integrated into your code just like other real devices and the code can be executed on these cloud devices just like the real devices.
Using Emulators and Simulators
- Android Emulators: Appium allows testing on Android emulators, which can be created and managed using the Android Virtual Device (AVD) Manager included with the Android Studio/SDK. Emulators are a convenient way to test your apps on different Android versions and configurations without using physical devices.
- iOS Simulators: Xcode comes with a built-in iOS Simulator that can be used for iOS testing. This simulator allows you to test your app on multiple iOS versions and devices without the need for a physical device.
Though emulators and simulators can help you in the initial phases of testing, it is always recommended to perform testing on real devices to eliminate any platform-specific issues or behavior that may not be precisely represented in an emulated environment.
Writing your First Appium Test
After you have set up Appium and the required prerequisites, let’s dive into writing our first Appium test automation script. Let us go through the steps of beginning our journey to write Appium tests.
Choosing a Programming Language
Appium supports multiple programming languages like Java, Python, Ruby, C#, etc, and one can choose from their preferred language to start automating tests. For this article, we will be using Java as the programming language.
Setting up the Test Automation Framework
Depending on the programming language you chose, you can set up a test automation framework. For example, you can use TestNG or JUnit with Java, or Pytest if using Python. We will be using TestNG in our example.
Creating our First Appium Test Script
We will create a new Maven Project in Eclipse( you can also choose IntelliJ Idea as the IDE) and add the following dependencies in the pom.xml file.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>firstAppiumProject</groupId>
<artifactId>firstAppiumProject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>firstAppiumProject</name>
<dependencies>
<!-- https://mvnrepository.com/artifact/io.appium/java-client -->
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>9.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.7.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Once the required dependencies for the project are configured, we will now write our first Appium test. Create a Java class and paste the below code into it.
Note that we will be using the TestGrid Real Device Cloud and use real devices to run our test. If you wish to use a physical device or emulator/simulator, you may change the desired capabilities in the same code.
package firstAppiumProject.appiumTest;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import io.appium.java_client.AppiumBy;
import io.appium.java_client.android.AndroidDriver;
public class FirstAppiumTest {
AndroidDriver driver;
@BeforeMethod
public void setUp() throws MalformedURLException {
//Setting desired capabilities for the android device with details like device name, version, etc
//Since we are using the TestGrid Real device cloud we have copied the capabilities from the device
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "Galaxy A23 5G");
capabilities.setCapability("platformVersion", "14");
capabilities.setCapability("platformName", "android");
capabilities.setCapability("automationName", "UiAutomator2");
capabilities.setCapability("udid", "R5CT80S9RPW");
capabilities.setCapability("tg:userToken", "Your TestGrid user token");
capabilities.setCapability("systemPort", "4006");
capabilities.setCapability("uiautomator2ServerLaunchTimeout", "90000");
//setting capability for application we want to test
capabilities.setCapability("appPackage", "com.google.android.calculator");
capabilities.setCapability("appActivity", "com.android.calculator2.Calculator");
//Instantiating Android Driver and using Appium server host and port
driver = new AndroidDriver(new URL("URL to connect to device through appium server"), capabilities);
}
@Test
public void multiply() {
//Locating numbers, mathematical operator and equals button on the calculator app
driver.findElement(AppiumBy.id("com.google.android.calculator:id/digit_4")).click();
driver.findElement(AppiumBy.id("com.google.android.calculator:id/op_mul")).click();
driver.findElement(AppiumBy.id("com.google.android.calculator:id/digit_9")).click();
driver.findElement(AppiumBy.id("com.google.android.calculator:id/eq")).click();
//Storing the result in a string variable
String result = driver.findElement(AppiumBy.id("com.google.android.calculator:id/result_final")).getText();
System.out.println("The result is - "+result);
//Asserting the result as expected
Assert.assertEquals(result, "36");
}
@AfterMethod
public void tearDown() {
//Quitting the driver object
driver.quit();
}
}
In the above test, we are trying to perform a calculation on a pre-installed Calculator app and verify the result. You may choose to select any other app in case this app is not available on your device. Since we are using the TestGrid Real Device Cloud, the Desired Capabilities are generated using its properties. If you use a physical device, an emulator, or a simulator, you can use the values corresponding to your device in the capabilities like device name, platform version, device udid, automation name, etc. You need not specify the port or the user token. Also note that the URL would be the Appium Server URL, i.e., the URL of the local host of the system on which the Appium server is started. The URL and the port for the same can be seen in the Appium Desktop or Appium server logs.
Let us now try to understand the different methods used in our Appium automation test.
Locating the Elements
Appium allows you to locate the different mobile elements by employing different locator strategies. You can select from the various available strategies like the ID, XPath, accessibility ID, and more. The below line of code locates the number 4 using the ID locator-
AndroidElement btn = driver.findElement(AppiumBy.id("com.google.android.calculator:id/digit_4"));
Or
driver.findElement(AppiumBy.id("com.google.android.calculator:id/digit_4"));
Executing the Appium Test Script
Once the code is written, you can execute/run it as a TestNG Test. It will launch the mobile app on the connected device or emulator and perform the actions specified in the test script. Once the execution is complete you can see the execution logs in the console as shown below-
If you have executed your test script on TestGrid Real Device Cloud, you can leverage the elaborate reports generated post-execution.
Best Practices for Appium Testing
Appium is a powerful solution for mobile test automation, if you follow best practices it ensures reliability, maintainability, and efficiency. Some of the Appium best practices are listed below-
- Write Maintainable and Readable Tests: One should adopt a consistent and clear naming convention for the tests. This enhances the code readability as well as makes it easier to understand for maintenance. General principles of modularity promote code reusability and easier maintenance. Ensure proper commenting on code to make it easier for others to understand the code and modify it when required.
- Handling Flaky Tests: Flaky tests exhibit intermittent and inconsistent behavior. To mitigate the flakiness of tests, try to implement explicit wait mechanisms using tools like WebDriverWait or custom wait functions. This reduces the chances of timing-related issues.
- Parallel Test Execution: Parallel execution of tests can significantly reduce the execution time. To make the most of it, ensure that the test cases are independent and do not interfere with each other to avoid race conditions when running in parallel.
- Reporting and Logging: Implement comprehensive reporting and logging mechanisms in your test framework, which will provide valuable insights into the execution. Issue diagnostics will be easier with relevant logs, screenshots, and other artifacts.
- Continuous Integration and Deployment: Integrate your Appium tests in CI/CD pipelines to get early feedback on potential issues as soon as there is a code change. Leverage tools like Jenkins, Travis CI, or GitHub to achieve the same.
Troubleshooting Errors and Common Problems
Though Appium is a robust mobile test automation tool, Appium testing may encounter various issues. Some common problems and troubleshooting techniques are listed below.
- SessionNotCreatedException – When the Appium server fails to create a new session with the mobile device or emulator/simulator, the session not created exception occurs. Potential causes could be incorrect desired capabilities, missing dependencies, or connectivity issues.
- NoSuchElementException – This error occurs when Appium is unable to locate an element on the application screen. The reason behind it might be incorrect locator strategy, timing issues, or application changes.
- StaleElementReferenceException – This error occurs when you are trying to interact with an element that is no longer attached to the application’s DOM (Document Object Model). It can be due to changes in the application, refresh, or timing issues.
Debugging Techniques
- Make use of the Appium Desktop app or command-line interface to view logs and diagnose issues. Appium provides logging information that can help identify the root cause of the issues.
- You can enable verbose logging in your test framework to gather more detailed information about the test execution.
- Utilize the Appium Inspector or any other inspection tool to verify element locators and application state. You can easily identify issues and understand the application’s structure using it.
The best practices combined with effective troubleshooting techniques can help you enhance the overall quality, reliability, and efficiency of your Appium tests. This will ensure a smooth mobile testing experience.
Key Takeaways
- Appium is a powerful open-source mobile test automation tool that enables developers and testers to perform cross-platform testing using the programming language of their choice.
- Appium can be easily downloaded and configured in your systems and the same can be verified using the Appium doctor.
- Appium offers multiple locator strategies and methods to perform actions.
- Appium tests can be executed on physical devices, real device cloud as well as emulators/simulators.
- Adhering to best practices can help you maintain reliable and efficient test scripts.
- Common errors encountered during the execution of Appium tests can be easily mitigated using the correct techniques and by enabling the logging.
- Appium tests can be integrated with CI/CD pipelines to improve the turnaround time for finding issues.
FAQs
1. What is Appium?
Appium is an open-source mobile automation framework that enables users to automate mobile web, hybrid, and native applications on a variety of platforms, including iOS and Android.
2. Which programming languages does Appium support?
Appium supports a wide range of programming languages, including Java, Python, Ruby, JavaScript, and C#.
3. What is thе diffеrеncе bеtwееn Appium and Sеlеnium?
Appium is a mobilе automation tеsting framework that is usеd for tеsting nativе, hybrid, and mobilе wеb applications, whеrеas Sеlеnium is a wеb automation tеsting framework that is usеd for tеsting wеb applications.
4. What is thе Appium Inspеctor?
Thе Appium Inspеctor is a tool that allows dеvеlopеrs and tеstеrs to inspеct thе usеr intеrfacе еlеmеnts of an application and gеnеratе tеst scripts for automation.
5. Can Appium bе intеgratеd with othеr tеsting framеworks?
Yеs, Appium can bе intеgratеd with othеr tеsting framеworks such as TеstNG, JUnit, etc.
6. What is thе diffеrеncе bеtwееn Appium and UI Automator?
UI Automator is an Android automation tеsting framework that is usеd for tеsting nativе Android applications, whеrеas Appium is a cross-platform automation tеsting framework that supports tеsting for both Android and iOS applications.