Skip to content

Commit

Permalink
Forked for jasmine2 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
Miguel Ping committed Jan 26, 2015
1 parent fafbf8c commit 8b1bd69
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 89 deletions.
24 changes: 14 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### This is a fork for jasmine2 compatibility ###
As soon as https://github.com/jintoppy/protractor-html-screenshot-reporter/issues/44 is solved,
there's no need for this fork

# HTML Reporter with Screenshots for Protractor

This is built on top of Screenshot Reporter for Protractor https://github.com/swissmanu/protractor-screenshot-reporter
Expand Down Expand Up @@ -90,21 +94,21 @@ Default is `false`.

### Screenshots only for failed test cases (optional)
Also you can define if you want capture screenshots only from failed test cases using the `takeScreenShotsOnlyForFailedSpecs:` option:

```javascript
new HtmlReporter({
baseDirectory: '/tmp/screenshots'
, takeScreenShotsOnlyForFailedSpecs: true
});
```
If you set the value to `true`, the reporter for the passed test will still be generated, but, there will be no screenshot.

Default is `false`.


### Add title for the html report (optional)
Also you can define a document title for the html report generated using the `docTitle:` option:

```javascript
new HtmlReporter({
baseDirectory: '/tmp/screenshots'
Expand All @@ -116,7 +120,7 @@ Default is `Generated test report`.

### Change html report file name (optional)
Also you can change document name for the html report generated using the `docName:` option:

```javascript
new HtmlReporter({
baseDirectory: '/tmp/screenshots'
Expand All @@ -127,7 +131,7 @@ Default is `report.html`.

### Option to override CSS file used in reporter (optional)
You can change stylesheet used for the html report generated using the `cssOverrideFile:` option:

```javascript
new HtmlReporter({
baseDirectory: '/tmp/screenshots'
Expand All @@ -137,7 +141,7 @@ Default is `report.html`.

### Preserve base directory (optional)
You can preserve the base directory using `preserveDirectory:` option:

```javascript
new HtmlReporter({
baseDirectory: '/tmp/screenshots'
Expand All @@ -150,14 +154,14 @@ Default is `false`.

On running the task via grunt, screenshot reporter will be generating json and png files for each test.

Now, you will also get a summary report, Stack trace information also.
Now, you will also get a summary report, Stack trace information also.

With this postprocessing, you will get a json which has all the metadata, and also an html page showing the results.
With this postprocessing, you will get a json which has all the metadata, and also an html page showing the results.


![test report in html](https://raw.githubusercontent.com/jintoppy/protractor-html-screenshot-reporter/master/testreporter.png "test report")

Please see the examples folder for a sample usage.
Please see the examples folder for a sample usage.

For running the sample, do the following commands in the examples folder

Expand All @@ -168,7 +172,7 @@ $ grunt install
$ grunt test:e2e
```

After the test run, you can see that, a screenshots folder will be created with all the reports generated.
After the test run, you can see that, a screenshots folder will be created with all the reports generated.


## License
Expand Down
174 changes: 101 additions & 73 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,31 +41,39 @@ function defaultPathBuilder(spec, descriptions, results, capabilities) {
* (Object) containig meta data to store along with a screenshot
*/
function defaultMetaDataBuilder(spec, descriptions, results, capabilities) {
var passed = spec.passedExpectations
, failed = spec.failedExpectations;

var metaData = {
description: descriptions.join(' ')
, passed: results.passed()
, passed: _.every(passed.concat(failed), function(it){return it.passed})
, os: capabilities.caps_.platform
, browser: {
name: capabilities.caps_.browserName
, version: capabilities.caps_.version
}
};
if(results.items_.length > 0) {
var result = results.items_[0];
if(!results.passed()){
var failedItem = _.where(results.items_,{passed_: false})[0];
if(failedItem){
metaData.message = failedItem.message || 'Failed';
metaData.trace = failedItem.trace? (failedItem.trace.stack || 'No Stack trace information') : 'No Stack trace information';
}

}else{
metaData.message = result.message || 'Passed';
metaData.trace = result.trace.stack;
console.log(passed,failed)
if(passed.length > 0 || failed.length > 0) {
var result = passed[0];

if(failed.length > 0) {
var messages = _.pluck(failed, 'message'),
stacks = _.pluck(failed, 'stack').join('\n'),

//report all failures
metaData.message = messages.length && messages.join('\n') || 'Failed';
metaData.trace = stacks.length && stacks.join('\n') : 'No Stack trace information';

} else {
metaData.message = result && result.message || 'Passed';
metaData.trace = result && result.stack;
}

}

console.log(metaData)

return metaData;
}

Expand Down Expand Up @@ -132,68 +140,88 @@ function ScreenshotReporter(options) {
}
}

/** Function: reportSpecResults
* Called by Jasmine when reporting results for a test spec. It triggers the
* whole screenshot capture process and stores any relevant information.
*
* Parameters:
* (Object) spec - The test spec to report.
*/
ScreenshotReporter.prototype.reportSpecResults =
function reportSpecResults(spec) {
/* global browser */
var self = this
, results = spec.results()

if(!self.takeScreenShotsForSkippedSpecs && results.skipped) {
return;
}
var currentSuite, currentSpec;
ScreenshotReporter.prototype.jasmineStarted = function () {
//console.log("##test[progressStart 'Running Jasmine Tests']");
};

browser.takeScreenshot().then(function (png) {
browser.getCapabilities().then(function (capabilities) {
var descriptions = util.gatherDescriptions(
spec.suite
, [spec.description]
)


, baseName = self.pathBuilder(
spec
, descriptions
, results
, capabilities
)
, metaData = self.metaDataBuilder(
spec
, descriptions
, results
, capabilities
)

, screenShotFile = baseName + '.png'
, metaFile = baseName + '.json'
, screenShotPath = path.join(self.baseDirectory, screenShotFile)
, metaDataPath = path.join(self.baseDirectory, metaFile)

// pathBuilder can return a subfoldered path too. So extract the
// directory path without the baseName
, directory = path.dirname(screenShotPath);

metaData.screenShotFile = screenShotFile;
mkdirp(directory, function(err) {
if(err) {
throw new Error('Could not create directory ' + directory);
} else {
util.addMetaData(metaData, metaDataPath, descriptions, self.finalOptions);
if(!(self.takeScreenShotsOnlyForFailedSpecs && results.passed())) {
util.storeScreenShot(png, screenShotPath);
}
util.storeMetaData(metaData, metaDataPath);
}
});
});
});
ScreenshotReporter.prototype.jasmineDone = function () {
//console.log("##test[progressFinish 'Running Jasmine Tests']");
};

ScreenshotReporter.prototype.suiteStarted = function (suite) {
currentSuite = suite;
//console.log("##START[testSuiteStarted name='" + (suite.fullName) + "']");
};

ScreenshotReporter.prototype.suiteDone = function (suite) {
//console.log("##test[testSuiteFinished name='" + (suite.fullName) + "']");
};

ScreenshotReporter.prototype.specStarted = function (spec) {
currentSpec = spec;
//console.log("##START[testStarted name='" + (spec.description) + "' captureStandardOutput='true']");
};

ScreenshotReporter.prototype.specDone = function (spec) {
//console.log("##test[testFinished name='" + (spec.description) + "']");

/** Function: specDone
* Called by Jasmine when a test spec is done. It triggers the
* whole screenshot capture process and stores any relevant information.
*
* Parameters:
* (Object) spec - The test spec to report.
*/

var self = this, results = spec;
if(!self.takeScreenShotsForSkippedSpecs && results.skipped) {
return;
}

browser.takeScreenshot().then(function (png) {
browser.getCapabilities().then(function (capabilities) {
var descriptions = util.gatherDescriptions(
currentSuite
, [spec.description]
)

, baseName = self.pathBuilder(
spec
, descriptions
, results
, capabilities
)
, metaData = self.metaDataBuilder(
spec
, descriptions
, results
, capabilities
)

, screenShotFile = baseName + '.png'
, metaFile = baseName + '.json'
, screenShotPath = path.join(self.baseDirectory, screenShotFile)
, metaDataPath = path.join(self.baseDirectory, metaFile)

// pathBuilder can return a subfoldered path too. So extract the
// directory path without the baseName
, directory = path.dirname(screenShotPath);

metaData.screenShotFile = screenShotFile;
mkdirp(directory, function(err) {
if(err) {
throw new Error('Could not create directory ' + directory);
} else {
util.addMetaData(metaData, metaDataPath, descriptions, self.finalOptions);
if(!(self.takeScreenShotsOnlyForFailedSpecs && results.passedExpectations)) {

This comment has been minimized.

Copy link
@avnersorek

avnersorek Mar 12, 2015

results.passedExpectations is always true (it can be an empty array though).
Should be
if(!(self.takeScreenShotsOnlyForFailedSpecs && results.status === 'passed')) {

util.storeScreenShot(png, screenShotPath);
}
util.storeMetaData(metaData, metaDataPath);
}
});
});
});
};

module.exports = ScreenshotReporter;
10 changes: 5 additions & 5 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ function addHTMLReport(jsonData, baseName, options){
var basePath = path.dirname(baseName),
htmlFile = path.join(basePath, options.docName),
stream;

stream = fs.createWriteStream(htmlFile);
stream.write(jsonParser.processJson(jsonData, options));
stream.end();

}

function addMetaData(metaData, baseName, descriptions, options){
Expand Down Expand Up @@ -109,10 +109,10 @@ function storeMetaData(metaData, file) {
* itself.
*/
function gatherDescriptions(suite, soFar) {
soFar.push(suite.description);
if(suite) soFar.push(suite.description);

if(suite.parentSuite) {
return gatherDescriptions(suite.parentSuite, soFar);
if(suite && suite._parent) {
return gatherDescriptions(suite._parent, soFar);
} else {
return soFar;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "protractor-html-screenshot-reporter",
"version": "0.0.19",
"version": "0.0.20",
"description": "An npm module and grunt plugin which generates your Protractor test reports in HTML with screenshots",
"main": "index.js",
"repository": {
Expand Down

0 comments on commit 8b1bd69

Please sign in to comment.