【发布时间】:2021-08-06 10:07:13
【问题描述】:
我想根据测试结果在日志中写一些东西。例如,如果有 0 个测试通过,我们可以在日志中写入一些内容,或者如果有超过 1 个测试被跳过
【问题讨论】:
标签: javascript automated-tests cypress
我想根据测试结果在日志中写一些东西。例如,如果有 0 个测试通过,我们可以在日志中写入一些内容,或者如果有超过 1 个测试被跳过
【问题讨论】:
标签: javascript automated-tests cypress
您可以为此使用柏树after run api。由于这仍然是一项实验性功能,您必须在 cypress.json
experimentalInteractiveRunEvents: true
然后在你的cypress/plugins/index.js 文件中你可以写:
module.exports = (on, config) => {
on('after:run', (results) => {
if (results.totalPassed == 0) {
console.log('Print something')
} else if (results.totalPassed > 0) {
console.log('Print something')
}
})
}
你应该cypress/plugins/index.js 看起来像:
//For cucumber integration
const cucumber = require('cypress-cucumber-preprocessor').default
//For Image Snapshot Plugin
const {
addMatchImageSnapshotPlugin,
} = require('cypress-image-snapshot/plugin');
//For cypress retry plugin
require('cypress-plugin-retries')
module.exports = (on, config) => {
on('file:preprocessor', cucumber()); //For cypress cucumber preprocessor
addMatchImageSnapshotPlugin(on); //For Image Snapshot Plugin
require('cypress-plugin-retries/lib/plugin')(on); //For cypress retry plugin
on('after:run', (results) => { //after run event
if (results.totalPassed == 0) {
console.log('Print something')
} else if (results.totalPassed > 0) {
console.log('Print something')
}
});
}
要记住的一点是,此代码是插件文件的一部分,因此在 Node 环境中执行,因此您不能在此文件中调用 Cypress 或 cy 命令。
这些是结果对象的其他属性,您也可以使用:
// results will look something like this when run via `cypress run`:
// {
// 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,
// // ... more properties...
// }
// // ... more properties...
// }
【讨论】:
npx cypress run --spec cypress/integration/*.js --env tags=uidfdffd The following validation error was thrown by your plugins file (C:\Repos\visual-regression-template\cypress\plugins\index.js). Error: You must pass a valid event name when registering a plugin. You passed: after:run` 以下是有效事件: - 文件:预处理器 - 之前:浏览器:启动 - 任务 - 之后:截图`
cypress/plugins/index.js 文件吗?我更新了一项更改,您可以尝试将after:run 更改为test:after:run。
cypress.json 进行更改。