Parallel tests with Appium and Genymotion Device Image

Parallel tests with Appium and Genymotion Device Image

2024-04-23

Prerequisite

Prerequisite: Enable and connect devices to adb.

Refer to the Connect to ADB guide for steps.

Start Appium server

Important: The following tutorial requires Appium 2.0 or higher.

Parallel testing can be done with a single Appium server.

Previously, testing required starting N Appium servers for N devices.

Start an Appium server using the basic command: appium

Write your tests script in Python

Python is the language used in this tutorial.

Pytest will be used as the test framework.

Pytest supports plugins such as xdist for parallel tests.

Pytest supports plugins such as rerunfailures to rerun flaky tests.

Here is a simple Python script.

Replace [instanceX_ip] values with the IPs of the instances.

# -*- coding: utf-8 -*-
import pytest
from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy
from appium.options.android import UiAutomator2Options

def create_android_driver(udid, systemPort):
    # Setting capabilities directly on the options object
    capabilities = {
        "appium:deviceName": "Genymotion Cloud PaaS",
        "platformName": "Android",
        "automationName": "UiAutomator2",
        "appium:udid": udid,
        "appium:systemPort": systemPort,
        "appium:appPackage": "com.android.settings",
        "appium:appActivity": ".Settings"
    }
    url = "http://localhost:4723"
    options = UiAutomator2Options()
    options.load_capabilities(capabilities)
    driver = webdriver.Remote(url, options=options)
    # Return the driver
    return driver

@pytest.mark.parametrize(
    "udid, systemPort",
    [
        ("[instance1_ip]:5555", "8201"),
        ("[instance2_ip]:5555", "8202"),
        ("[instance3_ip]:5555", "8203"),
    ],
)
def test_sum(udid, systemPort):  # Accept udid and systemPort as parameters
    driver = create_android_driver(udid, systemPort)
    try:
        driver.find_element(by=AppiumBy.XPATH, value='//*[@text="Battery"]')
    finally:
        # Ensure proper teardown
        driver.quit()

For convenience, we will call this Python script test_example.py.

How to run Python tests in parallel

pytest test_example.py

pytest -n 3 test_example.py

The first command runs tests on one device at a time. The second command uses multiple workers to run tests in parallel. The number of workers is defined by -n.

How to run Java tests in parallel

A Riddle application is used for testing.

It runs two tests on two devices.

The application needs to be installed on the device.

You can change those two lines to UiAutomator2Options().setApp($PWD/Apps/appriddle.apk).

where $PWD is the absolute path where your project is located.

Configuring devices used for testing is described there. Replace the udid with the ADB serial number of the device.

Run the tests: 1. Start the devices and connect them to your computer/CI server 2. Start appium server: appium 3. Run the project: mvn test

To see an example of test sharding and flaky tests branches: