我正在尝试使用 Appium 自动化 Android 应用程序的消息传递场景,其中 Device1 向 Device2 发送消息。我的目标是在两台真实的 Android 设备上运行测试。这是我想要实现的流程:
1.在端口4723和4725上启动Appium服务器,将两台设备连接到不同的端口。 2.初始化设备 ID 和其他所需功能。 3.在端口4723和4725上初始化两个Appium驱动。 4.使用driver1从Device1发送消息,并使用driver2验证Device2中消息的接收。
问题: 1.测试用例仅在一台设备上执行,而不是在两台设备上并行执行。 2.所有执行均在真实设备上进行。
问题: 为了在两台设备上运行此测试用例,我需要做什么?任何有关使用线程或任何其他方法的指导将不胜感激!
代码示例
from appium import webdriver
from appium.options.android import UiAutomator2Options
from appium.webdriver.common.appiumby import AppiumBy
import time
def send_message_on_device_1(driver):
""" Function to send a message from device 1 """
# Code to send message
def verify_message_on_device_2(driver):
""" Function to verify the message on device 2 """
# Code to verify message
def initialize_driver(device_name, port):
""" Function to initialize Appium driver for each device """
desired_caps = {
"platformName": "Android",
"deviceName": device_name,
"appPackage": "your.app.package",
"appActivity": "your.app.activity",
"automationName": "UiAutomator2",
"noReset": True
}
capabilities_options = UiAutomator2Options().load_capabilities(desired_caps)
appium_server_url = f'http://127.0.0.1:{port}/wd/hub'
driver = webdriver.Remote(command_executor=appium_server_url, options=capabilities_options)
return driver
if __name__ == "__main__":
# Initialize Appium driver for device 1 on port 4723
driver1 = initialize_driver("device1_udid", 4723)
# Initialize Appium driver for device 2 on port 4725
driver2 = initialize_driver("device2_udid", 4725)
try:
# Send message from device 1
send_message_on_device_1(driver1)
# Verify message on device 2
verify_message_on_device_2(driver2)
finally:
# Quit both drivers
driver1.quit()
driver2.quit()
尝试以下appium选项:
-udid:唯一的设备 ID。您将通过以下命令获取 id:“adb.exe devices”
-systemPort:每个并行会话的 UiAutomator2 服务器的唯一系统端口号
因此您想要的上限和函数定义应如下所示:
def initialize_driver(device_name, port, udid_device, port_AndroidUiautomator):
""" Function to initialize Appium driver for each device """
desired_caps = {
"platformName": "Android",
"deviceName": device_name,
"appPackage": "your.app.package",
"appActivity": "your.app.activity",
"automationName": "UiAutomator2",
"noReset": True,
"udid": udid_device,
"systemPort": port_AndroidUiautomator # port of your UiAutomator2 server e.g. from range 8200..8299
}
capabilities_options = UiAutomator2Options().load_capabilities(desired_caps)
appium_server_url = f'http://127.0.0.1:{port}/wd/hub' # here is the port of your appium server
driver = webdriver.Remote(command_executor=appium_server_url, options=capabilities_options)
return driver