Skip to main content
Cypress应用

After Spec 事件

after:spec 事件会在运行完一个 spec 文件后触发。当通过 cypress open 运行 Cypress 时,该事件会在浏览器关闭时触发。

语法

caution

⚠️ 这段代码属于 setupNodeEvents 函数的一部分, 因此会在 Node 环境中执行。你不能在此函数中调用 Cypresscy 命令, 但可以直接访问文件系统和操作系统的其他部分。

caution

⚠️ 通过 cypress open 运行时,只有在启用了 experimentalInteractiveRunEvents 标志 时才会触发 after:spec 事件。

const { defineConfig } = require('cypress')

module.exports = defineConfig({
// setupNodeEvents can be defined in either
// the e2e or component configuration
e2e: {
setupNodeEvents(on, config) {
on('after:spec', (spec, results) => {
/* ... */
})
},
},
})

spec (对象)

spec 文件的详细信息,包含以下属性:

属性描述
namespec 文件的基本名称(例如 login.cy.js
relativespec 文件相对于项目根目录的路径(例如 cypress/e2e/login.cy.js
absolutespec 文件的绝对路径(例如 /Users/janelane/my-app/cypress/e2e/login.cy.js

results (对象)

spec 文件结果的详细信息,包括通过/失败等的数量以及测试本身的详细信息。

只有在通过 cypress run 运行时才会提供结果。通过 cypress open 运行时,结果将为 undefined。

用法

你可以从 after:spec 事件处理程序中返回一个 promise,Cypress 会等待该 promise 完成后再继续处理 spec 的视频或继续运行其他 spec(如果有的话)。

在 spec 运行完成后将相对路径记录到 stdout

const { defineConfig } = require('cypress')

module.exports = defineConfig({
// setupNodeEvents can be defined in either
// the e2e or component configuration
e2e: {
setupNodeEvents(on, config) {
on('after:spec', (spec, results) => {
// spec 对象类似这样:
// {
// name: 'login.cy.js',
// relative: 'cypress/e2e/login.cy.js',
// absolute: '/Users/janelane/my-app/cypress/e2e/login.cy.js',
// }
// results 对象类似这样:
// {
// stats: {
// suites: 0,
// tests: 1,
// passes: 1,
// pending: 0,
// skipped: 0,
// failures: 0,
// // ...更多属性
// }
// reporter: 'spec',
// tests: [
// {
// title: ['login', 'logs user in'],
// state: 'passed',
// // ...更多属性...
// }
// ],
// error: null,
// video: '/Users/janelane/my-app/cypress/videos/login.cy.js.mp4',
// screenshots: [],
// // ...更多属性...
// }
console.log('Finished running', spec.relative)
})
},
},
})

示例

如果 spec 通过则删除录制的视频

当满足某些条件时,你可以删除 spec 的录制视频。这将跳过视频的压缩和上传到 Cypress Cloud 的过程。

下面的示例展示了如何删除没有失败测试的 spec 的录制视频。

const { defineConfig } = require('cypress')
const fs = require('fs')

module.exports = defineConfig({
// setupNodeEvents can be defined in either
// the e2e or component configuration
e2e: {
setupNodeEvents(on, config) {
on('after:spec', (spec, results) => {
// 是否有失败?
if (results && results.video && results.stats.failures === 0) {
// 如果 spec 通过则删除视频
fs.unlinkSync(results.video)
}
})
},
},
})

如果没有测试重试则删除录制的视频

当满足某些条件时,你可以删除 spec 的录制视频。这将跳过视频的压缩和上传到 Cypress Cloud 的过程。

下面的示例展示了如何在使用 Cypress 测试重试 时删除没有重试尝试的 spec 的录制视频。

const { defineConfig } = require('cypress')
const fs = require('fs')

module.exports = defineConfig({
// setupNodeEvents can be defined in either
// the e2e or component configuration
e2e: {
setupNodeEvents(on, config) {
on('after:spec', (spec, results) => {
if (results && results.video) {
// 是否有任何重试尝试失败?
const failures = results.tests.some((test) =>
test.attempts.some((attempt) => attempt.state === 'failed')
)
if (!failures) {
// 如果 spec 通过且没有测试重试则删除视频
fs.unlinkSync(results.video)
}
}
})
},
},
})

另请参阅