将 cucumber-html-reporter 与 Nightwatch-Cucumber 一起使用时 JSON 输入意外结束
Unexpected end of JSON input when using cucumber-html-reporter with Nightwatch-Cucumber
我使用 Nightwatch-Cucumber
来自动化我的端到端测试,并希望在使用 cucumber-html-reporter
执行测试后创建一个黄瓜 html 报告,但是我在使用 [=] 生成报告时出错18=]:
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
,我的 cucumber 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
的无效部分是什么?
尝试在单独的测试执行步骤中生成 Cucumber html 报告。例如,如果您使用 npm 脚本,请使用单独的 npm 脚本来生成报告。您的方法的问题是在 Cucumber.js 全局 After
挂钩中尚未创建报告。这就是为什么你会收到这样的错误。我建议使用单独的 node.js 脚本来 运行 cucumber-html-reporter.
我使用 Event Handler
解决了这个问题。我将 Cucumber 报告生成放在事件的此类事件处理程序函数中 AfterFeatures
,它对我来说非常完美。
您可以找到更多信息 here。
我使用 Nightwatch-Cucumber
来自动化我的端到端测试,并希望在使用 cucumber-html-reporter
执行测试后创建一个黄瓜 html 报告,但是我在使用 [=] 生成报告时出错18=]:
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
,我的 cucumber 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
的无效部分是什么?
尝试在单独的测试执行步骤中生成 Cucumber html 报告。例如,如果您使用 npm 脚本,请使用单独的 npm 脚本来生成报告。您的方法的问题是在 Cucumber.js 全局 After
挂钩中尚未创建报告。这就是为什么你会收到这样的错误。我建议使用单独的 node.js 脚本来 运行 cucumber-html-reporter.
我使用 Event Handler
解决了这个问题。我将 Cucumber 报告生成放在事件的此类事件处理程序函数中 AfterFeatures
,它对我来说非常完美。
您可以找到更多信息 here。