Skip to content

Commit

Permalink
Extensible approach to custom reports
Browse files Browse the repository at this point in the history
  • Loading branch information
justyna-olszak-wttech committed Jan 14, 2022
1 parent dbf9a7a commit 4788b30
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 25 deletions.
28 changes: 20 additions & 8 deletions core/command/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const allSettled = require('../util/allSettled');
const fs = require('../util/fs');
const logger = require('../util/logger')('report');
const compare = require('../util/compare/');
const writeXrayReport = require('../util/writeXrayReport');

function replaceInFile (file, search, replace) {
return new Promise((resolve, reject) => {
Expand All @@ -26,20 +25,33 @@ function replaceInFile (file, search, replace) {
});
}

async function processCustomReport (config, reporter) {
const engineScriptsPath = config.engine_scripts;
const customReport = config.customReport;
if (customReport) {
const customReportScript = path.resolve(engineScriptsPath, customReport.script);
if (fs.existsSync(customReportScript)) {
return await require(customReportScript)(config, reporter);
} else {
console.warn('WARNING: reporting script not found: ' + customReportScript);
}
}
}

function writeReport (config, reporter) {
const promises = [];

if (config.report && config.report.indexOf('CI') > -1 && config.ciReport.format === 'junit') {
promises.push(writeJunitReport(config, reporter));
} else if (config.report && config.report.indexOf('Xray') > -1) {
promises.push(writeXrayReport(config, reporter))
} else {
if (config.report && config.report.indexOf('json') > -1) {
promises.push(writeJsonReport(config, reporter));
}
promises.push(writeBrowserReport(config, reporter));
}

if (config.report && config.report.indexOf('json') > -1) {
promises.push(writeJsonReport(config, reporter));
}

promises.push(writeBrowserReport(config, reporter));
promises.push(processCustomReport(config, reporter));

return allSettled(promises);
}

Expand Down
7 changes: 2 additions & 5 deletions core/util/engineTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,11 @@ function generateTestPair (config, scenario, viewport, variantOrScenarioLabelSaf
test: testFilePath,
selector: selector,
fileName: fileName,
label: scenario.label,
requireSameDimensions: getRequireSameDimensions(scenario, config),
misMatchThreshold: getMisMatchThreshHold(scenario, config),
url: scenario.url,
referenceUrl: scenario.referenceUrl,
expect: getScenarioExpect(scenario),
viewportLabel: viewport.label,
metadata: scenario.metadata
veportLabel: viewport.label,
...scenario
};
}

Expand Down
1 change: 1 addition & 0 deletions core/util/extendConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ function extendConfig (config, userConfig) {
config.asyncCompareLimit = userConfig.asyncCompareLimit;
config.backstopVersion = version;
config.dockerCommandTemplate = userConfig.dockerCommandTemplate;
config.customReport = userConfig.customReport;
return config;
}

Expand Down
7 changes: 6 additions & 1 deletion test/configs/backstop.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"postInteractionWait": 0,
"selectors": [],
"selectorExpansion": true,
"misMatchThreshold" : 0.1,
"misMatchThreshold": 0.1,
"requireSameDimensions": true,
"metadata": ["TEST-1"]
}
Expand All @@ -47,6 +47,11 @@
"engineOptions": {
"args": ["--no-sandbox"]
},
"customReport": {
"script": "customReports/xrayReport.js",
"reportLocation": "backstop_data/xray_report",
"reportName": "xrayReport.json"
},
"asyncCaptureLimit": 5,
"asyncCompareLimit": 50,
"debug": false,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
const fs = require('../util/fs');
const { writeFile } = require('fs');
const { ensureDir } = require('fs-extra');
const path = require('path');
const logger = require('../util/logger')('Xray report');
const _ = require('lodash');
const cloneDeep = require('lodash/cloneDeep');
const { stat } = require('fs');
const util = require('util');

module.exports = function (config, reporter) {
const jsonReporter = cloneDeep(reporter);

function toAbsolute (p) {
return path.isAbsolute(p) ? p : path.join(config.projectPath, p);
Expand Down Expand Up @@ -44,7 +43,6 @@ module.exports = function (config, reporter) {
);
}

debugger;
return transformedTestCases;
}

Expand All @@ -56,19 +54,22 @@ module.exports = function (config, reporter) {
return results;
}

logger.log('Writing Xray json report');
const jsonReporter = cloneDeep(reporter);
const ensureDirPromise = util.promisify(ensureDir);
const writeFilePromise = util.promisify(writeFile);

return fs.ensureDir(toAbsolute(config.json_report)).then(function () {
return ensureDirPromise(toAbsolute(config.customReport.reportLocation)).then(function () {
const res = transformToXrayJson(jsonReporter.tests);
const reportPath = toAbsolute(path.join(config.customReport.reportLocation, config.customReport.reportName));

return fs.writeFile(toAbsolute(config.compareJsonFileName), JSON.stringify(res, null, 2)).then(
return writeFilePromise(reportPath, JSON.stringify(res, null, 2)).then(
function () {
logger.log('Wrote Xray Json report to: ' + toAbsolute(config.compareJsonFileName));
console.log('Wrote Xray report to: ' + reportPath);
},
function (err) {
logger.error('Failed writing Xray Json report to: ' + toAbsolute(config.compareJsonFileName));
console.error('Failed writing Xray report to: ' + reportPath);
throw err;
}
);
});
};
};

0 comments on commit 4788b30

Please sign in to comment.