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

Commit 18db296

Browse files
committed
Full bluebird conversion.
1 parent 75f7e37 commit 18db296

16 files changed

Lines changed: 382 additions & 341 deletions

.eslintrc

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
// "parser": "babel-eslint",
3+
"root": true,
4+
"extends": "eslint:recommended",
5+
"rules": {
6+
"strict": 0,
7+
"quotes": [0, "single"],
8+
"comma-dangle": 0,
9+
"curly": [1, "multi-line"],
10+
"no-console": 0,
11+
"no-use-before-define": [1, "nofunc"],
12+
"no-unused-vars": [1, {
13+
"vars": "local",
14+
"varsIgnorePattern": "^_"
15+
}],
16+
"no-underscore-dangle": 0,
17+
"new-cap": 0,
18+
"consistent-return": 0,
19+
"camelcase": 0,
20+
"dot-notation": 0,
21+
"semi": [1, "always"],
22+
},
23+
env: {
24+
// "es6": true,
25+
"node": true
26+
}
27+
}

lib/asyncCallback.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,20 @@
44
* run workflow again or execute callback function
55
*/
66

7-
var workflow = require('./workflow.js');
87
var endSession = require('./endSession.js');
98
var Promise = require('bluebird');
109

1110
module.exports = function(err) {
11+
var workflow = require('./workflow');
12+
var ctx = this;
13+
1214
/**
1315
* if error occured don't do another shot (if multiple screen width are set)
1416
*/
1517
/*istanbul ignore next*/
1618
if (err) return Promise.reject(err);
1719
return Promise.try(function() {
1820

19-
var ctx = this;
2021

2122
/**
2223
* on multiple screenWidth or multiple page elements

lib/compareImages.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
*/
66

77
var resemble = require('node-resemble-js');
8+
var Promise = require('bluebird');
89

910
module.exports = function() {
1011

12+
var ctx = this;
1113
/**
1214
* need to find done function because gm doesn't have node like callbacks (err,res)
1315
*/
@@ -17,19 +19,19 @@ module.exports = function() {
1719
/**
1820
* if there is no need for image comparison or no images gets saved on fs, just continue
1921
*/
20-
if(!this.isComparable || !this.self.saveImages) {
22+
if(!ctx.isComparable || !ctx.self.saveImages) {
2123
return;
2224
}
2325

2426
/**
2527
* compare images
2628
*/
27-
var diff = resemble(this.baselinePath).compareTo(this.regressionPath);
29+
var diff = resemble(ctx.baselinePath).compareTo(ctx.regressionPath);
2830

2931
/**
3032
* map 'ignore' configuration to resemble options
3133
*/
32-
var ignore = this.currentArgs.ignore || "";
34+
var ignore = ctx.currentArgs.ignore || "";
3335
if (ignore.indexOf("color") === 0) {
3436
diff.ignoreColors();
3537
} else if (ignore.indexOf("antialias") === 0) {
@@ -39,8 +41,10 @@ module.exports = function() {
3941
/**
4042
* execute the comparison
4143
*/
42-
return Promise.fromCallback(function(cb) {
43-
diff.onComplete(cb.bind(null,null));
44+
return new Promise(function(resolve, reject) {
45+
diff.onComplete(function(res) { // this callback has bad arity
46+
resolve(res);
47+
});
4448
});
4549
})
4650
.nodeify(done);

lib/cropImage.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ module.exports = function(res, done) {
1414

1515
var ctx = this;
1616
var excludeRect = res.excludeRect;
17-
var shot = gm(this.screenshot).quality(100);
17+
var shot = gm(ctx.screenshot).quality(100);
1818
var cropDim;
1919

2020
return Promise.try(function() {
2121

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);
22+
var x = parseInt(ctx.currentArgs.x, 10);
23+
var y = parseInt(ctx.currentArgs.y, 10);
24+
var width = parseInt(ctx.currentArgs.width, 10);
25+
var height = parseInt(ctx.currentArgs.height, 10);
2626

2727
if (!isNaN(x) && !isNaN(y) && !isNaN(width) && !isNaN(height)) {
2828

@@ -59,12 +59,13 @@ module.exports = function(res, done) {
5959
}
6060
})
6161
.then(function() {
62-
if (!ctx.self.saveImages) return;
63-
64-
/**
65-
* save image to fs
66-
*/
67-
return shot.writeAsync(ctx.filename || ctx.baselinePath)
62+
return Promise.try(function() {
63+
if (!ctx.self.saveImages) return;
64+
/**
65+
* save image to fs
66+
*/
67+
return shot.writeAsync(ctx.filename || ctx.baselinePath);
68+
})
6869
.then(function() {
6970
/**
7071
* generate image buffer

lib/documentScreenshot.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ module.exports = function documentScreenshot(fileName, cb) {
8383
function takeScreenshot() {
8484

8585
// Take screenshot
86-
return self.screenshot.bind(self)
86+
return self.screenshot.call(self)
8787
// Cache image into tmp dir
8888
.then(function cacheImage(res) {
8989
var file = tmpDir + '/' + currentXPos + '-' + currentYPos + '.png';
@@ -162,11 +162,9 @@ module.exports = function documentScreenshot(fileName, cb) {
162162
* crop screenshot regarding page size
163163
*/
164164
.then(function cropScreenshot() {
165-
return Promise.fromCallback(function(cb) {
166-
gm(fileName)
167-
.crop(response.execute[0].value.documentWidth, response.execute[0].value.documentHeight, 0, 0)
168-
.write(fileName, cb);
169-
});
165+
return gm(fileName)
166+
.crop(response.execute[0].value.documentWidth, response.execute[0].value.documentHeight, 0, 0)
167+
.writeAsync(fileName);
170168
})
171169
/*!
172170
* remove tmp dir

lib/endSession.js

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,46 +6,47 @@ var request = Promise.promisify(require('request'), {multiArgs: true});
66

77
module.exports = function(done) {
88

9-
var that = this;
9+
var ctx = this;
1010

1111
return Promise.try(function setOriginalResolution() {
1212
/**
1313
* if screenwidth was set, get back to old resolution
1414
*/
15-
if (!that.self.defaultScreenDimension) return;
15+
if (!ctx.self.defaultScreenDimension) return;
1616

17-
return that.instance.windowHandleSize({
18-
width: that.self.defaultScreenDimension.width,
19-
height: that.self.defaultScreenDimension.height
17+
return ctx.instance.windowHandleSize({
18+
width: ctx.self.defaultScreenDimension.width,
19+
height: ctx.self.defaultScreenDimension.height
2020
});
2121
})
2222
/**
2323
* If we have an applitools session open, close it
2424
*/
2525
.then(function closeApplitools() {
26-
if (!that.self.usesApplitools) return;
26+
if (!ctx.self.usesApplitools) return;
2727

2828
// Whether or not we should automatically save this test as baseline.
29-
var updateBaseline = (that.self.isNew && that.applitools.saveNewTests) ||
30-
(!that.self.isNew && that.applitools.saveFailedTests);
29+
var updateBaseline = (ctx.self.isNew && ctx.applitools.saveNewTests) ||
30+
(!ctx.self.isNew && ctx.applitools.saveFailedTests);
3131

3232
return request({
33-
qs: {apiKey: that.applitools.apiKey, updateBaseline: updateBaseline},
34-
url: that.self.host + '/api/sessions/running/' + that.self.sessionId,
33+
qs: {apiKey: ctx.applitools.apiKey, updateBaseline: updateBaseline},
34+
url: ctx.self.host + '/api/sessions/running/' + ctx.self.sessionId,
3535
method: 'DELETE',
36-
headers: that.self.headers,
37-
timeout: that.self.reqTimeout
36+
headers: ctx.self.headers,
37+
timeout: ctx.self.reqTimeout
38+
})
39+
.spread(function clearAndStore(res, body) {
40+
if (!body) return;
41+
42+
ctx.self.resultObject[ctx.currentArgs.name] = merge({
43+
id: ctx.self.sessionId,
44+
url: ctx.self.url
45+
}, JSON.parse(body));
46+
ctx.self.url = undefined;
47+
ctx.self.sessionId = undefined;
48+
ctx.self.isNew = undefined;
3849
});
3950
})
40-
.spread(function clearAndStore(res, body) {
41-
if (!body) return;
42-
43-
that.self.resultObject[that.currentArgs.name] = merge({
44-
id: that.self.sessionId,
45-
url: that.self.url
46-
}, JSON.parse(body));
47-
that.self.url = undefined;
48-
that.self.sessionId = undefined;
49-
that.self.isNew = undefined;
50-
}).nodeify(done);
51+
.nodeify(done);
5152
};

0 commit comments

Comments
 (0)