Registering Appium Nodes to the Grid Hub
To integrate Appium nodes with a Selenium Grid Hub, you must define node configurations in JSON format. When running multiple Appium instances on a single host, each instance requires its own port and corresponding configuration file.
Use the following command to launch an Appium node, ensuring you specify unique ports for multiple instances:
appium -p 5723 --nodeconfig node_config_02.json
The configuration file (e.g., node_config_02.json) should be structured as follows:
{
"capabilities": [
{
"browserName": "android",
"version": "7.0",
"maxInstances": 1,
"platform": "ANDROID"
}
],
"configuration": {
"cleanUpCycle": 2000,
"timeout": 30000,
"proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
"url": "http://127.0.0.1:5723/wd/hub",
"host": "127.0.0.1",
"port": 5723,
"maxSession": 1,
"register": true,
"registerCycle": 5000,
"hubPort": 4444,
"hubHost": "192.168.31.69",
"hubProtocol": "http"
}
}
Managing Multiple Devices and Emulators
When executing tests across multiple emulators or real devices, it is crtiical to target the specific device using the -s flag with the adb utility:
adb -s emulator-5554 shell am start -n com.example.app/.MainActivity
In your test scripts, replace the direct Appium server URL with the Grid Hub address. Use the udid capability to ensure the test is routed to the corect device:
import os
from appium import webdriver
class TestMobileGrid:
def setup_method(self):
options = {
'platformName': 'Android',
'platformVersion': '6.0',
'appPackage': 'com.example.app',
'appActivity': '.MainActivity',
'udid': os.getenv('TARGET_UDID')
}
self.driver = webdriver.Remote('http://192.168.31.69:4444/wd/hub', options)
self.driver.implicitly_wait(10)
def teardown_method(self):
self.driver.quit()
def test_search_functionality(self):
self.driver.find_element_by_id('search_bar').click()
self.driver.find_element_by_id('input_field').send_keys('Keyword')
self.driver.find_element_by_xpath("//*[@text='Keyword']").click()
Parallel Execution with Shell Scripts
To trigger tests across all connected devices simultaneously, you can automate the process using a shell script. The & operator allows for background, concurrent execution:
#!/bin/bash
for device_id in $(adb devices | grep "device$" | awk '{print $1}')
do
echo "Launching test on: ${device_id}"
TARGET_UDID=$device_id pytest test_suite.py &
done
Key Considerations:
- Session Cleanup: Always include
driver.quit()in your teardown methods. Failing to close sessions will keep nodes occupied, preventing them from accepting new tasks from the Hub. - Routing: The Hub acts as a proxy; ensure your script points to the Hub's IP/port, not the local Appium server.
- Device Targeting: When multiple devices share the same version, the
udidparameter takes precedence overplatformVersionordeviceNamefor ensuring test isolation.