Date: 2024-04-23
Prerequisite
- Enable and connect your devices to adb.
- For instructions, refer to Connect to ADB.
Start Appium server
- The following tutorial requires Appium 2.0 or higher.
- It is possible to do parallel testing using only one Appium server.
- Previously, we had to start N Appium servers to test N devices in parallel.
- To start an Appium server, use the basic command: appium
Write your tests script in Python
- We will use Python for this tutorial.
- You can use a different language if you prefer.
- We have chosen Pytest as the test framework.
- Pytest has useful plugins such as xdist for running tests in parallel.
- Pytest has a plugin called rerunfailures for rerunning failed tests.
- Here is a simple Python script. 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()
How to run Python tests in parallel
- Pytest will execute tests on one device at a time.
- To run all tests at the same time, use the pytest-xdist plugin.
- Run: pytest -n 3 test_example.py
- -n indicates the number of worker processes you want to use.
- Here we start the suite with 3 processes.
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 installed on the device.
- You can change those 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:
- Start the devices and connect it to your computer/CI server.
- Start Appium server: appium
- Run project: mvn test
- To see an example of test sharding (splitting a set of tests and run each batch on several devices), please refer to the test_sharding branch.
- To see an example of implementing retries to avoid flaky tests, please refer to the flaky_tests branch.
[End of content]