top of page
doanhoavn

Appium Mobile Testting

Updated: Apr 27, 2023


History

Appium was first released in 2012 by Dan Cuellar as an open-source tool for automating mobile application testing. It was initially developed as a way to automate iOS app testing using the WebDriver protocol. Later, support for Android and other mobile platforms was added, making Appium a cross-platform tool for mobile app automation. Since its inception, Appium has gained widespread popularity among mobile app developers and testers due to its ease of use, flexibility, and open-source nature.


What Is Appium

Appium is an open-source tool used for automating mobile application testing. It allows you to write automated tests using a variety of programming languages such as Java, Python, Ruby, and JavaScript, and run them on real devices or emulators/simulators.


Appium supports the automation of native, hybrid, and mobile web applications for iOS and

Android platforms. It uses the WebDriver protocol to interact with the mobile application and provides a set of APIs that you can use to automate user actions such as tapping, scrolling, swiping, and entering text.


Appium is designed to be cross-platform, which means that you can use the same test scripts to automate testing on both iOS and Android platforms. It also supports a wide range of testing frameworks, including JUnit, TestNG, and Cucumber, allowing you to integrate your tests with your existing development workflow.





Advantages vs Disadvantages


Advantages of Appium:

  1. Cross-platform support: Appium supports the automation of mobile applications on both iOS and Android platforms, making it a highly versatile tool.

  2. Open-source: Appium is an open-source tool, which means that it is free to use and has a large community of developers contributing to its development and maintenance.

  3. Wide range of programming language support: Appium supports a variety of programming languages, including Java, Python, Ruby, and JavaScript, making it easy for developers to write tests in the language of their choice.

  4. Integration with testing frameworks: Appium supports a wide range of testing frameworks, including JUnit, TestNG, and Cucumber, allowing developers to integrate their tests with their existing development workflow.

  5. Compatibility with real devices and emulators/simulators: Appium supports both real devices and emulators/simulators, allowing developers to test their applications in a variety of environments.

Disadvantages of Appium:

  1. Setup and configuration: Setting up Appium and configuring it to work with your environment can be a complex process, especially for beginners.

  2. Limited support for non-mobile applications: Appium is designed specifically for mobile application testing and may not be suitable for testing non-mobile applications.

  3. Limited support for older versions of Android: Appium may not support older versions of Android, which can be a limitation for developers who need to test their applications on older devices.

  4. Limited support for certain features: Appium may not support all the features of a particular mobile platform, which can be a limitation for developers who need to test specific features of their applications.

Older version of Android

  1. Android 2.3 (Gingerbread)

  2. Android 3.0/3.1/3.2 (Honeycomb)

  3. Android 4.0 (Ice Cream Sandwich)

  4. Android 4.1/4.2/4.3 (Jelly Bean)

Limited support for certain features

  1. Mobile device gestures: While Appium supports a wide range of gestures like tap, swipe, pinch, and zoom, there may be some limitations in terms of the complexity of gestures that can be performed on certain mobile platforms.

  2. Barcode scanning: Appium may not fully support barcode scanning features on certain mobile platforms, which can be a limitation for developers who need to test these features in their applications.

  3. Near Field Communication (NFC): Appium may not fully support NFC features on certain mobile platforms, which can be a limitation for developers who need to test these features in their applications.

  4. Camera and media capture: Appium may not fully support all camera and media capture features on certain mobile platforms, which can be a limitation for developers who need to test these features in their applications.


Tools & Libraries

1. Appium is tool that allows you to automate interactions with mobile devices

2. Appium Inspector is a tool that allows you to inspect elements of a moible application. However, in order to interact with the application using Appium, you must configure the capabilities of the device on which the application is running. These capabilities include information such as the device name, platform name, platform version, and the application's package and activity names

3. Android SDK: (https://developer.android.com/studio) is a software development kit to help you create virtual devices for testing by Android Virtual Device (AVD). You can create a virtual device by specifying the device's hardware configuration, such as the screen size, resolution, and memory, as well as The android platform version and system image

4. Android SDK Platform Tools: is a set of command-line utilities that allow developers to communicate with Android devices and emulators from a computer. These tools provide a way to debug, test, and analyze Android apps and system software from the command line.

Some of the tools included in the Android Platform Tools are ADB (Android Debug Bridge), Fastboot, Systrace, and Hprof-conv. ADB is the primary tool used for debugging and interacting with Android devices from a computer. Fastboot is used to flash system images and firmware updates to Android devices. Systrace is used to analyze system performance, and Hprof-conv is used to convert heap dump files into more readable formats.


adb devices   //all the devices that are currently connected to your 
                computer and authorized to communicate with ADB

4. IDE use for writing test script such as IntellJ, VSCode, Pycharm..



Installation

  • Appium Inspector: https://github.com/appium/appium-inspector

--> Download, extract, and run exe

Approach 1: If you installed Android Studio, you can install SDK Platform Tools

1) Open Android Studio and click on the "SDK Manager" icon in the toolbar.

2) In the SDK Manager window, click on the "SDK Tools" tab.

3) Scroll down to the "Android SDK Command-line Tools" section and check the box next to it.

4) Click "Apply" to download and install the Command-line Tools.

5) Add the platform-tools directory to your system's PATH variable so that you can run the adb command from any directory (C:\Users\hoadoan\AppData\Local\Android\Sdk\platform-tools)

6) Once the installation is complete, close and reopen Android Studio.

7) Open the terminal window from the toolbar or by pressing Alt + F12.

8) Type the command adb devices in the terminal window and press Enter.

9) This will list all the connected devices and emulators.

Approach 2: Download Android SDK Platform Tools

1) Download the Android SDK Platform Tools from the official Android website: https://developer.android.com/studio/releases/platform-tools

2) Extract the downloaded ZIP file to a folder on your computer. For example, you can extract it to C:\android-sdk\platform-tools on Windows or /Users/<your username>/android-sdk/platform-tools on macOS or Linux.

3) Open a terminal or command prompt on your computer.

4) Add the platform-tools directory to your system's PATH variable so that you can run the adb command from any directory


How to configure device capabilities to interact with devices in Applium Inspector


Precondition:

1) Start appium server

2) Device must be in running

3) You must set up ANDROID_HOME variable because Appium Inspector requires access to the Android SDK to interact with the Android device or emulator. The path of Android_home you can easily access by opening Android studio. Then click SDK Manager icon, look at Android SDK Location

Go to Environment Variable, set up ANDROID_HOME


It's our turn to configure the device's capabilities

Noted that deviceName is got from the adb devices command

Before starting session, please do not forget to save Capabilities so that you can reuse it later

Click on "Start Session" button, Appium inspector will display GUI of device and you now can inspect elements on that



You can use recordings in appium inspector to record steps


Build Frameworks

Dependencies

1) java-client: The Java client for Appium is a library that provides a Java API for writing test scripts that can automate interactions with mobile apps. It allows developers to write test scripts in Java programming language, which can then be executed on various mobile platforms like Android and iOS

2) The other dependencies like web UI Automation: selenium java, testng


Test Script


Connect to Appium Driver


  • Appium 1.0 (current)

public class Browser {
    public AndroidDriver getAndroidDriver() throws MalformedURLException {
        System.out.println("Create Appium Driver Instance");
        URL url = new URL("http://127.0.0.1:4723/wd/hub");
        // Appium 1 with Selenium 3
        DesiredCapabilities caps = getCapabilities();
        return new AndroidDriver(url, caps);
    }
    public DesiredCapabilities getCapabilities() {
        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
        caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, "13.0");
        caps.setCapability(MobileCapabilityType.DEVICE_NAME, "emulator-5554");
        caps.setCapability(MobileCapabilityType.APP, "C:/Users/hoadoan/Documents/ToDoList.apk");  //it should be put in configuration file
        return caps;
    }
}

  • Appium 2.0

public AppiumDriver getDriver() throws MalformedURLException {
        System.out.println("Create Appium Driver Instance");
        // Appium 2 with Selenium 4
        URL url = new URL("http://127.0.0.1:4723/wd/hub");
        BaseOptions options=getOptions();
        return new AppiumDriver(url, options);
    }
    public BaseOptions getOptions(){
        return new BaseOptions()
                .setPlatformName("Android")
                .setPlatformVersion("13.0")
//                .setAutomationName("Appium") //appium 2.o
                .amend("deviceName", "emulator-5554")
                .amend("app", "C:/Users/hoadoan/Documents/ToDoList.apk")
                .amend("noReset", true);
    }

Locator Strategy


MobileElement is deprecated in java-client version 7. Now please use AppiumBy


Here are some of the methods of AppiumBy that you can use in your test automation:

  • Accessibility ID

  • Class Name

  • ID

  • Name

  • XPath

In my opinion, you should use the recording in Appium Inspector to get the element with the selected language




Strategies to use Android-specific locators in Appium

After commonly supported strategies to use locators in Appium, there are few platforms and automation type-specific locator strategies. Let’s look into the Android-specific locators in Appium in detail.

UIAutomator2: UIAutomator Selector

The UIAutomator selector locator in Appium is one of the unique locator strategies where you have to create a Java statement and pass it as the locator text to the method. In this statement, you need to use the UiSelector class to build the Java statement.


For example, In some cases, you would like to get the element which contains text


clickElement(AppiumBy.ByAndroidUIAutomator.androidUIAutomator("new UiSelector().text(\"Personal\")"));

You should put common method to reuse it


public void selectOptionFromDropDown(By locator, String text) {
    clickElement(locator);
    clickElement(AppiumBy.ByAndroidUIAutomator.androidUIAutomator(String.format("new UiSelector().text(\"%s\")", text)));
}

To check whether or not the locator is correct or not. Please use the search function in Appium Inspector




Basic Concepts

1) Appium Inspector

Appium Inspector is a GUI (Graphical User Interface) tool that is used to inspect and interact with the user interface of mobile applications during the testing process. It is a part of Appium Desktop, which is a free and open-source application that provides a user-friendly interface for running Appium tests and managing Appium servers.

With Appium Inspector, testers can easily inspect the elements of the mobile app's user interface and generate XPath or other locator strategies for those elements. This allows them to create more accurate and robust test scripts that can interact with the app's UI elements.

Appium Inspector provides several features that can help testers, including:

  1. Element inspector: Allows testers to view the app's UI elements and their attributes.

  2. Element locator: Helps testers to generate XPath, ID, or other locator strategies for UI elements.

  3. Interactive testing: Allows testers to interact with the app's UI elements and perform actions like clicking, swiping, scrolling, and typing.

  4. Highlighting: Highlights the UI elements on the device or emulator screen when they are selected in Appium Inspector.

Appium Desired Capabilities

Desired Capabilities are keys and values encoded in a JSON object, sent by Appium clients to the server when a new automation session is requested. They tell the Appium drivers all kinds of important things about how you want your test to work. Each Appium client builds capabilities in a way specific to the client's language, but at the end of the day, they are sent over to Appium as JSON objects.


{ "platformName": "iOS",

"platformVersion": "11.0",

"deviceName": "iPhone 7",

"automationName": "XCUITest",

"app": "/path/to/my.app"

}


2) What is Android SDK

The Android SDK (Software Development Kit) provides the necessary tools and libraries to interact with Android devices and emulators. It includes the Android Debug Bridge (ADB), which is a command-line tool that enables communication between your computer and an Android device or emulator. The ADB allows you to install, uninstall, and debug applications on Android devices and emulators, among other things.


In addition, the Android SDK provides other essential tools and components such as Android Emulator, which allows you to create and run virtual Android devices on your computer, and the Android SDK Build-Tools, which provide the necessary utilities for building and packaging Android applications.


Therefore, to perform mobile automation testing on Android devices or emulators, you need to have the Android SDK installed on your computer so that you can interact with the device or emulator using Appium or any other testing framework.


3) Mobile Platform Types


Common Questions

1) Appium is built on the idea that testing native apps shouldn't require including an SDK?

Certainly! When developing and testing native mobile apps, software developers and testers traditionally rely on Software Development Kits (SDKs) provided by the platform vendors (such as Android SDK or iOS SDK). These SDKs contain a set of tools, libraries, and documentation that enable developers to create and test mobile apps for that specific platform.

However, Appium challenges this traditional approach by allowing developers and testers to test native mobile apps without having to include an SDK or recompile the app. Instead, Appium uses a "cross-platform" automation framework that is built on top of existing platform-specific automation frameworks (such as UIAutomator for Android and XCUITest for iOS).

This means that Appium enables testers to write automated tests using familiar programming languages and test frameworks, such as Java, Python, Ruby, and Selenium WebDriver, rather than having to learn platform-specific languages and SDKs.


2) What is Appium's API?

It's WebDriver spec

Of course, Appium wants to support the cases where user interaction does differ from web to mobile or web to TV, and so Appium also makes use of the built-in extensibility of the WebDriver spec. The result is that no matter what platform you want to automate, when you use Appium, you will do so using the standard WebDriver spec, with two caveats:

  • We might not have any way to support a particular WebDriver API command on a given platform, and so some commands might be unsupported (for example, getting or setting cookies is not possible in the world of native mobile app automation).

  • We might support automation behaviors that go beyond what's available in the WebDriver API command list, though any such commands will be valid and spec-compliant extensions to the WebDriver API.

3) Issues in running appium


--> What you need to do is to uninstall uiautomator apps. Go to Android settings -> Apps. Then search for uiautomator, 2 apps will pop up. Uninstall those and you’ll be good to go. This error occurs when the sessions crash over and over again so change your code in such a way that if it causes some error, it automatically quits the driver.


Solution: Stop appium server and execute below commands


adb uninstall io.appium.uiautomator2.server  
adb uninstall io.appium.uiautomator2.server.test


Write Appium Log

appium --log-level debug >

89 views0 comments

Recent Posts

See All

Comments


Post: Blog2_Post
bottom of page