Skip to content

Commit

Permalink
feat: add support for dynamic filename for HTML Report (#3667)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vaibhav Singh authored and gravityvi committed Apr 7, 2023
1 parent 4173ac0 commit b82b75f
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 8 deletions.
3 changes: 2 additions & 1 deletion lib/reporter/global-reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ module.exports = class GlobalReporter {
}

async writeReport(reporter, globalResults) {
const {globals, output_folder, start_session, folder_format} = this.settings;
const {globals, output_folder, start_session, folder_format, reporter_options: {filename_format}} = this.settings;
const needsCallback = reporter.write.length === 3;
const options = {
filename_prefix: this.settings.report_prefix,
Expand All @@ -241,6 +241,7 @@ module.exports = class GlobalReporter {
start_session,
reporter,
openReport: this.openReport,
filename_format,
folder_format
};

Expand Down
23 changes: 19 additions & 4 deletions lib/reporter/reporters/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ class HtmlReporter extends BaseReporter {
return folderPrefix;
}

getFileName() {
let fileName = 'index';
const {filename_format} = this.options;
if (filename_format) {
if (typeof filename_format === 'function') {
fileName = filename_format(this.results);
} else if (typeof filename_format === 'string') {
fileName = filename_format;
}
}

return fileName;
}

computePassedAndFailedCounts(module) {
const result = {passedCount: 0, failedCount: 0};

Expand Down Expand Up @@ -230,16 +244,17 @@ class HtmlReporter extends BaseReporter {
const shouldOpenReport = this.options.openReport;

const destFolder = path.join(output_folder, this.getFolderPrefix(), 'nightwatch-html-report');
const filename = path.join(destFolder, 'index.html');
const fileName = `${this.getFileName()}.html`;
const filePath = path.join(destFolder, `${this.getFileName()}.html`);

const jsonString = JSON.stringify(results);

HtmlReact.writeNightwatchHTMLReport(destFolder, jsonString);
HtmlReact.writeNightwatchHTMLReport(destFolder, fileName, jsonString);

Logger.logDetailedMessage(Logger.colors.stack_trace(` Wrote HTML report file to: ${path.resolve(filename)}` + '\n'), 'info');
Logger.logDetailedMessage(Logger.colors.stack_trace(` Wrote HTML report file to: ${path.resolve(filePath)}` + '\n'), 'info');

if (shouldOpenReport) {
return this.openReporter(filename);
return this.openReporter(filePath);
}

}
Expand Down
5 changes: 4 additions & 1 deletion lib/settings/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,10 @@ module.exports = {
output_folder: 'tests_output',

//The folder formatting to use while saving HTML report
folder_format: null
folder_format: null,

// The file name formatting to use while saving HTML report
filename_format: null
},

// A string or array of folders (excluding subfolders) where the tests are located.
Expand Down
6 changes: 4 additions & 2 deletions test/src/runner/testReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ describe('testReporter', function() {
done();
}
},
output_folder: 'output'
output_folder: 'output',
reporter_options: {}
});

return reporter.writeReportToFile().then(function(result) {
Expand Down Expand Up @@ -385,7 +386,8 @@ describe('testReporter', function() {
done();
}
},
output_folder: 'output'
output_folder: 'output',
reporter_options: {}
});

return reporter.writeReportToFile().then(function(result) {
Expand Down
41 changes: 41 additions & 0 deletions test/src/runner/testRunnerHtmlOutput.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,45 @@ describe('testRunnerHTMLOutput', function() {
then(_ => {
});
});

it('test html report file with a file format function', function () {

const testsPath = [
path.join(__dirname, '../../sampletests/withfailures')
];

MockServer.addMock({
url: '/wd/hub/session/1352110219202/screenshot',
method: 'GET',
response: JSON.stringify({
sessionId: '1352110219202',
status: 0,
value: '<faketag>fakedata'
})
}, true);


return runTests({source: testsPath, reporter: 'html'}, settings({
output_folder: outputPath,
globals: {
waitForConditionPollInterval: 20,
waitForConditionTimeout: 50,
retryAssertionTimeout: 50,
reporter: function () {
}
},
reporter_options: {
filename_format: function() {
return 'test_report';
}
}
}))
.then(_ => {
return readFilePromise(`${outputPath}${path.sep}nightwatch-html-report${path.sep}test_report.html`);
}).
then(_ => {
}).catch((err) => {
console.log(err);
});
});
});

0 comments on commit b82b75f

Please sign in to comment.