You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Applitools中下载差异高亮图片?Java Selenium用TestResultsHandler遇错求示例

使用Java Selenium通过TestResultsHandler下载Applitools差异图的解决方案及示例

1. 依赖配置(Maven)

首先确保你的pom.xml中包含正确的Applitools依赖,建议使用稳定兼容版本(如5.64.0):

<dependencies>
    <!-- Applitools Eyes Selenium核心依赖 -->
    <dependency>
        <groupId>com.applitools</groupId>
        <artifactId>eyes-selenium-java5</artifactId>
        <version>5.64.0</version>
    </dependency>
    <!-- TestResultsHandler所需的通用依赖 -->
    <dependency>
        <groupId>com.applitools</groupId>
        <artifactId>eyes-common-java5</artifactId>
        <version>5.64.0</version>
    </dependency>
    <!-- Selenium WebDriver依赖 -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>4.11.0</version>
    </dependency>
</dependencies>

2. 完整可运行代码示例

以下是包含测试执行、结果获取及差异图下载的完整Java类:

import com.applitools.eyes.BatchInfo;
import com.applitools.eyes.Eyes;
import com.applitools.eyes.TestResults;
import com.applitools.eyes.results.TestResultsHandler;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import java.io.File;
import java.io.IOException;

public class ApplitoolsDiffDownloader {
    public static void main(String[] args) {
        WebDriver driver = new ChromeDriver();
        Eyes eyes = new Eyes();
        
        try {
            // 从环境变量读取API密钥,避免硬编码
            eyes.setApiKey(System.getenv("APPLITOOLS_API_KEY"));
            // 设置测试批次标识
            eyes.setBatch(new BatchInfo("Diff Download Demo Batch"));
            
            // 启动Applitools测试会话
            eyes.open(driver, "Demo Web App", "Visual Check with Diff Download");
            
            // 导航到目标页面并执行视觉检查
            driver.get("https://example.com");
            eyes.checkWindow("Initial Page Load");
            
            // 结束测试并获取结果(false表示不自动抛异常)
            TestResults results = eyes.close(false);
            
            // 初始化结果处理器
            TestResultsHandler handler = new TestResultsHandler(results);
            
            // 创建保存目录
            String savePath = "./applitools_diff_images/";
            new File(savePath).mkdirs();
            
            // 下载差异图、基准图、当前截图
            handler.downloadDiffs(new File(savePath));
            handler.downloadBaselineImages(new File(savePath));
            handler.downloadCurrentImages(new File(savePath));
            
            System.out.println("图片已保存至: " + savePath);
            
        } catch (IOException e) {
            System.err.println("下载失败: " + e.getMessage());
            e.printStackTrace();
        } finally {
            // 确保关闭会话和驱动
            eyes.abortIfNotClosed();
            driver.quit();
        }
    }
}

3. 常见错误排查

  • 依赖版本不兼容:必须保证eyes-selenium-java5和eyes-common-java5版本完全一致,版本 mismatch 会导致TestResultsHandler初始化失败。
  • API密钥无效:检查密钥是否与Applitools控制台的一致,建议通过环境变量传递而非硬编码。
  • 无差异结果:如果测试没有视觉差异,downloadDiffs不会生成文件,确保测试场景确实存在视觉变化。
  • 网络/代理问题:无法连接Applitools服务时,可配置代理:
    eyes.setProxy(new ProxySettings("proxy-host", 8080));
    
  • 权限不足:确保保存目录有读写权限,避免因权限问题导致文件写入失败。

内容的提问来源于stack exchange,提问作者Sharad Shrestha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 09:48:13