-
Notifications
You must be signed in to change notification settings - Fork 30
/
casper-helpers.js
79 lines (70 loc) · 2.36 KB
/
casper-helpers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
casper.options.viewportSize = {
width : 1280,
height : 800
};
casper.on('page.initialized', function( page ) {
this.evaluate(function() {
var isFunction = function(o) {
return typeof o == 'function';
},
bind,
slice = [].slice,
proto = Function.prototype,
featureMap = {
'function-bind': 'bind'
};
function has(feature) {
var prop = featureMap[feature];
return isFunction(proto[prop]);
}
// check for missing features
if (!has('function-bind')) {
// adapted from Mozilla Developer Network example at
// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind
bind = function bind(obj) {
var args = slice.call(arguments, 1),
self = this,
nop = function() {},
bound = function() {
return self.apply(this instanceof nop ? this : (obj || {}), args.concat(slice.call(arguments)));
};
nop.prototype = this.prototype || {}; // Firefox cries sometimes if prototype is undefined
bound.prototype = new nop();
return bound;
};
proto.bind = bind;
}
});
});
var stamp = (function () {
var i = 0;
return function () {
return ++i;
};
})();
var template = function (input, variables) {
for (var i = 0, len = variables.length; i < len; i++) {
var item = variables[i];
input = input.replace(/%[sd]/, item);
}
return input;
};
var newImageFilename = function ( alt ) {
return template('tests/screenshots/casper-%d.png', [alt || stamp()]);
};
var capture = function (alt) {
var filename = newImageFilename( alt );
casper.capture( filename );
casper.test.comment( filename.replace('tests/screenshots/', '') );
};
casper.on("page.error", function(msg, trace) {
if (msg === 'WebGL not supported' ||
msg.match(/^ReferenceError.*?Uint8ClampedArray$/)) {
// we know
return;
}
this.echo("Error: " + msg, "ERROR");
});
casper.test.on("fail", function(failure) {
failure.message = "Message : " + failure.message + "\nLine : " + failure.line + "\nCode : " + failure.lineContents;
});