Skip to content

Commit

Permalink
Bracco 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
mantovanig committed Jan 16, 2017
0 parents commit d063d61
Show file tree
Hide file tree
Showing 8 changed files with 1,239 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
135 changes: 135 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
# BRACCO

Bracco is a simple runner of [BackstopJS](https://github.com/garris/BackstopJS) for visual regression testing.

You can easly **split, manage and run your scenarios**

## Methods

### **init(conf[Object])**

Initialize the runner.

Arguments: the conf object used in your scenario

`conf[Object]`
```javascript
{
testhost: "https://www.amazon.it/",
refhost: "https://www.amazon.co.uk/",
delay: 1000,
misMatchThreshold: 10
}
```

### **reference(options[Object], test[Boolean])**

Reference task of backstopJS.

`options[Object]`
```javascript
{
scenario: string,
tags: [array]
}
```

The second argument test is a bool flag if you want run test task after the reference task is finished.


### **test(options[Object])**
Test task of backstopJS.

`options[Object]`
```javascript
{
scenario: string,
tags: [array]
}
```

## Files structure
Create in the root of your project a folder named `scenarios` and create subfolder for each group of scenario.

Example:

```
casper_sripts
│ onBefore.js
│ onReady.js
└───footer
│ │ onBefore.js
│ │ onReady.js
scenarios
└───header
│ │ headerAll.js
│ │ headerMenu.js
│ │ headerSearch.js
│ │ ...
└───footer
│ footerAll.js
│ footerSocial.js
│ ...
```


## Basic Usage

`index.js`
```javascript
const bracco = require('bracco');

bracco.init({
testhost: "https://www.amazon.it/",
refhost: "https://www.amazon.co.uk/"
});

bracco.reference({
scenario: 'footer'
});

bracco.test({
scenario: 'footer'
});
```



`scenarios/footer/footerAll.js`
```javascript
module.exports =
(conf) => {
return [{
"label": "FooterAll",
"tags": ["common"],
"referenceUrl": conf.refhost,
"url": conf.testhost,
"removeSelectors": [
'#main'
],
"selectors": [
"#siteFooter"
],
"onBeforeScript": "footer/onBefore.js",
"onReadyScript": "footer/onReady.js"
}]
};
```



`casper_scripts/footer/onReady.js`
```javascript
module.exports = function (casper, scenario, vp) {

casper.waitForSelector('footer', function() {
this.scrollToBottom();
});

console.log('onReady.js has run for: ', vp.name);
};
```
6 changes: 6 additions & 0 deletions conf/commonSettings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
testhost: "",
refhost: "",
delay: 500,
misMatchThreshold: 10
}
22 changes: 22 additions & 0 deletions conf/defaultConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module.exports = {
"id": "prod_test",
"viewports": [
{
"name": "xlarge",
"width": 1440,
"height": 900
}
],
"scenarios": [],
"paths": {
"bitmaps_reference": "backstop_data/bitmaps_reference",
"bitmaps_test": "backstop_data/bitmaps_test",
"casper_scripts": "casper_scripts",
"html_report": "backstop_data/html_report",
"ci_report": "backstop_data/ci_report"
},
"casperFlags": [],
"engine": "phantomjs",
"report": ["browser"],
"debug": true
}
16 changes: 16 additions & 0 deletions conf/defaultScenario.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const conf = require('./commonSettings');

module.exports = {
"label": "ScenarioTitle",
"tags": [],
"referenceUrl": conf.refhost,
"url": conf.testhost,
"hideSelectors": [],
"removeSelectors": [],
"selectors": [],
"readyEvent": null,
"delay": conf.delay,
"misMatchThreshold" : conf.misMatchThreshold,
"onBeforeScript": "onBefore.js",
"onReadyScript": "onReady.js"
}
108 changes: 108 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
"use strict";

// libs
const globby = require('globby');
const _ = require('lodash');
const backstop = require('backstopjs');
const path = require('path');
const jsonfile = require('jsonfile');

// settings
const defaultSettings = require('./conf/commonSettings');
const defaultScenario = require('./conf/defaultScenario');
const defaultConfig = require('./conf/defaultConfig');


const bracco = {

init(settings) {
//setup vars (testhost, refhost, settings..)
this.conf = Object.assign({}, defaultSettings, settings);
},

buildConfig(options) {
let scenarios = [];
let tags = [];
let conf = this.conf;

// read tags option
if(Array.isArray(options.tags)) {

if(options.tags.length > 0) {
tags = tags.concat(options.tags);
}

} else if(options.tags !== '' && options.tags) {
tags.push(options.tags);
}

// build path for globby
let root = path.normalize(process.cwd());
let pathScenario = (options.hasOwnProperty('scenario')) ? root + '/scenarios/' + options.scenario + '/*.js' : root + '/scenarios/**/*.js';

// get scenarios from js files
globby.sync([pathScenario]).map(e => {
let getScenario = require(e);
let newScenarios = [];

if(tags.length > 0) {

getScenario(conf)
.filter(scenario => scenario.hasOwnProperty('tags'))
.map(scenario => {

scenario.tags.map(cTag => {
let match = _.findIndex(tags, o => o === cTag );
if(match !== -1) {
newScenarios.push(Object.assign({}, defaultScenario, scenario));
}
});

});

} else {

newScenarios = getScenario(conf).map(scenario => Object.assign({}, defaultScenario, scenario));

}

scenarios = scenarios.concat(newScenarios);
});

// merge with default config of backstop
let config = Object.assign({}, defaultConfig);

// add scenario to config
config.scenarios = config.scenarios.concat(scenarios);

// write config in dafault backstop json
let jsonFile = root + '/backstop.json';
jsonfile.writeFileSync(jsonFile, config);
},

reference(options, test) {

this.buildConfig(options);

backstop('reference')
.then(() => {
if(test) {
backstop('test');
}
}).catch((err) => {
console.log("Error: ", err);
});

},

test(options) {

this.buildConfig(options);

backstop('test');
}

};


module.exports = bracco;
15 changes: 15 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "bracco",
"version": "1.0.0",
"description": "Simple runner based on backstopJs for Visual Regression Test",
"main": "index.js",
"repository": "https://gitlab.com/mantovanig/bracco.git",
"author": "mantovanig",
"license": "MIT",
"dependencies": {
"backstopjs": "^2.3.5",
"globby": "^6.1.0",
"jsonfile": "^2.4.0",
"lodash": "^4.17.3"
}
}
Loading

0 comments on commit d063d61

Please sign in to comment.