cypress验证失败时,录制视频

122 阅读1分钟

目的

当cypress验证失败时,保留录制的视频,这样就可以通过录制的视频,直观的知道出错时的具体界面效果了

实现方式

cypress.config.ts

import { defineConfig } from 'cypress'
import fs from 'fs'

export default defineConfig({
  // 默认情况下cypress不对录制的视频进行压缩(优点是速度快,缺点是视频文件大),如果需要对录制的视频进行压缩,则将该属性设置为true
  videoCompression: false,
  // 启用操作录屏(错误时,可以通过录屏,看到底是哪里有问题)
  video: true,
  e2e: {
    setupNodeEvents(on, config) {
      // implement node event listeners here
      on(
        'after:spec',
        (spec: Cypress.Spec, results: CypressCommandLine.RunResult) => {
          if (results && results.video) {
            // 只有在出错时,才保留录制的视频
            // Do we have failures for any retry attempts?
            const failures = results.tests.some((test) =>
              test.attempts.some((attempt) => attempt.state === 'failed')
            )
            if (!failures) {
              // delete the video if the spec passed and no tests retried
              fs.unlinkSync(results.video)
            }
          }
        }
      )
    },
  },

  component: {
    devServer: {
      framework: 'vue',
      bundler: 'vite',
    },
  },
})