【问题标题】:Using Cypress, can we do an assert with test results使用赛普拉斯,我们可以用测试结果做一个断言吗
【发布时间】:2021-08-06 10:07:13
【问题描述】:

我想根据测试结果在日志中写一些东西。例如,如果有 0 个测试通过,我们可以在日志中写入一些内容,或者如果有超过 1 个测试被跳过

【问题讨论】:

    标签: javascript automated-tests cypress


    【解决方案1】:

    您可以为此使用柏树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
    • 当然:。 . . . . . . .. module.exports = (on, config) => { on('task', { log(message) { console.log(message) return null }, }) on('file:preprocessor', cucumber()) addMatchImageSnapshotPlugin(on, config) require('cypress-plugin-retries/lib/plugin')(on) on('test:after:run', (results) => { if (results.totalPassed == 0) { 控制台.log('Print something') } else if (results.totalPassed > 0) { console.log('Print something') } }) }
    • 我已经更新了我的答案。请立即尝试。也不要忘记对答案中提到的cypress.json 进行更改。
    • 我已经包含了这个,“experimentalInteractiveRunEvents”:true。我现在会检查你的新答案
    猜你喜欢
    • 2023-02-14
    • 2021-01-02
    • 1970-01-01
    • 1970-01-01
    • 2021-12-22
    • 2021-12-21
    • 1970-01-01
    • 1970-01-01
    • 2019-12-22
    相关资源
    最近更新 更多