Skip to content
This repository was archived by the owner on Dec 18, 2019. It is now read-only.

Commit 75f7e37

Browse files
committed
Convert to bluebird promises.
1 parent 3f7497d commit 75f7e37

17 files changed

Lines changed: 853 additions & 1017 deletions

lib/asyncCallback.js

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,53 +4,56 @@
44
* run workflow again or execute callback function
55
*/
66

7-
var workflow = require('./workflow.js').workflow,
8-
endSession = require('./endSession.js');
7+
var workflow = require('./workflow.js');
8+
var endSession = require('./endSession.js');
9+
var Promise = require('bluebird');
910

1011
module.exports = function(err) {
11-
12-
var that = this;
13-
1412
/**
1513
* if error occured don't do another shot (if multiple screen width are set)
1614
*/
1715
/*istanbul ignore next*/
18-
if(err) {
19-
return this.cb(err);
20-
}
16+
if (err) return Promise.reject(err);
17+
return Promise.try(function() {
2118

22-
/**
23-
* on multiple screenWidth or multiple page elements
24-
* repeat workflow
25-
*/
26-
if(this.screenWidth && this.screenWidth.length) {
19+
var ctx = this;
2720

2821
/**
29-
* if multiple screen widths are given
30-
* start workflow all over again with same parameter
22+
* on multiple screenWidth or multiple page elements
23+
* repeat workflow
3124
*/
32-
this.queuedShots[0].screenWidth = this.screenWidth;
33-
return workflow.call(this.self, this.pagename, this.queuedShots, this.cb);
25+
if(ctx.screenWidth && ctx.screenWidth.length) {
26+
27+
/**
28+
* if multiple screen widths are given
29+
* start workflow all over again with same parameter
30+
*/
31+
ctx.queuedShots[0].screenWidth = ctx.screenWidth;
32+
return workflow.call(ctx.self, ctx.pagename, ctx.queuedShots);
33+
34+
} else if (ctx.queuedShots.length > 1) {
3435

35-
} else if (this.queuedShots.length > 1) {
36+
/**
37+
* if multiple page modules are given
38+
*/
39+
return endSession.call(ctx)
40+
.then(function() {
41+
ctx.queuedShots.shift();
42+
return workflow.call(ctx.self, ctx.pagename, ctx.queuedShots, ctx.cb);
43+
});
44+
45+
}
3646

3747
/**
38-
* if multiple page modules are given
48+
* finish command
3949
*/
40-
return endSession.call(this, function() {
41-
that.queuedShots.shift();
42-
return workflow.call(that.self, that.pagename, that.queuedShots, that.cb);
50+
return endSession.call(ctx)
51+
.then(function() {
52+
ctx.self.takeScreenshot = undefined;
53+
return ctx.self.resultObject;
54+
})
55+
.finally(function() {
56+
ctx.self.resultObject = {};
4357
});
44-
45-
}
46-
47-
/**
48-
* finish command
49-
*/
50-
return endSession.call(this, function(err) {
51-
that.self.takeScreenshot = undefined;
52-
that.cb(err, that.self.resultObject);
53-
that.self.resultObject = {};
5458
});
55-
5659
};

lib/compareImages.js

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,35 @@ module.exports = function() {
1313
*/
1414
var done = arguments[arguments.length - 1];
1515

16-
/**
17-
* if there is no need for image comparison or no images gets saved on fs, just continue
18-
*/
19-
if(!this.isComparable || !this.self.saveImages) {
20-
return done();
21-
}
22-
23-
/**
24-
* compare images
25-
*/
26-
var diff = resemble(this.baselinePath).compareTo(this.regressionPath);
27-
28-
/**
29-
* map 'ignore' configuration to resemble options
30-
*/
31-
var ignore = this.currentArgs.ignore || "";
32-
if (ignore.indexOf("color") === 0) {
33-
diff.ignoreColors();
34-
} else if (ignore.indexOf("antialias") === 0) {
35-
diff.ignoreAntialiasing();
36-
}
37-
38-
/**
39-
* execute the comparison
40-
*/
41-
diff.onComplete(done.bind(null,null));
16+
return Promise.try(function() {
17+
/**
18+
* if there is no need for image comparison or no images gets saved on fs, just continue
19+
*/
20+
if(!this.isComparable || !this.self.saveImages) {
21+
return;
22+
}
23+
24+
/**
25+
* compare images
26+
*/
27+
var diff = resemble(this.baselinePath).compareTo(this.regressionPath);
28+
29+
/**
30+
* map 'ignore' configuration to resemble options
31+
*/
32+
var ignore = this.currentArgs.ignore || "";
33+
if (ignore.indexOf("color") === 0) {
34+
diff.ignoreColors();
35+
} else if (ignore.indexOf("antialias") === 0) {
36+
diff.ignoreAntialiasing();
37+
}
38+
39+
/**
40+
* execute the comparison
41+
*/
42+
return Promise.fromCallback(function(cb) {
43+
diff.onComplete(cb.bind(null,null));
44+
});
45+
})
46+
.nodeify(done);
4247
};

lib/cropImage.js

Lines changed: 64 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -4,99 +4,93 @@
44
* crop image according to user arguments and its position on screen and save it
55
*/
66

7-
var gm = require('gm'),
8-
async = require('async'),
9-
request = require('request'),
10-
exclude = require('./exclude.js');
7+
var Promise = require('bluebird');
8+
var gm = require('gm');
9+
Promise.promisifyAll(gm.prototype);
10+
var request = Promise.promisify(require('request'), {multiArg: true});
11+
var exclude = require('./exclude.js');
1112

1213
module.exports = function(res, done) {
1314

14-
var that = this,
15-
excludeRect = res.excludeRect,
16-
shot = gm(this.screenshot).quality(100),
17-
cropDim;
15+
var ctx = this;
16+
var excludeRect = res.excludeRect;
17+
var shot = gm(this.screenshot).quality(100);
18+
var cropDim;
1819

19-
var x = parseInt(this.currentArgs.x, 10);
20-
var y = parseInt(this.currentArgs.y, 10);
21-
var width = parseInt(this.currentArgs.width, 10);
22-
var height = parseInt(this.currentArgs.height, 10);
20+
return Promise.try(function() {
2321

24-
if (!isNaN(x) && !isNaN(y) && !isNaN(width) && !isNaN(height)) {
22+
var x = parseInt(this.currentArgs.x, 10);
23+
var y = parseInt(this.currentArgs.y, 10);
24+
var width = parseInt(this.currentArgs.width, 10);
25+
var height = parseInt(this.currentArgs.height, 10);
2526

26-
/**
27-
* crop image with given arguments
28-
*/
29-
cropDim = {
30-
x: x - res.scrollPos.x,
31-
y: y - res.scrollPos.y,
32-
width: width,
33-
height: height
34-
};
27+
if (!isNaN(x) && !isNaN(y) && !isNaN(width) && !isNaN(height)) {
3528

36-
exclude(shot, excludeRect);
37-
shot.crop(cropDim.width, cropDim.height, cropDim.x, cropDim.y);
29+
/**
30+
* crop image with given arguments
31+
*/
32+
cropDim = {
33+
x: x - res.scrollPos.x,
34+
y: y - res.scrollPos.y,
35+
width: width,
36+
height: height
37+
};
3838

39-
} else if (res && res.elemBounding) {
39+
exclude(shot, excludeRect);
40+
shot.crop(cropDim.width, cropDim.height, cropDim.x, cropDim.y);
4041

41-
/**
42-
* or use boundary of specific CSS element
43-
*/
44-
cropDim = {
45-
x: res.elemBounding.left + (res.elemBounding.width / 2),
46-
y: res.elemBounding.top + (res.elemBounding.height / 2),
47-
width: isNaN(width) ? res.elemBounding.width : width,
48-
height: isNaN(height) ? res.elemBounding.height : height
49-
};
42+
} else if (res && res.elemBounding) {
5043

51-
exclude(shot, excludeRect);
52-
shot.crop(cropDim.width, cropDim.height, cropDim.x - (cropDim.width / 2), cropDim.y - (cropDim.height / 2));
44+
/**
45+
* or use boundary of specific CSS element
46+
*/
47+
cropDim = {
48+
x: res.elemBounding.left + (res.elemBounding.width / 2),
49+
y: res.elemBounding.top + (res.elemBounding.height / 2),
50+
width: isNaN(width) ? res.elemBounding.width : width,
51+
height: isNaN(height) ? res.elemBounding.height : height
52+
};
5353

54-
} else {
55-
exclude(shot, excludeRect);
56-
}
54+
exclude(shot, excludeRect);
55+
shot.crop(cropDim.width, cropDim.height, cropDim.x - (cropDim.width / 2), cropDim.y - (cropDim.height / 2));
5756

58-
async.waterfall([
59-
/**
60-
* save image to fs
61-
*/
62-
function(cb) {
63-
if(!that.self.saveImages) {
64-
return cb();
65-
}
57+
} else {
58+
exclude(shot, excludeRect);
59+
}
60+
})
61+
.then(function() {
62+
if (!ctx.self.saveImages) return;
6663

67-
return shot.write(that.filename || that.baselinePath, cb);
68-
},
6964
/**
70-
* generate image buffer
65+
* save image to fs
7166
*/
72-
function() {
73-
var cb = arguments[arguments.length - 1];
74-
return shot.toBuffer('PNG', cb);
75-
},
67+
return shot.writeAsync(ctx.filename || ctx.baselinePath)
68+
.then(function() {
69+
/**
70+
* generate image buffer
71+
*/
72+
return shot.toBufferAsync('PNG');
73+
})
7674
/**
7775
* upload image to applitools
7876
*/
79-
function(buffer) {
80-
var cb = arguments[arguments.length - 1];
81-
if (!that.self.usesApplitools) {
82-
return cb();
83-
}
84-
request({
85-
qs: {apiKey: that.applitools.apiKey},
86-
url: that.self.host + '/api/sessions/running/' + that.self.sessionId,
77+
.then(function(buffer) {
78+
if (!ctx.self.usesApplitools) return;
79+
return request({
80+
qs: {apiKey: ctx.applitools.apiKey},
81+
url: ctx.self.host + '/api/sessions/running/' + ctx.self.sessionId,
8782
method: 'POST',
88-
headers: that.self.headers,
89-
timeout: that.self.reqTimeout,
83+
headers: ctx.self.headers,
84+
timeout: ctx.self.reqTimeout,
9085
json: {
9186
'appOutput': {
9287
'title': res.title,
9388
'screenshot64': new Buffer(buffer).toString('base64')
9489
},
95-
'tag': that.currentArgs.tag || '',
96-
'ignoreMismatch': that.currentArgs.ignoreMismatch || false
90+
'tag': ctx.currentArgs.tag || '',
91+
'ignoreMismatch': ctx.currentArgs.ignoreMismatch || false
9792
}
98-
}, cb);
99-
}
100-
], done);
101-
93+
});
94+
});
95+
}).nodeify(done);
10296
};

0 commit comments

Comments
 (0)