Prerequisite
First, enable and connect your devices to adb.
To enable and connect your devices to adb, refer to Connect to ADB.
Start Appium server
> Important: The following tutorial requires Appium 2.0 or higher.
It is easy to do parallel testing using only one appium server. Before that, it required starting N appium servers in order to test N devices in parallel.
Start an Appium server using the basic command: appium
Write your tests script in Python
> Note: This tutorial uses Python throughout the tutorial but you can use a different language if you prefer.
This tutorial uses Pytest as the test framework.
Pytest has several useful plugins like:
xdist: for running tests in parallel.rerunfailures: for rerunning failed tests an arbitrary number of times to reduce build failures due to flaky 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() |
view raw paas_appium_test_python.pyFor convenience, call this python script test_example.py.
How to run Python tests in parallel
If the python script is run as follows:
Bash
pytest test_example.py
pytest executes tests on one device at a time.
However, in order to have results as soon as possible, execute all the tests at the same time.
To execute all the tests at the same time, take advantage of the pytest-xdist plugin and run the following command:
Bash
pytest -n 3 test_example.py
-n: number of worker processes you want to use.
This command starts the suite with 3 processes.
How to run Java tests in parallel
A Riddle application is used for testing.
The application runs 2 same tests on 2 devices.
The application needs to be already installed on the device.
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 them to your computer/CI server
2. Start appium server: appium
3. 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
- implementing retries to avoid flaky tests, please refer to the flaky_tests branch