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

如何将服务器文件传输至Cypress的fixtures文件夹?

在Cypress中从文件服务器下载文件到Fixtures目录的方案

针对你提到的大文件无法存入Git、需要从自建服务器获取的场景,以下是几种实用的实现方式:

方法1:使用Cypress内置命令直接下载

利用cy.request获取文件二进制数据,再通过cy.writeFile写入Fixtures目录,适合单文件或少量文件的按需下载。

// 在测试用例或前置钩子中执行
cy.request({
  url: 'http://你的文件服务器地址/大文件.pdf',
  method: 'GET',
  responseType: 'arraybuffer' // 针对二进制文件设置
}).then((response) => {
  // 定义Fixtures中的目标路径
  const targetFixturePath = 'downloaded-files/大文件.pdf';
  // 写入文件,指定binary编码确保文件完整性
  cy.writeFile(`cypress/fixtures/${targetFixturePath}`, response.body, 'binary');
});

// 之后即可像普通Fixture一样使用
cy.fixture('downloaded-files/大文件.pdf', 'binary').then(fileContent => {
  // 示例:上传文件操作
  cy.get('input[type="file"]').attachFile({
    fileContent,
    fileName: '大文件.pdf',
    mimeType: 'application/pdf'
  });
});

方法2:测试启动前批量下载(Node.js脚本)

在Cypress配置中通过Node.js代码实现批量下载,适合测试套件启动前预先获取所有需要的文件,用流处理大文件更高效。

首先安装依赖:

npm install axios --save-dev

修改cypress.config.js:

const { defineConfig } = require('cypress');
const fs = require('fs');
const path = require('path');
const axios = require('axios');

module.exports = defineConfig({
  e2e: {
    setupNodeEvents(on, config) {
      // 在整个测试套件运行前执行下载
      on('before:run', async () => {
        // 可定义需要下载的文件列表
        const filesToDownload = [
          { url: 'http://你的文件服务器地址/文件1.zip', fixturePath: 'downloaded/文件1.zip' },
          { url: 'http://你的文件服务器地址/文件2.mp4', fixturePath: 'downloaded/文件2.mp4' }
        ];

        for (const file of filesToDownload) {
          const fullTargetPath = path.join(__dirname, 'cypress/fixtures', file.fixturePath);
          // 自动创建目标目录(如果不存在)
          fs.mkdirSync(path.dirname(fullTargetPath), { recursive: true });

          // 用流方式下载大文件,避免内存占用过高
          const response = await axios({
            url: file.url,
            method: 'GET',
            responseType: 'stream'
          });

          const writer = fs.createWriteStream(fullTargetPath);
          response.data.pipe(writer);

          await new Promise((resolve, reject) => {
            writer.on('finish', resolve);
            writer.on('error', reject);
          });
        }
      });
    },
  },
});

方法3:封装自定义命令复用下载逻辑

将下载逻辑封装成Cypress自定义命令,方便在多个测试用例中灵活调用。

在cypress/support/commands.js中添加:

const path = require('path');

Cypress.Commands.add('downloadToFixture', (fileUrl, fixtureRelativePath) => {
  const fullFixturePath = path.join(Cypress.config('fixturesFolder'), fixtureRelativePath);
  
  // 创建目标目录
  cy.exec(`mkdir -p ${path.dirname(fullFixturePath)}`); // 跨平台也可使用fs模块替代exec

  cy.request({
    url: fileUrl,
    method: 'GET',
    responseType: 'arraybuffer'
  }).then(response => {
    cy.writeFile(fullFixturePath, response.body, 'binary');
  });
});

在测试用例中调用:

// 下载文件到指定Fixture路径
cy.downloadToFixture('http://你的文件服务器地址/测试文件.docx', 'temp/测试文件.docx');

// 使用下载后的Fixture
cy.fixture('temp/测试文件.docx', 'binary').then(file => {
  cy.get('#file-upload').attachFile({
    fileContent: file,
    fileName: '测试文件.docx',
    mimeType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
  });
});

关键注意事项

  • 将下载的文件目录加入.gitignore,比如添加cypress/fixtures/downloaded/和cypress/fixtures/temp/,避免冗余文件提交到Git。
  • 大文件优先使用方法2的流处理方式,防止内存溢出。
  • 可添加文件校验逻辑(比如对比MD5值),确保下载的文件完整可用。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 08:50:36