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

Karate UI:AWS Codepipeline中文件下载验证问题

解决Karate UI测试中AWS Codepipeline下载文件定位问题

方法一:修改Chrome配置指定项目内下载目录

在karate-config.js中配置Chrome的下载偏好,直接指定项目目录下的文件夹,确保本地和CI环境路径一致:

function fn() {
  // 定义项目内的目标下载目录,比如根目录下的target/downloads
  const downloadDir = karate.os.name.startsWith('windows') 
    ? 'target\\downloads' 
    : 'target/downloads';
  // 确保目录存在,避免下载失败
  karate.createDir(downloadDir);

  const chromeOptions = {
    args: [
      '--no-sandbox', // AWS CI环境必需,解决权限限制
      '--disable-dev-shm-usage',
      '--headless=new' // 无头模式适配CI运行
    ],
    prefs: {
      'download.default_directory': karate.pathTo(downloadDir), // 绑定项目内目录
      'download.prompt_for_download': false, // 关闭下载弹窗
      'download.directory_upgrade': true,
      'safebrowsing.enabled': true
    }
  };

  const driverConfig = {
    type: 'chrome',
    options: chromeOptions,
    start: true
  };

  return { driver: driverConfig };
}

核心要点:

  • 用karate.pathTo()处理路径,自动适配Windows/Linux环境
  • 提前创建目录避免下载失败
  • AWS环境必须添加--no-sandbox等参数,否则Chrome无法启动

方法二:CI环境下主动定位默认下载目录

如果修改配置遇到阻碍,可以在测试用例中直接指定CI环境的默认下载路径:

Scenario: 验证下载文件存在
  Given driver 'https://your-app-url.com'
  When click('#download-button')
  Then wait 10 // 等待文件下载完成
  And def ciDownloadDir = karate.os.name.startsWith('windows') ? 'C:\\Users\\root\\Downloads' : '/home/ubuntu/Downloads'
  And def targetFile = ciDownloadDir + '/your-file-name.pdf'
  And assert karate.exists(targetFile)

说明:AWS Codepipeline的Linux环境默认下载目录通常是/home/ubuntu/Downloads或/root/Downloads,可通过CI日志打印路径确认。

额外注意事项

  • 确保CI运行用户对项目目录有读写权限
  • 下载后添加足够等待时间,避免文件未完全写入就触发验证
  • 测试结束后清理下载目录,防止残留文件干扰后续用例

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 23:52:04