无法使用量角器为失败的场景生成JSON报告 [英] JSON report not generating for failed scenarios using protractor
问题描述
如果我的方案失败,则不会生成JSON报告.但是对于通行证场景,我可以看到JSON报告.
If my scenarios got failed the JSON report not generating. But for passes scenarios I can able to see the JSON report.
请在下面找到我的配置文件.
Please find my config file as below.
在注释提示控制台中,我可以看到失败消息:
In comment prompt console I can able to see the failure message:
W/launcher-忽略未捕获的错误AssertionError:期望的false等于true
W/launcher - Ignoring uncaught error AssertionError: expected false to equal true
E/发射器-错误:发射器退出,剩余1个任务
E/launcher - BUG: launcher exited with 1 tasks remaining
推荐答案
您可以使用钩子保存报告,因此不要从protractor.conf.js
文件生成文件,而要使用黄瓜钩子.
You can save the report by using a hook, so don't generate the file form the protractor.conf.js
file, but use a cucumber-hook for it.
挂钩看起来像这样
reportHook.js:
const cucumber = require('cucumber');
const jsonFormatter = cucumber.Listener.JsonFormatter();
const fs = require('fs-extra');
const jsonFile = require('jsonfile');
const path = require('path');
const projectRoot = process.cwd();
module.exports = function reportHook() {
this.registerListener(jsonFormatter);
/**
* Generate and save the report json files
*/
jsonFormatter.log = function(report) {
const jsonReport = JSON.parse(report);
// Generate a featurename without spaces, we're gonna use it later
const featureName = jsonReport[0].name.replace(/\s+/g, '_').replace(/\W/g, '').toLowerCase();
// Here I defined a base path to which the jsons are written to
const snapshotPath = path.join(projectRoot, '.tmp/json-output');
// Think about a name for the json file. I now added a featurename (each feature
// will output a file) and a timestamp (if you use multiple browsers each browser
// execute each feature file and generate a report)
const filePath = path.join(snapshotPath, `report.${featureName}.${new Date}.json`);
// Create the path if it doesn't exists
fs.ensureDirSync(snapshotPath);
// Save the json file
jsonFile.writeFileSync(filePath, jsonReport, {
spaces: 2
});
};
}
您可以将此代码保存到文件reportHook.js
中,然后将其添加到cucumberOpts:.require
中,这样在您的代码中将看起来像这样
You can save this code to the file reportHook.js
and then add it to the cucumberOpts:.require
so it will look like this in your code
cucumberOpts: {
require: [
'../step_definitions/*.json',
'../setup/hooks.js',
'../setup/reportHook.js'
],
....
}
即使步骤/方案失败,它也应生成报告文件.
Even with failed steps / scenario's it should generate the report file.
希望有帮助
这篇关于无法使用量角器为失败的场景生成JSON报告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!