Skip to content

Commit

Permalink
feat(protractor): auto aggregate reports (#435)
Browse files Browse the repository at this point in the history
  • Loading branch information
stoffeastrom committed Mar 6, 2019
1 parent cef3428 commit 7ea0dca
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 24 deletions.
26 changes: 22 additions & 4 deletions commands/protractor/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const utils = require('@after-work.js/utils');
const options = require('./options');
const { getIPaddress, logSeleniumNodeInfo } = require('./utils');

const { uiReport, reporter } = require('./plugins/reporter');
const { uiReport, reporter, aggregate } = require('./plugins/reporter');

const screenshoterPath = path.resolve(
__dirname,
Expand Down Expand Up @@ -61,13 +61,17 @@ const setReporterInfo = async (browser) => {
browser.reporterInfo.browserVersion = cap.get('version');
const platform = browserName === 'internet-explorer' ? 'WINDOwS' : cap.get('platform');
browser.reporterInfo.platform = platform.replace(/ /g, '-').toLowerCase();
browser.reporterInfo.reportName = `${
browser.reporterInfo.browserName
browser.reporterInfo.reportName = `${browser.reporterInfo.browserName}-${
process.env.AW_CURRENT_SESSION_TIMESTAMP
}-report-${browser.reporterInfo.startTime}_${Math.floor(
Math.random() * 10000000,
)}`;
};

if (!process.env.AW_CURRENT_SESSION_TIMESTAMP) {
process.env.AW_CURRENT_SESSION_TIMESTAMP = +new Date();
}

const baseConfig = {
// ---------------------------------------------------------------------------
// ----- How to connect to Browser Drivers -----------------------------------
Expand Down Expand Up @@ -293,7 +297,17 @@ const baseConfig = {
// the WebDriver instance has been shut down. It is passed the exit code
// (0 if the tests passed). This is called only once before the program
// exits (after onCleanUp).
afterLaunch() {},
afterLaunch() {
const pattern = `${process.env.AW_CURRENT_ARTIFACTS_PATH}/*-${
process.env.AW_CURRENT_SESSION_TIMESTAMP
}-report-*.json`;
const reports = globby.sync(pattern);
aggregate(
`all-${process.env.AW_CURRENT_SESSION_TIMESTAMP}-reports`,
process.env.AW_CURRENT_ARTIFACTS_PATH,
reports,
);
},

// The params object will be passed directly to the Protractor instance,
// and can be accessed from your test as browser.params. It is an arbitrary
Expand Down Expand Up @@ -405,6 +419,10 @@ if (!argv.specs && argv.glob.length) {
argv.specs = specs;
}

if (!process.env.AW_CURRENT_ARTIFACTS_PATH) {
process.env.AW_CURRENT_ARTIFACTS_PATH = argv.artifactsPath;
}

module.exports = {
config: argv,
};
28 changes: 8 additions & 20 deletions commands/protractor/src/plugins/reporter/aggregate.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,19 @@ function reduceTestObject(accumulator, currentValue) {
return accumulator;
}

module.exports = function aggregateReports(reportName, artifactsPath) {
const files = fs.readdirSync(artifactsPath);

const allTests = files.filter(name => path.extname(name) === '.json')
.map(name => path.resolve(artifactsPath, name))
.map(filepath => JSON.parse(fs.readFileSync(filepath, 'utf8')));

if (allTests.length === 0) {
module.exports = function aggregateReports(reportName, artifactsPath, reports) {
if (reports.length <= 1) {
return undefined;
}

const sumTests = allTests.reduce(reduceTestObject);
const sumTests = reports
.map(r => JSON.parse(fs.readFileSync(r, 'utf8')))
.reduce(reduceTestObject);

if (sumTests) {
console.error(sumTests);
const fileName = path.resolve(artifactsPath, `${reportName}.json`);

return new Promise((resolve, reject) => {
fs.writeFile(fileName, JSON.stringify(sumTests, null, '\t'), (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
}).then(() => report.generate(fileName));
fs.writeFileSync(fileName, JSON.stringify(sumTests, null, '\t'));
report.generate(fileName);
}
return sumTests;
};
2 changes: 2 additions & 0 deletions commands/protractor/src/plugins/reporter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const fs = require('fs');
const path = require('path');
const utils = require('./utils');
const report = require('./create-static');
const aggregate = require('./aggregate');

const { Base } = mocha.reporters;

Expand Down Expand Up @@ -300,4 +301,5 @@ function uiReport(runner, options) {
module.exports = {
uiReport,
reporter,
aggregate,
};

0 comments on commit 7ea0dca

Please sign in to comment.