JSON report not generating for failed scenarios using protractor

后端 未结 1 2006
谎友^
谎友^ 2021-01-28 15:07

If my scenarios got failed the JSON report not generating. But for passes scenarios I can able to see the JSON report.

Please find my config file as below.

相关标签:
1条回答
  • 2021-01-28 16:06

    You can save the report by using a hook, so don't generate the file form the protractor.conf.js file, but use a cucumber-hook for it.

    The hook can look like this

    reportHook.js:

    const cucumber = require('cucumber');
    const jsonFormatter = cucumber.Listener.JsonFormatter();
    const fs = require('fs-extra');
    const jsonFile = require('jsonfile');
    const path = require('path');
    const projectRoot = process.cwd();
    
    module.exports = function reportHook() {
      this.registerListener(jsonFormatter);
    
      /**
       * Generate and save the report json files
       */
      jsonFormatter.log = function(report) {
        const jsonReport = JSON.parse(report);
    
        // Generate a featurename without spaces, we're gonna use it later
        const featureName = jsonReport[0].name.replace(/\s+/g, '_').replace(/\W/g, '').toLowerCase();
    
        // Here I defined a base path to which the jsons are written to
        const snapshotPath = path.join(projectRoot, '.tmp/json-output');
    
        // Think about a name for the json file. I now added a featurename (each feature
        // will output a file) and a timestamp (if you use multiple browsers each browser 
        // execute each feature file and generate a report)
        const filePath = path.join(snapshotPath, `report.${featureName}.${new Date}.json`);
    
        // Create the path if it doesn't exists
        fs.ensureDirSync(snapshotPath);
    
        // Save the json file
        jsonFile.writeFileSync(filePath, jsonReport, {
          spaces: 2
        });
      };
    }

    You can save this code to the file reportHook.js and then add it to the cucumberOpts:.require so it will look like this in your code

    cucumberOpts: {
      require: [
        '../step_definitions/*.json',
        '../setup/hooks.js',
        '../setup/reportHook.js'
      ],
      ....
    }

    Even with failed steps / scenario's it should generate the report file.

    Hope it helps

    0 讨论(0)
提交回复
热议问题