capture.js
3.01 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*eslint strict:0*/
var fs = require('fs');
casper.test.begin('Casper.capture() tests', 1, {
testFile: '/tmp/__casper_test_capture.png',
setUp: function(test) {
if (fs.exists(this.testFile) && fs.isFile(this.testFile)) {
try {
fs.remove(this.testFile);
} catch (e) {
}
}
},
tearDown: function(test) {
try {
fs.remove(this.testFile);
} catch(e) {
}
},
test: function(test) {
var self = this;
casper.start('tests/site/index.html', function() {
this.viewport(300, 200);
this.capture(self.testFile);
test.assert(fs.isFile(self.testFile), 'Casper.capture() captured a screenshot');
});
casper.run(function() {
test.done();
});
}
});
casper.test.begin('Casper.captureSelector() tests', 2, {
testFile: '/tmp/__casper_test_capture.png',
setUp: function(test) {
if (fs.exists(this.testFile) && fs.isFile(this.testFile)) {
try {
fs.remove(this.testFile);
} catch (e) {
}
}
},
tearDown: function(test) {
try {
fs.remove(this.testFile);
} catch(e) {
}
},
test: function(test) {
var self = this;
casper.start('tests/site/index.html', function() {
this.viewport(300, 200);
this.captureSelector(self.testFile, 'ul');
test.assert(fs.isFile(self.testFile),
'Casper.captureSelector() captured from a selector');
var sizeBeforeScroll = fs.size(self.testFile);
this.scrollTo(10, 20);
this.captureSelector(self.testFile, 'ul');
var sizeAfterScroll = fs.size(self.testFile);
test.assert(sizeBeforeScroll === sizeAfterScroll,
'Casper.captureSelector() captured from a selector after scrolling');
});
casper.run(function() {
test.done();
});
}
});
casper.test.begin('Casper.captureBase64() tests', 4, function(test) {
casper.start('tests/site/index.html', function() {
test.assert(this.captureBase64('png').length > 0,
'Casper.captureBase64() rendered a page capture as base64');
test.assert(this.captureBase64('png', 'ul').length > 0,
'Casper.captureBase64() rendered a capture from a selector as base64');
test.assert(this.captureBase64('png', {top: 0, left: 0, width: 30, height: 30}).length > 0,
'Casper.captureBase64() rendered a capture from a clipRect as base64');
this.viewport(300, 200);
var sizeBeforeScroll = this.captureBase64('png', 'ul').length;
this.scrollTo(10, 20);
var sizeAfterScroll = this.captureBase64('png', 'ul').length;
test.assert(sizeBeforeScroll === sizeAfterScroll,
'Casper.captureBase64() rendered from a selector must be the same before and after scrolling');
}).run(function() {
test.done();
});
});