咨询Appium模拟器与真机并行测试方法及adb设备识别问题
Android自动化测试:并行运行+模拟器名称显示问题解答
Hey there! Let's tackle your two questions one by one—they’re both super common when setting up Android automation testing, so I’ve got you covered.
一、如何在模拟器与真机上并行运行自动化测试
实现并行测试主要看你用的测试框架,下面分两种主流场景说明:
1. 原生Android测试(Espresso + Android Test Orchestrator)
这是Google官方推荐的原生测试方案,支持设备级并行:
- Step 1: 配置Module级build.gradle
开启Test Orchestrator并添加依赖,让测试能在多设备上并行执行:android { defaultConfig { testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" // 启用Test Storage服务,支持多设备测试隔离 testInstrumentationRunnerArguments useTestStorageService: 'true' } testOptions { // 启用Android Test Orchestrator,实现测试用例隔离 execution 'ANDROIDX_TEST_ORCHESTRATOR' animationsDisabled = true // 配置单元测试并行(可选) unitTests.all { maxParallelForks = Runtime.runtime.availableProcessors() } // 配置仪器测试的Orchestrator参数 orchestrator { clearPackageData = true // 每次测试后清除应用数据,避免干扰 } } } dependencies { androidTestImplementation 'androidx.test:runner:1.5.2' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' androidTestUtil 'androidx.test:orchestrator:1.5.2' } - Step 2: 连接所有目标设备
确保你的真机(开启USB调试)和模拟器都正常连接,用adb devices确认所有设备都被识别。 - Step 3: 运行并行测试
执行Gradle命令:
这条命令会自动在所有已连接的设备上并行执行你的仪器测试用例。./gradlew connectedAndroidTest
2. 跨平台自动化测试(Appium)
如果用Appium做跨设备测试,需要为每个设备创建独立的Driver实例:
- Step 1: 准备设备信息
用adb devices获取每个设备的udid(真机是一串字符,模拟器是emulator-xxxx)。 - Step 2: 编写多Driver并行代码
以Java为例,用线程池创建多个Driver,分别绑定不同设备:// 设备1:模拟器 DesiredCapabilities emulatorCaps = new DesiredCapabilities(); emulatorCaps.setCapability("deviceName", "MyCustomEmulator"); emulatorCaps.setCapability("udid", "emulator-5554"); emulatorCaps.setCapability("platformName", "Android"); emulatorCaps.setCapability("app", "/path/to/your/app.apk"); // 设备2:真机 DesiredCapabilities realDeviceCaps = new DesiredCapabilities(); realDeviceCaps.setCapability("deviceName", "MyRealPhone"); realDeviceCaps.setCapability("udid", "abc123456789"); // 替换为你的真机UDID realDeviceCaps.setCapability("platformName", "Android"); realDeviceCaps.setCapability("app", "/path/to/your/app.apk"); // 用线程池并行启动测试 ExecutorService executor = Executors.newFixedThreadPool(2); executor.submit(() -> new AndroidDriver(new URL("http://localhost:4723/wd/hub"), emulatorCaps)); executor.submit(() -> new AndroidDriver(new URL("http://localhost:4723/wd/hub"), realDeviceCaps)); - Step 3: 启动Appium服务并运行测试
启动Appium服务器后,运行你的测试代码,两个设备就会同时执行测试。
二、adb devices显示emulator-5554而非自定义名称的解决方法
这个是因为emulator-xxxx是模拟器的默认ADB识别标识(基于它的监听端口,5554是第一个模拟器的默认端口),而你设置的是AVD的名称,ADB不会自动关联。解决方法有两种:
方法1: 命令行启动模拟器时指定名称
用以下命令启动模拟器,直接指定ADB识别的设备名称:
emulator -avd YOUR_CUSTOM_AVD_NAME -prop ro.adb.device.name=YOUR_DESIRED_DEVICE_NAME
替换YOUR_CUSTOM_AVD_NAME为你创建的AVD名称,YOUR_DESIRED_DEVICE_NAME为你想在adb devices中看到的名称。
方法2: 修改AVD配置文件永久生效
- 找到你的AVD配置目录:一般在
~/.android/avd/[你的AVD名称].avd/(Windows是C:\Users\[你的用户名]\.android\avd\[你的AVD名称].avd\) - 打开
config.ini文件,添加一行:ro.adb.device.name=YOUR_DESIRED_DEVICE_NAME - 保存文件后,重启模拟器,再执行
adb kill-server && adb start-server,之后adb devices就会显示你的自定义名称了。
Hope these solutions work smoothly for you! If you hit any roadblocks, feel free to ask more details.
内容的提问来源于stack exchange,提问作者sandy
相关产品推荐
相关产品推荐

