Skip to content

Commit

Permalink
Added more options for report naming
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenzitron committed Dec 30, 2016
1 parent 21e0a85 commit f7b813b
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 9 deletions.
60 changes: 58 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ Filename for html report.

<pre><code>jasmine.getEnv().addReporter(new Jasmine2HtmlReporter({
savePath: './test/reports/',
filePrefix: 'index'
fileNamePrefix: 'Prefix'
}));</code></pre>

Default is <code>htmlReport.html</code>
Default is <code>nothing</code>

### Consolidate and ConsolidateAll (optional)

Expand Down Expand Up @@ -130,3 +130,59 @@ This option, if false, will show only failures.
}));</code></pre>

Default is <code>true</code>

### fileName (optional)

This will be the name used for the html file generated thanks to this tool.

<pre><code>jasmine.getEnv().addReporter(new Jasmine2HtmlReporter({
....
fileName: 'MyReportName'
}));</code></pre>

Default is <code>htmlReport</code>

### fileNameSeparator (optional)

This will set the separator between filename elements, for example, prefix, sufix etc.

<pre><code>jasmine.getEnv().addReporter(new Jasmine2HtmlReporter({
....
fileNameSeparator: '_'
}));</code></pre>

Default is <code>-</code>

### fileNamePrefix (optional)

Prefix used before the name of the report

<pre><code>jasmine.getEnv().addReporter(new Jasmine2HtmlReporter({
....
fileNamePrefix: ''
}));</code></pre>

Default is <code>empty</code>

### fileNameSuffix (optional)

Prefix used after the name of the report

<pre><code>jasmine.getEnv().addReporter(new Jasmine2HtmlReporter({
....
fileNameSuffix: ''
}));</code></pre>

Default is <code>empty</code>

### fileNameDateSuffix (optional)

Datetime information to be added in the name of the report. This will be placed after the fileNameSuffix if it exists.
The format is: YYYYMMDD HHMMSS,MILL -> 20161230 133323,728

<pre><code>jasmine.getEnv().addReporter(new Jasmine2HtmlReporter({
....
fileNameDateSuffix: true
}));</code></pre>

Default is <code>false</code>
48 changes: 43 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var fs = require('fs'),

require('string.prototype.startswith');

var UNDEFINED, exportObject = exports;
var UNDEFINED, exportObject = exports, reportDate;

function sanitizeFilename(name){
name = name.replace(/\s+/gi, '-'); // Replace white space with dash
Expand Down Expand Up @@ -69,6 +69,19 @@ function rmdir(dir) {
}catch (e) { log("problem trying to remove a folder:" + dir); }
}

function getReportDate(){
if (reportDate === undefined)
reportDate = new Date();
return reportDate.getFullYear() + '' +
(reportDate.getMonth() + 1) +
reportDate.getDate() + ' ' +
reportDate.getHours() + '' +
reportDate.getMinutes() + '' +
reportDate.getSeconds() + ',' +
reportDate.getMilliseconds();
}


function Jasmine2HTMLReporter(options) {

var self = this;
Expand All @@ -85,7 +98,11 @@ function Jasmine2HTMLReporter(options) {
self.fixedScreenshotName = options.fixedScreenshotName === UNDEFINED ? false : options.fixedScreenshotName;
self.consolidate = options.consolidate === UNDEFINED ? true : options.consolidate;
self.consolidateAll = self.consolidate !== false && (options.consolidateAll === UNDEFINED ? true : options.consolidateAll);
self.filePrefix = options.filePrefix || (self.consolidateAll ? 'htmlReport' : 'htmlReport-');
self.fileNameSeparator = options.fileNameSeparator === UNDEFINED ? '-' : options.fileNameSeparator;
self.fileNamePrefix = options.fileNamePrefix === UNDEFINED ? '' : options.fileNamePrefix;
self.fileNameSuffix = options.fileNameSuffix === UNDEFINED ? '' : options.fileNameSuffix;
self.fileNameDateSuffix = options.fileNameDateSuffix === UNDEFINED ? false : options.fileNameDateSuffix;
self.fileName = options.fileName === UNDEFINED ? 'htmlReport' : options.fileName;
self.cleanDestination = options.cleanDestination === UNDEFINED ? true : options.cleanDestination;
self.showPassed = options.showPassed === UNDEFINED ? true : options.showPassed;

Expand All @@ -110,6 +127,26 @@ function Jasmine2HTMLReporter(options) {
return __specs[spec.id];
}

function getReportFilename(specName){
var name = '';
console.log(self.fileNamePrefix);
if (self.fileNamePrefix)
name += self.fileNamePrefix + self.fileNameSeparator;

name += self.fileName;

if (specName !== undefined)
name += self.fileNameSeparator + specName;

if (self.fileNameSuffix)
name += self.fileNameSeparator + self.fileNameSuffix;

if (self.fileNameDateSuffix)
name += self.fileNameSeparator + getReportDate();

return name;
}

self.jasmineStarted = function(summary) {
totalSpecsDefined = summary && summary.totalSpecsDefined || NaN;
exportObject.startTime = new Date();
Expand Down Expand Up @@ -203,7 +240,7 @@ function Jasmine2HTMLReporter(options) {
}
// if we have anything to write here, write out the consolidated file
if (output) {
wrapOutputAndWriteFile(self.filePrefix, output);
wrapOutputAndWriteFile(getReportFilename(), output);
}
//log("Specs skipped but not reported (entire suite skipped or targeted to specific specs)", totalSpecsDefined - totalSpecsExecuted + totalSpecsDisabled);

Expand All @@ -228,7 +265,7 @@ function Jasmine2HTMLReporter(options) {

/******** Helper functions with closure access for simplicity ********/
function generateFilename(suite) {
return self.filePrefix + getFullyQualifiedSuiteName(suite, true) + '.html';
return getReportFilename(getFullyQualifiedSuiteName(suite, true));
}

function getFullyQualifiedSuiteName(suite, isFilename) {
Expand Down Expand Up @@ -365,9 +402,10 @@ function Jasmine2HTMLReporter(options) {
};

// To remove complexity and be more DRY about the silly preamble and <testsuites> element
var prefix = '<!DOCTYPE html><html><head lang=en><meta charset=UTF-8><title></title><style>body{font-family:"open_sans",sans-serif}.suite{width:100%;overflow:auto}.suite .stats{margin:0;width:90%;padding:0}.suite .stats li{display:inline;list-style-type:none;padding-right:20px}.suite h2{margin:0}.suite header{margin:0;padding:5px 0 5px 5px;background:#003d57;color:white}.spec{width:100%;overflow:auto;border-bottom:1px solid #e5e5e5}.spec:hover{background:#e8f3fb}.spec h3{margin:5px 0}.spec .description{margin:1% 2%;width:65%;float:left}.spec .resume{width:29%;margin:1%;float:left;text-align:center}</style></head>';
var prefix = '<!DOCTYPE html><html><head lang=en><meta charset=UTF-8><title>Test Report - ' + getReportDate() + '</title><style>body{font-family:"open_sans",sans-serif}.suite{width:100%;overflow:auto}.suite .stats{margin:0;width:90%;padding:0}.suite .stats li{display:inline;list-style-type:none;padding-right:20px}.suite h2{margin:0}.suite header{margin:0;padding:5px 0 5px 5px;background:#003d57;color:white}.spec{width:100%;overflow:auto;border-bottom:1px solid #e5e5e5}.spec:hover{background:#e8f3fb}.spec h3{margin:5px 0}.spec .description{margin:1% 2%;width:65%;float:left}.spec .resume{width:29%;margin:1%;float:left;text-align:center}</style></head>';
prefix += '<body><section>';
var suffix = '\n</section></body></html>';

function wrapOutputAndWriteFile(filename, text) {
if (filename.substr(-5) !== '.html') { filename += '.html'; }
self.writeFile(filename, (prefix + text + suffix));
Expand Down
4 changes: 2 additions & 2 deletions test/test3Spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
xdescribe('SPEC 3', function() {

it('2 should add a todo', function() {
it('3 should add a todo', function() {
browser.get('http://www.angularjs.org');

element(by.model('todoList.todoText')).sendKeys('write a protractor test');
Expand All @@ -12,7 +12,7 @@ xdescribe('SPEC 3', function() {
expect(todoList.get(2).getText()).toEqual('write a protractor test');
});

it('2 second it', function() {
it('3 second it', function() {
browser.get('http://www.angularjs.org');

element(by.model('todoList.todoText')).sendKeys('write a protractor test');
Expand Down

0 comments on commit f7b813b

Please sign in to comment.