我使用Nightwatch-Cucumber
自动化我的end2end测试,并希望创建一个黄瓜html报告后测试执行与cucumber-html-reporter
,但我得到一个错误,而报告生成时cucumber-html-reporter
:使用黄瓜-html记者与Nightwatch黄瓜意外结束
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"
}
]
通过詹金斯产生黄瓜HTML报告与Cucumber Reports Plugin
运行成功。
那么,我该如何解决我的问题以及哪个框架(Nightwatch-Cucumber
或cucumber-html-reporter
)才是原因呢?什么是我生成的cucumber.json
的无效部分?
您发布的JSON是有效的。检查文件以确保没有额外的东西,比如BOM字符。 – Barmar
这真的是问题的原因。我在一个编辑器中打开并保存了'cucumber.json',并将其保存为一个没有BOM的UTF-8文件,现在它运行。所以问题应该是'Nightwatch-Cucumber',它创建并写入这个文件。我如何确保在JavaScript中删除这些BOM?有没有Node.js库来做到这一点? – Martin