如何用python高效并行控制多台android设备?

问题描述 投票:0回答:1

我是一名比利时计算机科学专业的学生,正在从事一个项目,该项目涉及“通过 Python 脚本同时控制多个 Android 设备”。在测试过程中,我在使用 Android Studio 模拟器实现有效的多线程方面遇到了挑战,在连接到真实设备之前我最初使用的是 Android Studio 模拟器。我探索过 Appium 和 Selenium 服务器等解决方案,但事实证明,找到符合我需求的清晰信息和简单工作流程非常困难。 我尝试使用ADB和Appium,但一次只能控制一部手机。我对 Appium 和 Selenium 服务器的实验非常具有挑战性,我正在努力寻找适合此任务的清晰信息或简单的工作流程。

这种情况阻止了我的进步,我欢迎任何可以简化通过 Python 同时控制 Android 模拟器的过程的建议或建议。每个模拟器不应该执行相同的任务;他们应该遵循 Python 脚本概述的程序路径。我愿意完全改变我尝试实施的工作流程。

下面是我尝试使用 ADB 检索连接的设备,并初步尝试为每个设备启动 Appium 服务器:

# Function to get connected devices def get_connected_devices(): devices_configs = [] try: output = subprocess.check_output([adb_path, 'devices']).decode('utf-8') devices = output.strip().split('\n')[1:] for device in devices: ud_id = device.split('\t')[0] os_version = ( subprocess.check_output( [ adb_path, '-s', ud_id, 'shell', 'getprop', 'ro.build.version.release', ] ) .decode('utf-8') .strip() ) platform = "Android" system_port = str(8200 + len(devices_configs)) chrome_driver_port = str(8100 + len(devices_configs)) device_details = { "device": ud_id, "os_version": os_version, "ud_id": ud_id, "platform": platform, "systemPort": system_port, } devices_configs.append(device_details) return devices_configs except subprocess.CalledProcessError as e: print(f"Error executing ADB command: {e}") except Exception as e: print(f"An unexpected error occurred: {e}") # Retrieve connected devices connected_devices = get_connected_devices() # Start Appium servers for each device processes = [] for i in range(len(connected_devices)): port = 4724 + i cmd = ["start", "appium", "-p", str(port)] process = subprocess.Popen(cmd, shell=True) processes.append(process)

尽管如此,我在强制 Appium 使用特定设备端口时遇到了问题,因为它对所有设备都使用相同的端口:

# Pair each connected device with a server device_server_pairs = list(zip(connected_devices, servers)) # Start Appium sessions using threads with concurrent.futures.ThreadPoolExecutor() as executor: executor.map(start_appium_session, device_server_pairs) # Function to start Appium session for each device def start_appium_session(device_config_server_tuple): device_config, server = device_config_server_tuple capabilities = { 'platformName': device_config['platform'], 'platformVersion': device_config['os_version'], 'deviceName': device_config['ud_id'], 'systemPort': device_config['systemPort'], 'app': APP_PATH, } driver = webdriver.Remote(server, options=UiAutomator2Options().load_capabilities(capabilities))

我很感激您可以提供的任何见解或改进。

python android multithreading adb selenium-grid
1个回答
0
投票
AndroidViewClient/culebra

,它可以一次连接多个设备。 您可以在

https://github.com/dtmilano/AndroidViewClient/wiki/Resources#screencasts-and-videos

找到一些视频,特别是 Culebra GUI:多设备测试。虽然它显示在所有设备上运行相同的测试脚本,但没有什么可以阻止您运行完全不同的东西。

© www.soinside.com 2019 - 2024. All rights reserved.