Skip to content

Commit

Permalink
Added option to change sort function
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcin committed Dec 21, 2017
1 parent 56848c7 commit a944813
Show file tree
Hide file tree
Showing 10 changed files with 377 additions and 25 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## Version 0.5.8
* Added option to change sort function

## Version 0.5.7
* Fixed last spec not being saved
* Fixed pending specs not being saved
Expand Down
54 changes: 42 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,7 @@ In your Protractor configuration file, register `protractor-beautiful-reporter`
#### Jasmine 1.x:

```javascript
var HtmlReporter = require('protractor-beautiful-reporter');

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

onPrepare: function() {
// Add a screenshot reporter and store screenshots to `/tmp/screenshots`:
jasmine.getEnv().addReporter(new HtmlReporter({
baseDirectory: 'tmp/screenshots'
}));
}
}
No longer supported
```

#### Jasmine 2.x:
Expand Down Expand Up @@ -148,6 +137,47 @@ new HtmlReporter({
If you omit this, all images will be stored in main folder.


### Sort function (optional)
You can change default `sortFunction` option:

```javascript
new HtmlReporter({
baseDirectory: 'tmp/screenshots'
, sortFunction: function sortFunction(a, b) {
if (a.cachedBase === undefined) {
var aTemp = a.description.split('|').reverse();
a.cachedBase = aTemp.slice(0).slice(0,-1);
a.cachedName = aTemp.slice(0).join('');
};
if (b.cachedBase === undefined) {
var bTemp = b.description.split('|').reverse();
b.cachedBase = bTemp.slice(0).slice(0,-1);
b.cachedName = bTemp.slice(0).join('');
};

var firstBase = a.cachedBase;
var secondBase = b.cachedBase;

for (var i = 0; i < firstBase.length || i < secondBase.length; i++) {

if (firstBase[i] === undefined) { return -1; }
if (secondBase[i] === undefined) { return 1; }
if (firstBase[i].localeCompare(secondBase[i]) === 0) { continue; }
return firstBase[i].localeCompare(secondBase[i]);
}

var firstTimestamp = a.timestamp;
var secondTimestamp = b.timestamp;

if(firstTimestamp < secondTimestamp) return -1;
else return 1;
}
});
```

If you omit this, all specs will be sorted by timestamp (please be aware that sharded runs look ugly when sorted by default sort).


### Report for skipped test cases (optional)
You can define if you want report from skipped test cases using the `takeScreenShotsForSkippedSpecs` option:

Expand Down
2 changes: 1 addition & 1 deletion examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"protractor": "^5.1.1",
"grunt-protractor-runner": "~0.1.6",
"grunt-shell-spawn": "~0.3.0",
"protractor-beautiful-reporter": "^0.5.3",
"protractor-beautiful-reporter": "^0.5.8",
"grunt-concurrent": "^0.5.0",
"webdriver-manager": "^12.0.6"
}
Expand Down
111 changes: 111 additions & 0 deletions examples/protractor.jasmine2.with.sharding.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
var HtmlReporter = require('protractor-beautiful-reporter');
var path = require('path');

// ----- Config example for Jasmine 2 -----

exports.config = {
// ----- How to setup Selenium -----
//
// There are three ways to specify how to use Selenium. Specify one of the
// following:
//
// 1. seleniumServerJar - to start Selenium Standalone locally.
// 2. seleniumAddress - to connect to a Selenium server which is already
// running.
// 3. sauceUser/sauceKey - to use remote Selenium servers via SauceLabs.

// The location of the selenium standalone server .jar file.
//seleniumServerJar: 'node_modules/protractor/selenium/selenium-server-standalone-2.37.0.jar',

// Address of selenium server (before running protractor, run "webdriver-manager start" to start the Selenium server)
seleniumAddress: 'http://localhost:4444/wd/hub',

// The port to start the selenium server on, or null if the server should
// find its own unused port.
seleniumPort: null,

// Chromedriver location is used to help the selenium standalone server
// find chromedriver. This will be passed to the selenium jar as
// the system property webdriver.chrome.driver. If null, selenium will
// attempt to find chromedriver using PATH.
//chromeDriver: 'node_modules/protractor/selenium/chromedriver',

// Additional command line options to pass to selenium. For example,
// if you need to change the browser timeout, use
// seleniumArgs: ['-browserTimeout=60'],
seleniumArgs: [],

// If sauceUser and sauceKey are specified, seleniumServerJar will be ignored.
// The tests will be run remotely using SauceLabs.
sauceUser: null,
sauceKey: null,

// ----- What tests to run -----
//
// Spec patterns are relative to the location of this config.
specs: [
'./specs/*.js'
],

// ----- Capabilities to be passed to the webdriver instance ----
//
// For a full list of available capabilities, see
// https://code.google.com/p/selenium/wiki/DesiredCapabilities
// and
// https://code.google.com/p/selenium/source/browse/javascript/webdriver/capabilities.js
capabilities: {
browserName: 'chrome',
logName: 'Chrome - English',
version: '',
platform: 'ANY',
shardTestFiles: true,
maxInstances: 2,
},

// A base URL for your application under test. Calls to protractor.get()
// with relative paths will be prepended with this.
baseUrl: 'http://localhost:9999',

// Set the framework
framework: 'jasmine',

// Selector for the element housing the angular app - this defaults to
// body, but is necessary if ng-app is on a descendant of <body>
rootElement: 'body',

onPrepare: function () {
// Add a screenshot reporter:
jasmine.getEnv().addReporter(new HtmlReporter({
preserveDirectory: true,
takeScreenShotsOnlyForFailedSpecs: true,
screenshotsSubfolder: 'images',
jsonsSubfolder: 'jsons',
baseDirectory: 'reports-tmp',
pathBuilder: function pathBuilder(spec, descriptions, results, capabilities) {
// Return '<30-12-2016>/<browser>/<specname>' as path for screenshots:
// Example: '30-12-2016/firefox/list-should work'.
var currentDate = new Date(),
day = currentDate.getDate(),
month = currentDate.getMonth() + 1,
year = currentDate.getFullYear();

var validDescriptions = descriptions.map(function (description) {
return description.replace('/', '@');
});

return path.join(
day + "-" + month + "-" + year,
// capabilities.get('browserName'),
validDescriptions.join('-'));
}
}).getJasmine2Reporter());
},

jasmineNodeOpts: {
// If true, print colors to the terminal.
showColors: true,
// Default time to wait in ms before a test fails.
defaultTimeoutInterval: 30000,
}
};

140 changes: 140 additions & 0 deletions examples/protractor.jasmine2.with.sharding.sort.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
var HtmlReporter = require('protractor-beautiful-reporter');
var path = require('path');

// ----- Config example for Jasmine 2 -----

exports.config = {
// ----- How to setup Selenium -----
//
// There are three ways to specify how to use Selenium. Specify one of the
// following:
//
// 1. seleniumServerJar - to start Selenium Standalone locally.
// 2. seleniumAddress - to connect to a Selenium server which is already
// running.
// 3. sauceUser/sauceKey - to use remote Selenium servers via SauceLabs.

// The location of the selenium standalone server .jar file.
//seleniumServerJar: 'node_modules/protractor/selenium/selenium-server-standalone-2.37.0.jar',

// Address of selenium server (before running protractor, run "webdriver-manager start" to start the Selenium server)
seleniumAddress: 'http://localhost:4444/wd/hub',

// The port to start the selenium server on, or null if the server should
// find its own unused port.
seleniumPort: null,

// Chromedriver location is used to help the selenium standalone server
// find chromedriver. This will be passed to the selenium jar as
// the system property webdriver.chrome.driver. If null, selenium will
// attempt to find chromedriver using PATH.
//chromeDriver: 'node_modules/protractor/selenium/chromedriver',

// Additional command line options to pass to selenium. For example,
// if you need to change the browser timeout, use
// seleniumArgs: ['-browserTimeout=60'],
seleniumArgs: [],

// If sauceUser and sauceKey are specified, seleniumServerJar will be ignored.
// The tests will be run remotely using SauceLabs.
sauceUser: null,
sauceKey: null,

// ----- What tests to run -----
//
// Spec patterns are relative to the location of this config.
specs: [
'./specs/*.js'
],

// ----- Capabilities to be passed to the webdriver instance ----
//
// For a full list of available capabilities, see
// https://code.google.com/p/selenium/wiki/DesiredCapabilities
// and
// https://code.google.com/p/selenium/source/browse/javascript/webdriver/capabilities.js
capabilities: {
browserName: 'chrome',
logName: 'Chrome - English',
version: '',
platform: 'ANY',
shardTestFiles: true,
maxInstances: 2,
},

// A base URL for your application under test. Calls to protractor.get()
// with relative paths will be prepended with this.
baseUrl: 'http://localhost:9999',

// Set the framework
framework: 'jasmine',

// Selector for the element housing the angular app - this defaults to
// body, but is necessary if ng-app is on a descendant of <body>
rootElement: 'body',

onPrepare: function () {
// Add a screenshot reporter:
jasmine.getEnv().addReporter(new HtmlReporter({
preserveDirectory: true,
takeScreenShotsOnlyForFailedSpecs: true,
screenshotsSubfolder: 'images',
jsonsSubfolder: 'jsons',
baseDirectory: 'reports-tmp',
pathBuilder: function pathBuilder(spec, descriptions, results, capabilities) {
// Return '<30-12-2016>/<browser>/<specname>' as path for screenshots:
// Example: '30-12-2016/firefox/list-should work'.
var currentDate = new Date(),
day = currentDate.getDate(),
month = currentDate.getMonth() + 1,
year = currentDate.getFullYear();

var validDescriptions = descriptions.map(function (description) {
return description.replace('/', '@');
});

return path.join(
day + "-" + month + "-" + year,
// capabilities.get('browserName'),
validDescriptions.join('-'));
},
sortFunction: function sortFunction(a, b) {
if (a.cachedBase === undefined) {
var aTemp = a.description.split('|').reverse();
a.cachedBase = aTemp.slice(0).slice(0,-1);
a.cachedName = aTemp.slice(0).join('');
};
if (b.cachedBase === undefined) {
var bTemp = b.description.split('|').reverse();
b.cachedBase = bTemp.slice(0).slice(0,-1);
b.cachedName = bTemp.slice(0).join('');
};

var firstBase = a.cachedBase;
var secondBase = b.cachedBase;

for (var i = 0; i < firstBase.length || i < secondBase.length; i++) {

if (firstBase[i] === undefined) { return -1; }
if (secondBase[i] === undefined) { return 1; }
if (firstBase[i].localeCompare(secondBase[i]) === 0) { continue; }
return firstBase[i].localeCompare(secondBase[i]);
}

var firstTimestamp = a.timestamp;
var secondTimestamp = b.timestamp;

if(firstTimestamp < secondTimestamp) return -1;
else return 1;
}
}).getJasmine2Reporter());
},

jasmineNodeOpts: {
// If true, print colors to the terminal.
showColors: true,
// Default time to wait in ms before a test fails.
defaultTimeoutInterval: 30000,
}
};

Loading

0 comments on commit a944813

Please sign in to comment.