Skip to main content
Cypress应用

After Run 事件

after:run 事件在运行结束后触发。当通过 cypress open 运行 Cypress 时,该事件会在关闭项目时触发。

当通过 cypress run 运行时,每次执行 cypress run 都会触发该事件。因此,如果在并行模式下运行测试用例,该事件会在每台调用 cypress run 的机器上各触发一次。

语法

caution

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

caution

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

const { defineConfig } = require('cypress')

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

results (Object)

运行结果对象,包含通过/失败等总数统计、项目配置以及浏览器和系统详情。该对象与 Module API 返回的结果对象相同。

结果对象仅在通过 cypress run 运行时提供。通过 cypress open 运行时,results 将为 undefined。

用法

你可以从 after:run 事件处理函数中返回一个 Promise,Cypress 会等待该 Promise 完成后再继续执行测试用例。

记录运行通过的测试数量

const { defineConfig } = require('cypress')

module.exports = defineConfig({
// setupNodeEvents can be defined in either
// the e2e or component configuration
e2e: {
setupNodeEvents(on, config) {
on('after:run', (results) => {
// 通过 `cypress run` 运行时 results 对象示例:
// {
// totalDuration: 81,
// totalSuites: 0,
// totalTests: 1,
// totalFailed: 0,
// totalPassed: 1,
// totalPending: 0,
// totalSkipped: 0,
// browserName: 'electron',
// browserVersion: '59.0.3071.115',
// osName: 'darwin',
// osVersion: '16.7.0',
// cypressVersion: '3.1.0',
// config: {
// projectId: '1qv3w7',
// baseUrl: 'http://example.com',
// viewportWidth: 1000,
// viewportHeight: 660,
// // ... 更多属性...
// }
// // ... 更多属性...
// }
// }
if (results) {
// 在交互模式下 results 为 undefined
console.log(
results.totalPassed,
'out of',
results.totalTests,
'passed'
)
}
})
},
},
})

相关链接