Appium报错No route found for /session/.../appium/compare_images求助
解决Appium图片比对报错「No route found for /session/.../appium/compare_images」
这个报错核心原因是Appium Server与客户端版本不兼容,或缺少图像比对所需的扩展支持,以下是具体解决步骤:
1. 检查并升级Appium Server版本
- 执行
appium -v查看当前版本,确保版本≥1.8.0(建议使用最新稳定版) - 升级命令:
npm install -g appium
2. 匹配客户端依赖版本
- Maven用户调整
pom.xml中Java客户端版本,确保与Server兼容:<dependency> <groupId>io.appium</groupId> <artifactId>java-client</artifactId> <version>8.6.0</version> <!-- 对应Appium 2.x稳定版 --> </dependency> - Gradle用户调整
build.gradle:implementation 'io.appium:java-client:8.6.0'
3. 修正代码中的Base64编码错误
你的代码错误地对截图字节数组做了Base64编码,findImageOccurrence方法需要原始字节数组,Appium客户端会自动处理编码逻辑。修改这一行:
错误代码:
byte[] fullActualImage = Base64.encodeBase64(commonCucumberStep.getUiRunner().getAndroidDriver().getScreenshotAs(OutputType.BYTES));
修正后:
byte[] fullActualImage = commonCucumberStep.getUiRunner().getAndroidDriver().getScreenshotAs(OutputType.BYTES);
4. 启用Appium图像比对插件(仅Appium 2.x需要)
- 安装UIAutomator2驱动和图像插件:
appium driver install uiautomator2 appium plugin install images - 启动Appium Server时加载插件:
appium --use-plugins=images
修正后的完整代码示例
import java.io.File; import java.io.IOException; import java.nio.file.Files; import org.junit.Assert; import org.hamcrest.Matchers; import io.appium.java_client.imagecomparison.OccurrenceMatchingOptions; import io.appium.java_client.imagecomparison.OccurrenceMatchingResult; // ... 你的测试类代码 try { File expectedPartialImageFile = new File("path/to/your/target-partial-image.png"); byte[] expectedPartialImage = Files.readAllBytes(expectedPartialImageFile.toPath()); byte[] fullActualImage = commonCucumberStep.getUiRunner().getAndroidDriver().getScreenshotAs(OutputType.BYTES); OccurrenceMatchingOptions options = new OccurrenceMatchingOptions() .withEnabledVisualization(); OccurrenceMatchingResult result = commonCucumberStep.getUiRunner().getAndroidDriver() .findImageOccurrence(fullActualImage, expectedPartialImage, options); Assert.assertThat(result.getVisualization().length, Matchers.greaterThan(0)); Assert.assertNotNull(result.getRect()); } catch (IOException e) { e.printStackTrace(); }
内容的提问来源于stack exchange,提问作者Dany
相关产品推荐
相关产品推荐

