如何使用 Node JS 创建 Selenium Cucumber html 报告
How to create Selenium Cucumber html reports with Node JS
我想创建 Cucumber html 报告,我是 Node JS 的新手,我尝试搜索它并使用了以下
this.registerHandler('AfterFeatures', function(callback) {
try {
var options = {
theme: "bootstrap",
jsonFile: "/report/cucumber.json",
output: "/report/cucumber_report.html",
reportSuiteAsScenarios: true,
launchReport: true,
metadata: {
"App Version": "0.0.1"
}
};
reporter.generate(options);
} catch (e) {
console.log(e);
}
callback();
});
但是当我 运行 我的代码时,黄瓜功能场景被执行,它最终给我一个错误说明,
Unable to parse cucumberjs output into json: '/report/cucumber.json' { Error: /report/cucumber.json: ENOENT: no such file or directory, open '/report/cucumber.json'
at Object.fs.openSync (fs.js:652:18)
at Object.fs.readFileSync (fs.js:553:33)
at Object.readFileSync (/Users/sarav/Documents/GitHub/automationtests/node_modules/jsonfile/index.js:67:22)
at isValidJsonFile (/Users/sarav/Documents/GitHub/automationtests/node_modules/cucumber-html-reporter/lib/reporter.js:404:48)
at Object.generate (/Users/sarav/Documents/GitHub/automationtests/node_modules/cucumber-html-reporter/lib/reporter.js:426:9)
at Object.generateReport [as generate] (/Users/sarav/Documents/GitHub/automationtests/node_modules/cucumber-html-reporter/index.js:30:21)
at /Users/sarav/Documents/GitHub/automationtests/features/support/hooks.js:49:22
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
errno: -2,
code: 'ENOENT',
syscall: 'open',
path: '/report/cucumber.json' }
上面的代码是自动生成.json和.html文件还是我们需要手动创建.json文件并将其转换成html报告.
我在 Java 上工作,它会在执行结束时自动创建 json 和 html 报告。
因为这是非常新的,所以我无法弄清楚缺少的部分是什么
谢谢
您生成 HTML 报告的代码需要 json 文件:/report/cucumber.json 已经存在。
所以在测试时需要其他代码帮助生成json文件运行,我把我项目中用到的代码给大家参考。
注意:以下代码只能在 Cucumber 1 上运行,不能在 Cucumver 2 上运行,以下是我使用的版本:
"dependencies": {
"cucumber": "1.2.1",
"cucumber-html-reporter": "0.2.6",
1) cucumber-json-report.js 在 运行.[=15= 期间生成 Cucumber JSON 报告]
var fs = require('fs-extra');
var path = require('path');
var moment = require('moment');
var Cucumber = require('cucumber');
module.exports = function() {
var JsonFormatter = Cucumber.Listener.JsonFormatter();
JsonFormatter.log = function(string) {
var outputDir = './reports';
var targetJson = outputDir + '/cucumber_report.json';
if (fs.existsSync(outputDir)) {
fs.moveSync(outputDir, outputDir + '_' + moment().format('YYYYMMDD_HHmmss') + "_" + Math.floor(Math.random() * 10000), {
overwrite: true
});
}
fs.mkdirSync(outputDir);
fs.writeFileSync(targetJson, string);
};
this.registerListener(JsonFormatter);
};
2) screenshot.js失败时截图
module.exports = function() {
this.After(function(scenario, callback) {
if (scenario.isFailed()) {
browser.takeScreenshot().then(function(buffer) {
var decodedImage = new Buffer(buffer, 'base64');
scenario.attach(decodedImage, 'image/png');
callback();
}, function(err) {
callback(err);
});
} else {
callback();
}
});
};
3) cucumber-html-report.js 在所有功能 运行 结束后生成 Cucumber HTML 报告。
var reporter = require('cucumber-html-reporter');
module.exports = function() {
this.AfterFeatures(function(features, callback) {
var options = {
theme: 'bootstrap',
jsonFile: 'reports/cucumber_report.json',
output: 'reports/cucumber_report.html',
reportSuiteAsScenarios: true
};
reporter.generate(options);
callback();
});
};
4) Protractor conf.js 将以上三个文件包含在 cucumberOpts.require
cucumberOpts: {
monochrome: true,
strict: true,
plugin: ["pretty"],
require:[
'./step_definitions/*step.js',
'./support/screenshot.js',
'./support/cucumber-json-report.js',
'./support/cucumber-html-report.js'
],
tags: '',
},
我想创建 Cucumber html 报告,我是 Node JS 的新手,我尝试搜索它并使用了以下
this.registerHandler('AfterFeatures', function(callback) {
try {
var options = {
theme: "bootstrap",
jsonFile: "/report/cucumber.json",
output: "/report/cucumber_report.html",
reportSuiteAsScenarios: true,
launchReport: true,
metadata: {
"App Version": "0.0.1"
}
};
reporter.generate(options);
} catch (e) {
console.log(e);
}
callback();
});
但是当我 运行 我的代码时,黄瓜功能场景被执行,它最终给我一个错误说明,
Unable to parse cucumberjs output into json: '/report/cucumber.json' { Error: /report/cucumber.json: ENOENT: no such file or directory, open '/report/cucumber.json'
at Object.fs.openSync (fs.js:652:18)
at Object.fs.readFileSync (fs.js:553:33)
at Object.readFileSync (/Users/sarav/Documents/GitHub/automationtests/node_modules/jsonfile/index.js:67:22)
at isValidJsonFile (/Users/sarav/Documents/GitHub/automationtests/node_modules/cucumber-html-reporter/lib/reporter.js:404:48)
at Object.generate (/Users/sarav/Documents/GitHub/automationtests/node_modules/cucumber-html-reporter/lib/reporter.js:426:9)
at Object.generateReport [as generate] (/Users/sarav/Documents/GitHub/automationtests/node_modules/cucumber-html-reporter/index.js:30:21)
at /Users/sarav/Documents/GitHub/automationtests/features/support/hooks.js:49:22
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
errno: -2,
code: 'ENOENT',
syscall: 'open',
path: '/report/cucumber.json' }
上面的代码是自动生成.json和.html文件还是我们需要手动创建.json文件并将其转换成html报告.
我在 Java 上工作,它会在执行结束时自动创建 json 和 html 报告。
因为这是非常新的,所以我无法弄清楚缺少的部分是什么
谢谢
您生成 HTML 报告的代码需要 json 文件:/report/cucumber.json 已经存在。
所以在测试时需要其他代码帮助生成json文件运行,我把我项目中用到的代码给大家参考。
注意:以下代码只能在 Cucumber 1 上运行,不能在 Cucumver 2 上运行,以下是我使用的版本:
"dependencies": {
"cucumber": "1.2.1",
"cucumber-html-reporter": "0.2.6",
1) cucumber-json-report.js 在 运行.[=15= 期间生成 Cucumber JSON 报告]
var fs = require('fs-extra');
var path = require('path');
var moment = require('moment');
var Cucumber = require('cucumber');
module.exports = function() {
var JsonFormatter = Cucumber.Listener.JsonFormatter();
JsonFormatter.log = function(string) {
var outputDir = './reports';
var targetJson = outputDir + '/cucumber_report.json';
if (fs.existsSync(outputDir)) {
fs.moveSync(outputDir, outputDir + '_' + moment().format('YYYYMMDD_HHmmss') + "_" + Math.floor(Math.random() * 10000), {
overwrite: true
});
}
fs.mkdirSync(outputDir);
fs.writeFileSync(targetJson, string);
};
this.registerListener(JsonFormatter);
};
2) screenshot.js失败时截图
module.exports = function() {
this.After(function(scenario, callback) {
if (scenario.isFailed()) {
browser.takeScreenshot().then(function(buffer) {
var decodedImage = new Buffer(buffer, 'base64');
scenario.attach(decodedImage, 'image/png');
callback();
}, function(err) {
callback(err);
});
} else {
callback();
}
});
};
3) cucumber-html-report.js 在所有功能 运行 结束后生成 Cucumber HTML 报告。
var reporter = require('cucumber-html-reporter');
module.exports = function() {
this.AfterFeatures(function(features, callback) {
var options = {
theme: 'bootstrap',
jsonFile: 'reports/cucumber_report.json',
output: 'reports/cucumber_report.html',
reportSuiteAsScenarios: true
};
reporter.generate(options);
callback();
});
};
4) Protractor conf.js 将以上三个文件包含在 cucumberOpts.require
cucumberOpts: {
monochrome: true,
strict: true,
plugin: ["pretty"],
require:[
'./step_definitions/*step.js',
'./support/screenshot.js',
'./support/cucumber-json-report.js',
'./support/cucumber-html-report.js'
],
tags: '',
},