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

Cypress集成Allure HTML报告请求响应内容截断问题求助

解决Cypress+Allure报告中请求/响应体被截断的问题

你在Cypress 13.1.0与allure-cypress v3.0.4集成后,生成的HTML报告里每个测试步骤的请求、响应体被截断至132字符,尝试用cy.log添加附件未成功,以下是几个可行的解决办法:

方法1:配置Allure禁用日志截断

Allure默认会截断过长的日志内容,可在cypress.config.js中调整Allure的日志配置,设置足够大的截断长度或直接禁用截断:

修改cypress.config.js中的allureCypress配置:

const { defineConfig } = require('cypress');
const { allureCypress } = require('allure-cypress/reporter');
const allureWriter = require('@shelex/cypress-allure-plugin/writer');

module.exports = defineConfig({
  reporterOptions: {
    charts: true,
    reportPageTitle: 'Test API',
    embeddedScreenshots: false,
    saveAllAttempts: false,
  },
  e2e: {
    experimentalRunAllSpecs: true,
    specPattern: 'cypress/api-specs/**/*.spec.{js,jsx,ts,tsx}',
    setupNodeEvents(on, config) {
      allureWriter(on, config);
      allureCypress(on, config, {
        resultsDir: 'allure-results',
        // 添加日志配置,0表示不截断,也可设置具体长度如10000
        logger: {
          loggers: [{
            type: 'console',
            options: { truncate: 0 }
          }]
        }
      });
      require('@cypress/grep/src/plugin')(config);
      return config;
    },
  },
  trashAssetsBeforeRuns: true,
  headless: true,
  env: {
    grepFilterSpec: true,
    grepOmitFiltered: true,
    allureReuseAfterSpec: true,
  },
});

方法2:使用Allure API手动添加请求/响应为附件

利用allure-cypress提供的API,将请求体、响应体作为文本附件添加到测试步骤中,报告中点击附件即可查看完整内容:

修改你的sendApiRequest函数:

const sendApiRequest = ({
  method,
  url,
  headers = {},
  body = null,
  queryParams = {},
}) => {
  // 记录请求步骤
  cy.allure().step(`发送API请求: ${method} ${url}`);
  
  // 添加请求相关附件
  if (body) {
    cy.allure().attachment(
      '请求体',
      JSON.stringify(body, null, 2),
      'application/json'
    );
  }
  cy.allure().attachment(
    '请求头',
    JSON.stringify(headers, null, 2),
    'application/json'
  );

  return cy
    .request({
      method: method,
      url: url,
      headers: headers,
      body: body,
      qs: queryParams,
      failOnStatusCode: false,
    })
    .then(response => {
      // 添加响应相关附件
      cy.allure().attachment(
        '响应体',
        JSON.stringify(response.body, null, 2),
        'application/json'
      );
      cy.allure().step(`响应状态码: ${response.status}`);
      cy.allure().attachment(
        '响应头',
        JSON.stringify(response.headers, null, 2),
        'application/json'
      );

      if (response.status < 200 || response.status >= 300) {
        cy.allure().step(`请求失败,状态码: ${response.status}`);
        throw new Error(`API请求失败,状态码: ${response.status}`);
      }
      return cy.wrap(response);
    });
};

方法3:自定义Cypress请求日志

通过cy.request的log选项自定义日志内容,确保完整的请求/响应信息被捕获:

修改cy.request配置:

return cy
  .request({
    method: method,
    url: url,
    headers: headers,
    body: body,
    qs: queryParams,
    failOnStatusCode: false,
    // 自定义日志,显示完整请求参数
    log: {
      message: `${method} ${url}`,
      consoleProps: () => ({
        Method: method,
        URL: url,
        Headers: headers,
        Request Body: body,
        Query Params: queryParams
      })
    }
  })

配合方法1的Allure配置,可让自定义日志完整显示在报告中。

注意事项

  • 确保使用的allure-cypress版本为v3.0.4及以上,部分API在旧版本中不支持
  • 修改配置后,重新执行测试并生成报告:allure generate allure-results --clean && allure open

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 11:33:18