Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 115 additions & 0 deletions lib/collect-feature-files.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/**
* Get data from the feature files
* @param {featuresFolder} string with the features that are going to be parsed
* @param {jsonJoined} object with all the scenarios
* @return {array} array with the list of jsons
* @private
*/
const getFeatures = (featureFiles) => {
var Gherkin = require('gherkin');
var parser = new Gherkin.Parser();
var fs = require("fs");
var fileList = getFeatureFiles(featureFiles,fileList);
var featureList = fileList.map(file => {
var text = fs.readFileSync(file,{ encoding: 'utf8' });
var gherkinDocument = parser.parse(text);
return gherkinDocument;
});

featureList = addIdToFeatureScenarios(featureList);
featureList = addExamplesTablesToScenarioVariable(featureList);

return featureList;
}

/**
* Get the list of all feature files in an array object
* @param {string} dir the folder that is going to be parsed
* @param {array} fileList that will hold the list of all the feature objects
* @return {array} fileList with the list of all the feature objects
* @private
*/
const getFeatureFiles = (dir, fileList) =>{
var fs = require("fs");
fileList = fileList || [];

var files = fs.readdirSync(dir);
for(var i in files){
if (!files.hasOwnProperty(i)) continue;
var name = dir+'/'+files[i];
if (fs.statSync(name).isDirectory()){
getFeatureFiles(name, fileList);
} else if(name.endsWith(".feature")){
fileList.push(name);
}
}
return fileList;
}

/**
* Generate the id that is generated in the json file
* @param {string}featureName: name of the feature
* @param {string} scenarioName: name of the scenario
* @return {string} id value
* @private
*/
const generateJsonIdFromFeatureNameAndFileName = (featureName,scenarioName) => {
var featureName = featureName.replace(/\s/g, '-').replace(/_/g,'-').toLowerCase();
var scenarioName = scenarioName.replace(/\s/g, '-').replace(/_/g,'-').toLowerCase();
var id = featureName + ";" + scenarioName;
return id;
}

/**
* Add the field id to all the scenarios of the object generated from parsing the feature files folder
* @param {object} featureList: object with the list of features
* @return {object} featureList: object with the list of features
* @private
*/
const addIdToFeatureScenarios = (featureList) => {
featureList.forEach(feature => {
var featureName = feature.feature.name;
feature.feature.children.forEach(scenario => {
var scenarioName = scenario.name;
scenario.id = generateJsonIdFromFeatureNameAndFileName(featureName, scenarioName);
});
});

return featureList;
}

/**
* Add the examples table to each scenario online
* @param {object} featureList: object with the list of features
* @return {object} featureList: object with the list of features
* @private
*/
const addExamplesTablesToScenarioVariable = (featureList, featureIndex) => {
var features = featureList.map(feature =>{
feature.feature.children = feature.feature.children.map(scenario => {
if(scenario.examples){
var examples = scenario.examples[0];
var examplesHeaderCells = examples.tableHeader.cells.map(cell => cell.value);
var examplesRows = examples.tableBody.map(row =>
{
var rowContent = [];
row.cells.forEach(cell =>
{
rowContent.push(cell.value);
});
return rowContent;
});
var rows = [];
rows.push(examplesHeaderCells);
rows = rows.concat(examplesRows);
scenario.examplesForReport = rows;
}
return scenario;
});
return feature;
});

return features;
}

module.exports = {getFeatures};
99 changes: 99 additions & 0 deletions lib/format-json-report.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@

const formatJson = (options,jsonJoined,featureList) => {
var jsonReport = addAnAdditionalScenarioForEachScenarioOutline(jsonJoined, featureList);
jsonReport = addExamplesTablesAndChangeScenarioNameAndStepsTextOfFirstScenarioOutlineOfBlock(jsonJoined,featureList);

return jsonReport;
}

/**
* Add additional scenario to the json for each scenario outline with id ending in 1
* to find the unique scenarios it will search for id ending with ";;2"
* @param {options} entry options
* @return {array} array with the list of jsons
* @private
*/
const addAnAdditionalScenarioForEachScenarioOutline = (jsonJoined, featureList) =>{

jsonJoined.forEach(feature => {

var scenarios = feature.elements;
var scenariosTemp = [];

if(scenarios != null){
scenarios.forEach(function(scenario) {
var id = (String)(scenario.id);

if (id.substring(id.length - 3,id.length) === ";;2"){
var scenario1 = JSON.parse(JSON.stringify(scenario));
scenario1.id = id.substring(0,id.length - 3) + ";;1";
scenario1.name = "test";

scenario1.steps.forEach(function(step, index) {
step.output = "";
step.text = "";
step.result = "";
});

scenariosTemp.push(scenario1);
}
scenariosTemp.push(scenario);
});
}
feature.elements = scenariosTemp;
});

return jsonJoined;
}

/**
* Add the examples tables to the joined json, and modify the name and steps of the first scenario outline to be equal to the ones of the
* feature file
* @param {jsonToBeAdded} json that will be added to the new examples element
* @param {value} id of the element of the array that is going to be searched without the last two digits
* @return {jsonJoined} json with all the data
* @private
*/
const addExamplesTablesAndChangeScenarioNameAndStepsTextOfFirstScenarioOutlineOfBlock = (jsonReport,featureList) => {

var features = jsonReport.map(featureJson =>{
featureList.forEach(featureFromFolder =>{
featureFromFolderName = featureFromFolder.feature.name;
featureJsonName = featureJson.name;
if(featureFromFolderName == featureJsonName && featureJson.elements){
featureJson.elements = featureJson.elements.map(scenarioJson => {
featureFromFolder.feature.children.forEach(scenarioFromFolder =>{
var scenarioFromFolderName = scenarioFromFolder.name;
var scenarioJsonName = JSON.parse('"' + scenarioJson.name + '"');
var scenarioFromFolderId = scenarioFromFolder.id;
var scenarioJsonId = scenarioJson.id;

if((String)(scenarioFromFolderId) === (String)(scenarioJsonId.substring(0,scenarioFromFolderId.length))
&& scenarioJsonId.substring(scenarioJsonId.length - 3,scenarioJsonId.length) === ";;1"
&& scenarioJson.keyword ==="Scenario Outline"){
scenarioJson.examples = scenarioFromFolder.examplesForReport;
scenarioJson.name = escapeHTML(scenarioFromFolderName);
scenarioFromFolder.steps.forEach(stepScenarioFolder =>{
scenarioJson.steps.forEach(stepScenarioJson =>{
if (stepScenarioJson.line == stepScenarioFolder.location.line)
stepScenarioJson.name = escapeHTML(stepScenarioFolder.text);
}
)
})
}
});
return scenarioJson;
});
}
});
return featureJson;
});

return jsonReport;
}

const escapeHTML = (value) =>{
return value.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
}

module.exports = {formatJson};
Loading