Parallel tests with Appium and Genymotion Device Image

2024-04-23

Prerequisite

First, you will need to enable and connect your devices to adb. To do so, please refer to Connect to ADB.

Start Appium server

> Important: The following tutorial requires Appium 2.0 or higher.

It’s easy to do parallel testing using only one appium server. Before that, we had to start N appium servers in order to test N devices in parallel.

So let’s start an Appium server, simply using the basic command: appium

Write your tests script in Python

> Note: We will use Python throughout this tutorial but you can use a different language if you prefer.

We’ve chosen to use Pytest as our test framework. It has several useful plugins like:

Here is a simple python script. Please replace [instanceX_ip] values by the IPs of your 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()

view raw paas_appium_test_python.py

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

How to run Python tests in parallel

If we run the python script as follows:

Bash pytest test_example.py

pytest will execute tests on one device at a time. However, in order to have results as soon as possible, we need to execute all the tests at the same time. To do that, we can take advantage of the pytest-xdist plugin and run the following command:

Bash pytest -n 3 test_example.py

How to run Java tests in parallel

A Riddle application is used for testing. It runs 2 same tests on 2 devices.

The application needs to be already installed on the device but you can change those 2 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 there. Replace the udid by the ADB serial number of the device.

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

To see an example of: