我使用Nightwatch-Cucumber
来自动执行end2end测试,并希望在使用cucumber-html-reporter
执行测试后创建黄瓜html报告,但在cucumber-html-reporter
生成报告时出错:
Unable to parse cucumberjs output into json: 'reports/cucumber.json' SyntaxError: reports/cucumber.json: Unexpected end of JSON input
at JSON.parse (<anonymous>)
at Object.readFileSync (/Users/GRme/projects/e2e-web-tests/node_modules/jsonfile/index.js:69:17)
我不知道为什么生成的cucumber.json
无效。
我使用以下版本:
"cucumber-html-reporter": "^2.0.0",
"nightwatch": "^0.9.16",
"nightwatch-cucumber": "^7.1.10",
这是nightwatch.conf.js
中的配置:
require('nightwatch-cucumber')({
cucumberArgs: [
'--tags', '@run',
'--require', 'timeout.js',
'--require', 'hooks.js',
'--require', 'features/step_definitions',
'--format', 'pretty',
'--format', 'json:reports/cucumber.json',
'features']
});
这是hooks.js
,执行我的黄瓜html报告生成:
const {client} = require('nightwatch-cucumber');
const {defineSupportCode} = require('cucumber');
var reporter = require('cucumber-html-reporter');
var options = {
theme: 'bootstrap',
jsonFile: 'reports/cucumber.json',
output: 'reports/cucumber_report.html',
reportSuiteAsScenarios: true,
launchReport: false,
//ignoreBadJsonFile: true,
name: 'NIKITA end2end tests',
brandTitle: 'NIKITA end2end tests',
storeScreenShots: true,
metadata: {
// "App Version": "0.0.1",
// "Test Environment": "AAT",
// "Browser": "Chrome XXX",
// "Platform": "Mac OS X",
}
};
defineSupportCode(({Before, After}) => {
Before(function() {
client.maximizeWindow();
});
After(function() {
reporter.generate(options);
});
});
我生成且显然无效的cucumber.json看起来像这样:
[
{
"keyword": "Feature",
"line": 1,
"name": "only a test feature",
"tags": [],
"uri": "/Users/GRme/projects/e2e-web-tests/features/testFeature.feature",
"elements": [
{
"keyword": "Scenario",
"line": 4,
"name": "only a test Scenario",
"tags": [
{
"line": 3,
"name": "@run"
}
],
"id": "only-a-test-feature;only-a-test-scenario",
"steps": [
{
"arguments": [],
"keyword": "Before",
"result": {
"status": "passed",
"duration": 1
},
"hidden": true,
"match": {
"location": "/Users/GRme/projects/e2e-web-tests/hooks.js:24"
}
},
{
"arguments": [],
"keyword": "When ",
"name": "\"1\" seconds waiting",
"result": {
"status": "passed",
"duration": 2615
},
"line": 5,
"match": {
"location": "/Users/GRme/projects/e2e-web-tests/features/step_definitions/abstractStepDefinition.js:10"
}
},
{
"arguments": [],
"keyword": "After",
"result": {
"status": "passed",
"duration": 4
},
"hidden": true,
"match": {
"location": "/Users/GRme/projects/e2e-web-tests/hooks.js:28"
}
}
]
}
],
"id": "only-a-test-feature"
}
]
通过Jenkins生成Cucumber Html报告,Cucumber Reports Plugin
成功运行。
那么,我如何解决我的问题以及哪个框架(Nightwatch-Cucumber
或cucumber-html-reporter
)是什么原因?我生成的cucumber.json
的无效部分是什么?
答案 0 :(得分:0)
尝试在单独的测试执行步骤中生成黄瓜html报告。例如,如果您使用的是npm脚本,请使用单独的npm脚本来生成报告。您的方法的问题是,在Cucumber.js全局After
挂钩中,尚未创建报告。这就是你得到这样一个错误的原因。我建议使用一个单独的node.js脚本来运行cucumber-html-reporter。
答案 1 :(得分:0)
我使用Event Handler
解决了这个问题。我将Cucumber报告生成放在事件AfterFeatures
的事件处理程序函数中,它对我来说非常适合。
您可以找到更多信息here。