Managing Concurrent Mobile Automation on Multiple Devices with Appium and Python
When scaling mobile automation to run across multiple terminals simultaneously, utilizing Python's multi-threading capabilities is the standard approach. However, identifying devices correctly within the Appium server session is a common point of failure for many engineers.
Ensuring Device Uniqueness with UDID
In a multi-device setup, the deviceName capability in the Appium configuration is often descriptive rather than functional. Many developers assume this field uniquely identifies the phone, but Appium frequently ignores it or picks the first available device if only deviceName is provided. The udid (Unique Device Identifier) is the essential parameter for routing commands to specific hardware.
You can retrieve the udid by running the following command in your terminal:
adb devices
The output will list the serial numbers:
List of devices attached
SN12345678 device
SN87654321 device
In your automation script, you must include the udid in your desired capabilities to ensure the driver binds to the correct device. Below is an example of how to structure the session initialization:
from appium import webdriver
def create_driver_session(device_id, package_id, activity_name, platform_ver):
# udid is the critical parameter for parallel execution
capabilities = {
"platformName": "Android",
"platformVersion": platform_ver,
"automationName": "UiAutomator2",
"udid": device_id,
"deviceName": device_id,
"appPackage": package_id,
"appActivity": activity_name,
"noReset": True,
"autoAcceptAlerts": "true",
"newCommandTimeout": 180
}
# Connect to the Appium server
executor_url = "http://127.0.0.1:4723/wd/hub"
driver = webdriver.Remote(executor_url, capabilities)
return driver
Consistency in Application Package Names
Another hurdle when deploying tests to multiple devices is the inconsistency of package names across various distribution channels. The same application might have different suffixes depending on whether it was downloaded from an OEM-specific store (like Vivo or Xiaomi) or a third-party browser.
For example, a application package might vary as follows:
- Standard Version:
com.example.app.main - OEM Distributed Version:
com.example.app.oem_suffix
When automating parallel tests, ensure that the APKs installed across all target devices are from the same source to avoid ActivityNotFound errors. Standardizing the installation processs—such as using a single shared APK file via adb install—is the most reliable way to maintain consistent package names and activities across the entire test fleet.