量角器黄瓜HTML报告仅在第二次运行后生成?

时间:2019-01-03 17:56:38

标签: html protractor cucumber report

当我尝试运行我的代码时,仅在第二次运行后才生成html报告。 在第一次运行中,它会生成json文件,然后在第二次运行后,使用生成的json文件并创建HTML报告

请告诉我如何仅运行一次即可生成html报告。

下面是我尝试过的代码

hook.js

const {defineSupportCode} = require('cucumber');

defineSupportCode(function ({After}) {

   After(function(scenario,done)
{
const world = this;
console.log('in after block')
if (scenario.result.status === 'failed') {
console.log('in after block inside')

    browser.takeScreenshot().then(function (stream) {
        let decodedImage = new Buffer(stream.replace(/^data:image\/(png|gif|jpeg);base64,/, ''), 'base64');
        world.attach(decodedImage, 'image/png');
        console.log('screenshot successful');
    }).then(function () {
        done();
    });
}else {
    done();
}
});


       });

index.js

var reporter = require('cucumber-html-reporter');

var options = {
        theme: 'bootstrap',

    output: 'cucumber-report.html',
    reportSuiteAsScenarios: true,
    launchReport: true,
    screenshotsDirectory: 'screenshots123',
    takeScreenShotsOnlyForFailedSpecs: true,
     //screenshotsSubfolder: 'images',
    storeScreenshots: true,

};

reporter.generate(options);

Index.js

var reporter = require('cucumber-html-reporter');

var options = {
    theme: 'bootstrap',
    jsonFile: 'C:/Users/pc/ProtractorCucumber/htmlReport/cucumber_html_reporter/report.json',
  //  jsonFile: 'C:/Users/pc/ProtractorCucumber/htmlReport/cucumber_html_reporter/cucumber-report.json',
    output: 'C:/Users/pc/ProtractorCucumber/htmlReport/cucumber_html_reporter/cucumber-report.html',
   // output: 'report123.html',
    reportSuiteAsScenarios: true,
    launchReport: true,
    screenshotsDirectory: 'screenshots123',
    takeScreenShotsOnlyForFailedSpecs: true,
     //screenshotsSubfolder: 'images',
    storeScreenshots: true,

};

reporter.generate(options);

1 个答案:

答案 0 :(得分:0)

执行后,Cucumber-html-reporter将需要由黄瓜创建的JSON文件。

在调用 cucumber-html-report generate函数之前,请参考以下处理了异常的代码段。

const Cucumber = require('cucumber');
import { browser } from 'protractor';
import * as fs from 'fs';
import { config } from '../config/config';
import { defineSupportCode } from "cucumber";
import * as reporter from 'cucumber-html-reporter';
import { mkdirp } from 'mkdirp';

defineSupportCode(function ({ registerHandler, registerListener, After, setDefaultTimeout }) {
    setDefaultTimeout(10 * 1000);
    let jsonReports = process.cwd() + "/reports/json";
    let htmlReports = process.cwd() + "/reports/html";
    let targetJson = jsonReports + "/cucumber_report.json";


//BeforeFeature
    registerHandler('BeforeFeature', function (event, callback) {
        browser.get(config.baseUrl);
        callback();
    });

    After(function (scenario) {
        let world = this;
        if (scenario.isFailed()) {
            return browser.takeScreenshot().then(function (screenShot) {
                // screenShot is a base-64 encoded PNG
                world.attach(screenShot, 'image/png');

            });

        }

    })

    let cucumberReporterOptions = {

        theme: "bootstrap",
        //theme: "foundation",
      //  theme: "simple",
        jsonFile: targetJson,
        output: htmlReports + "/cucumber_reporter.html",
        reportSuiteAsScenarios: true,
        launchReport: false

    };

    let logFn = string => {
        if (!fs.existsSync(jsonReports)) {
            mkdirp.sync(jsonReports);
        }
        try {
            fs.writeFileSync(targetJson, string);
            reporter.generate(cucumberReporterOptions); // invoke cucumber-html-reporter
        } catch (err) {
            if (err) {
                console.log(`Failed to save cucumber test results to json file. 
                             Failed to create html report.
                             `);
                console.log(err);
            }
        }
    };
    let jsonformatter = new Cucumber.JsonFormatter({
        log: logFn
    });
    registerListener(jsonformatter);
})