Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Run plans and states sequentially

  • Loading branch information...
commit 8f8da7d7c483ca89301add37737009442b772572 1 parent fe5aa64
@SevInf SevInf authored
View
2  lib/cli/test.js
@@ -34,7 +34,7 @@ module.exports = function() {
.spread(function(config, plans) {
var tester = new Tester(config);
reporter(tester);
- return tester.runTests(plans);
+ return tester.runPlans(plans);
});
});
View
16 lib/promise-util.js
@@ -0,0 +1,16 @@
+'use strict';
+var q = require('q');
+
+exports.sequence = function sequence(funcs) {
+ return funcs.reduce(function(promise, func) {
+ return promise.then(func);
+ }, q());
+};
+
+exports.seqMap = function seqMap(array, callback) {
+ return exports.sequence(array.map(function(elem) {
+ return function() {
+ return callback(elem);
+ };
+ }));
+};
View
52 lib/runner.js
@@ -0,0 +1,52 @@
+'use strict';
+var EventEmitter = require('events').EventEmitter,
+
+ q = require('q'),
+ inherit = require('inherit'),
+ promiseUtils = require('./promise-util');
+
+module.exports = inherit(EventEmitter, {
+
+ __constructor: function(config) {
+ this.config = config;
+ },
+
+ _beforeAll: function() {
+ return q.resolve();
+ },
+
+ _afterAll: function() {
+ return q.resolve();
+ },
+
+ runPlans: function(plans) {
+ var _this = this;
+ return this._beforeAll()
+ .then(function() {
+ return promiseUtils.seqMap(plans, _this.runPlan.bind(_this));
+ })
+ .then(this._afterAll.bind(this));
+ },
+
+ runPlan: function(plan) {
+ var states = plan.getStates(),
+ runState = this._runState.bind(this, plan);
+ return this._beforePlan(plan)
+ .then(function() {
+ return promiseUtils.seqMap(states, runState);
+ })
+ .then(this._afterPlan.bind(this, plan));
+ },
+
+ _beforePlan: function(plan) {
+ return q.resolve();
+ },
+
+ _afterPlan: function(plan) {
+ return q.resolve();
+ },
+
+ _runState: function(plan, state) {
+ return q.resolve();
+ }
+});
View
52 lib/screen-shooter.js
@@ -1,46 +1,22 @@
'use strict';
-var EventEmitter = require('events').EventEmitter,
- q = require('q'),
- fs = require('q-io/fs'),
+var fs = require('q-io/fs'),
inherit = require('inherit'),
Browser = require('./browser'),
+ Runner = require('./runner'),
elementRect = require('./element-rect');
-module.exports = inherit(EventEmitter, {
- __constructor: function (config) {
- this.config = config;
- },
+module.exports = inherit(Runner, {
- runPlans: function(plans) {
- return q.all(plans.map(function(plan) {
- return this.runPlan(plan);
- }, this));
+ _beforePlan: function(plan) {
+ return fs.makeTree(this.config.getScreenshotsDir(plan.name));
},
- runPlan: function(plan) {
- var _this = this;
- return fs.makeTree(this.config.getScreenshotsDir(plan.name))
- .then(function() {
- return q.all(plan.getStates().map(function(state) {
- return _this._captureState(plan, state)
- .then(function(savePath) {
- _this.emit('endCapture', {
- name: plan.name,
- state: state,
- path: savePath
- });
- });
- }));
- });
- },
-
- _captureState: function(plan, state) {
+ _runState: function(plan, state) {
var _this = this,
browser = new Browser();
- this.emit('beginCapture', plan.name, state);
-
+ this.emit('beginCapture', plan, state);
return browser.open(this.config.getAbsoluteUrl(plan.url))
.then(function() {
return browser.findElement(plan.selector);
@@ -67,9 +43,17 @@ module.exports = inherit(EventEmitter, {
return browser.quit().then(function() {
return savePath;
});
- });
-
- }
+ })
+ .then(function(savePath) {
+ var data = {
+ name: plan.name,
+ state: state,
+ path: savePath
+ };
+ _this.emit('endCapture', data);
+ return data;
+ });
+ },
});
View
83 lib/tester.js
@@ -1,68 +1,51 @@
'use strict';
-var EventEmitter = require('events').EventEmitter,
-
- inherit = require('inherit'),
+var inherit = require('inherit'),
q = require('q'),
temp = require('temp'),
+ Runner = require('./runner'),
ScreenShooter = require('./screen-shooter'),
Image = require('./image');
-module.exports = inherit(EventEmitter, {
-
- __constructor: function(config) {
- this.config = config;
- },
+module.exports = inherit(Runner, {
- runTests: function(plans) {
+ _beforeAll: function() {
var _this = this;
- this.emit('beginTests');
- return q.all(plans.map(this.runTest.bind(this)))
- .then(function() {
- _this.emit('endTests');
+ return this._getScreenShooterConfig()
+ .then(function(config) {
+ _this._shooter = new ScreenShooter(config);
+ _this.emit('beginTests');
+ return _this._shooter._beforeAll();
});
},
- runTest: function(plan) {
- var _this = this;
+ _afterAll: function() {
+ this.emit('endTests');
+ return this._shooter._afterAll();
+ },
+
+ _beforePlan: function(plan) {
this.emit('beginPlan', plan.name);
- return this._getScreenShooterConfig()
- .then(function(config) {
- return new ScreenShooter(config);
- })
- .then(function(shooter) {
- shooter.on('beginCapture', function(name, state) {
- _this.emit('beginTest', state);
- });
+ return this._shooter._beforePlan(plan);
+ },
- shooter.on('endCapture', _this._compareScreenshots.bind(_this));
- return shooter.runPlan(plan);
- })
- .then(function() {
- _this.emit('endPlan', plan.name);
- });
+ _afterPlan: function(plan) {
+ this.emit('endPlan', plan.name);
+ return this._shooter._afterPlan(plan);
},
- _getScreenShooterConfig: function() {
- var d = q.defer(),
- _this = this;
- temp.mkdir('shooter', function(error, dir) {
- if (error) {
- return d.reject(error);
- }
- var config = Object.create(_this.config);
- config.screenshotsDir = dir;
- d.resolve(config);
- });
- return d.promise;
+ _runState: function(plan, state) {
+ this.emit('beginTest', state);
+ return this._shooter._runState(plan, state)
+ .then(this._compareScreenshots.bind(this));
},
_compareScreenshots: function (captureData) {
var prevPath = this.config.getScreenshotPath(captureData.name,
captureData.state),
_this = this;
- Image.compare(captureData.path, prevPath)
+ return Image.compare(captureData.path, prevPath)
.then(function(isEqual) {
_this.emit('endTest', {
name: captureData.name,
@@ -72,6 +55,20 @@ module.exports = inherit(EventEmitter, {
currentPath: captureData.path
});
});
- }
+ },
+ _getScreenShooterConfig: function() {
+ var d = q.defer(),
+ _this = this;
+ temp.mkdir('shooter', function(error, dir) {
+ if (error) {
+ return d.reject(error);
+ }
+ var config = Object.create(_this.config);
+ config.screenshotsDir = dir;
+ d.resolve(config);
+ });
+ return d.promise;
+ }
});
+
Please sign in to comment.
Something went wrong with that request. Please try again.