Skip to content
This repository has been archived by the owner on Jan 26, 2023. It is now read-only.

Commit

Permalink
introduced parameter object for ctor. introduced possibility to omit …
Browse files Browse the repository at this point in the history
…capture of skipped test cases
  • Loading branch information
Manuel Alabor committed Jan 30, 2014
1 parent 71fe968 commit bf6b026
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 107 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# Changelog
## Version 0.0.3
* Introduced parameter object for reporter constructor
* Define if you want to create screenshots from skipped test cases using the new `takeScreenShotsForSkippedSpecs` option

## Version 0.0.2
* Meta data contained the screenshot file path based on the wrong directory. Fixed.

Expand Down
46 changes: 30 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,26 @@ In your Protractor configuration file, register `protractor-screenshot-reporter`
var ScreenShotReporter = require('protractor-screenshot-reporter');

exports.config = {
// your config here ...
// your config here ...

onPrepare: function() {
// Add a screenshot reporter and store screenshots to `/tmp/screnshots`:
jasmine.getEnv().addReporter(new ScreenShotReporter('/tmp/screenshots'));
}
onPrepare: function() {
// Add a screenshot reporter and store screenshots to `/tmp/screnshots`:
jasmine.getEnv().addReporter(new ScreenShotReporter({
baseDirectory: '/tmp/screenshots')
});
}
}
```
## Configuration
### Target Directory (mandatory)
### Base Directory (mandatory)
You have to pass a directory path as parameter when creating a new instance of
the screenshot reporter:
```javascript
var reporter = new ScreenShotReporter('/tmp/screenshots');
var reporter = new ScreenShotReporter({
baseDirectory: '/tmp/screenshots'
});
```
If the given directory does not exists, it is created automatically as soon as a screenshot needs to be stored.
Expand All @@ -43,14 +47,14 @@ The function passed as second argument to the constructor is used to build up pa
```javascript
var path = require('path');

new ScreenShotReporter(
'/tmp/screenshots'
, function pathBuilder(spec, descriptions, results, capabilities) {
new ScreenShotReporter({
baseDirectory: '/tmp/screenshots'
, pathBuilder: function pathBuilder(spec, descriptions, results, capabilities) {
// Return '<browser>/<specname>' as path for screenshots:
// Example: 'firefox/list-should work'.
return path.join(capabilities.caps_.browser, descriptions.join('-'));
}
);
});
```
If you omit the path builder, a [GUID](http://de.wikipedia.org/wiki/Globally_Unique_Identifier) is used by default instead.
Expand All @@ -59,21 +63,31 @@ If you omit the path builder, a [GUID](http://de.wikipedia.org/wiki/Globally_Uni
You can modify the contents of the JSON meta data file by passing a function `metaDataBuilder` function as third constructor parameter:
```javascript
new ScreenShotReporter(
'/tmp/screenshots'
, undefined
, function metaDataBuilder(spec, descriptions, results, capabilities) {
new ScreenShotReporter({
baseDirectory: '/tmp/screenshots'
, metaDataBuilder: function metaDataBuilder(spec, descriptions, results, capabilities) {
// Return the description of the spec and if it has passed or not:
return {
description: descriptions.join(' ')
, passed: results.passed()
};
}
);
});
```
If you omit the meta data builder, the default implementation is used (see https://github.com/swissmanu/protractor-screenshot-reporter/blob/master/index.js#L42).
### Screenshots for skipped test cases (optional)
Since version 0.0.3, you can define if you want capture screenshots from skipped test cases using the `takeScreenShotsForSkippedSpecs` option:
```javascript
new ScreenShotReporter({
baseDirectory: '/tmp/screenshots'
, takeScreenShotsForSkippedSpecs: true
});
```
Default is `false`.
## Postprocess Meta Data
A screenshot is saved as PNG image. Along with it, a JSON file with a matching filename is created.
Expand Down
118 changes: 28 additions & 90 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,107 +62,40 @@ function defaultMetaDataBuilder(spec, descriptions, results, capabilities) {


/** Class: ScreenshotReporter
* Screenshot Reporter for Protractor:
* Running functional E2E tests using Protractor is nice. Executing them on a
* remote and/or virtualized Selenium Grid improves the productivity even more.
* But at least when a test case fails, you may become curious about the actual
* rendered output of the browser which runned the test.
*
* The protractor-screenshot-reporter creates a screenshot for each executed
* test. Along with a JSON file containining the test outcome, it gets saved on
* the machine which runs Protractor.
*
* This allows a deeper analysis of what happend during a failed test.
*
*
* Usage:
* The `protractor-screenshot-reporter` is available via npm:
*
* npm install protractor-screenshot-reporter --save-dev
*
* In your Protractor configuration file, register
* `protractor-screenshot-reporter` in Jasmine:
*
* var ScreenShotReporter = require('protractor-screenshot-reporter');
*
* exports.config = {
*
* // your config here ...
*
* onPrepare: function() {
* // Add a screenshot reporter and store screenshots into
* // `./test/screenshots`:
* jasmine.getEnv().addReporter(new ScreenShotReporter('./test/screenshots'));
* }
* };
*
*
* Configuration: Path Builder:
* The function passed as second argument is used to build up paths for
* screenshot files. The following example is the default implementation if you
* pass nothing:
*
* function defaultPathBuilder(spec, descriptions, results, capabilities) {
* return util.generateGuid();
* }
*
* Use this as a blueprint for your own path builders.
*
*
* Configuration: Meta Data Builder:
* You can modify the contents of the JSON meta data file by passing a function
* `metaDataBuilder` function as third parameter.
* Following example shows the default implementation which is used if you pass
* nothing. Use it as example when developing your own customizations of it:
*
* function defaultMetaDataBuilder(spec, descriptions, results
* , capabilities) {
*
* var metaData = {
* description: specDescriptions.join(' ')
* , passed: results.passed()
* , os: capabilities.caps_.platform
* , browser: {
* name: capabilities.caps_.browserName
* , version: capabilities.caps_.version
* }
* };
*
* if(results.items_.length > 0) {
* result = results.items_[0];
* metaData.message = result.message;
* metaData.trace = result.trace.stack;
* }
*
* return metaData;
* }
* Creates a new screenshot reporter using the given `options` object.
*
* For more information, please look at the README.md file.
*
* Parameters:
* (Object) options - Object with options as described below.
*
* Possible options:
* (String) baseDirectory - The path to the directory where screenshots are
* stored. If not existing, it gets created.
* Mandatory.
* (Function) pathBuilder - A function which returns a path for a screenshot
* to be stored.
* to be stored. Optional.
* (Function) metaDataBuilder - Function which returns an object literal
* containing meta data to store along with
* the screenshot.
* the screenshot. Optional.
* (Boolean) takeScreenShotsForSkippedSpecs - Do you want to capture a
* screenshot for a skipped spec?
* Optional (default: false).
*/
function ScreenshotReporter(baseDirectory, pathBuilder, metaDataBuilder) {
if(!baseDirectory || baseDirectory.length === 0) {
function ScreenshotReporter(options) {
options = options || {};

if(!options.baseDirectory || options.baseDirectory.length === 0) {
throw new Error('Please pass a valid base directory to store the ' +
'screenshots into.');
} else {
this.baseDirectory = options.baseDirectory;
}

if(!pathBuilder) {
pathBuilder = defaultPathBuilder;
}
if(!metaDataBuilder) {
metaDataBuilder = defaultMetaDataBuilder;
}

this.baseDirectory = baseDirectory;
this.pathBuilder = pathBuilder;
this.metaDataBuilder = metaDataBuilder;
this.pathBuilder = options.pathBuilder || defaultPathBuilder;
this.metaDataBuilder = options.metaDataBuilder || defaultMetaDataBuilder;
this.takeScreenShotsForSkippedSpecs =
options.takeScreenShotsForSkippedSpecs || false;
}

/** Function: reportSpecResults
Expand All @@ -175,15 +108,20 @@ function ScreenshotReporter(baseDirectory, pathBuilder, metaDataBuilder) {
ScreenshotReporter.prototype.reportSpecResults =
function reportSpecResults(spec) {
/* global browser */
var self = this;
var self = this
, results = spec.results()

if(!self.takeScreenShotsForSkippedSpecs && results.skipped) {
return;
}

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


, baseName = self.pathBuilder(
spec
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-screenshot-reporter",
"version": "0.0.2",
"version": "0.0.3",
"description": "Use the screenshot reporter to capture screenshots from your Selenium nodes after each executed Protractor test case.",
"main": "index.js",
"repository": {
Expand Down

0 comments on commit bf6b026

Please sign in to comment.