run.js
12.4 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
phantom.injectJs('casper.js');
var casper = new phantom.Casper({
faultTolerant: false,
verbose: true
});
var fs = require('fs');
var save = null;
phantom.args.forEach(function(arg) {
var debugMatch = /--loglevel=(\w+)/.exec(arg);
if (debugMatch) {
casper.options.logLevel = debugMatch[1];
}
var saveMatch = /--save=(.*)(\s|)/.exec(arg);
if (saveMatch) {
save = saveMatch[1];
}
});
// Casper#log()
casper.test.comment('logging');
var oldLevel = casper.options.logLevel;
casper.options.logLevel = 'info';
casper.options.verbose = false;
casper.log('foo', 'info');
casper.test.assert(casper.result.log.some(function(e) {
return e.message === 'foo' && e.level === 'info';
}), 'Casper.log() adds a log entry');
casper.options.logLevel = oldLevel;
casper.options.verbose = true;
// Casper#start()
casper.test.comment('navigating');
casper.start('tests/site/index.html', function(self) {
self.test.assert(self.exists('a') && !self.exists('chucknorriz'), 'Casper.exists() can check if an element exists');
self.test.assertTitle('CasperJS test index', 'Casper.start() casper can start itself an open an url');
self.test.assertEval(function() {
return typeof(__utils__) === "object";
}, 'Casper.start() injects ClientUtils instance within remote DOM');
self.test.comment('fetching');
self.test.assertEquals(self.fetchText('ul li'), 'onetwothree', 'Casper.fetchText() can retrieves text contents');
self.test.comment('encoding');
var image = self.base64encode('file://' + phantom.libraryPath + '/site/images/phantom.png');
self.test.assertEquals(image.length, 6160, 'Casper.base64encode() can retrieve base64 contents');
self.test.comment('clicking');
self.click('a[href="test.html"]');
});
casper.test.assert(casper.steps.length === 1, 'Casper.start() can add a new navigation step');
// Casper.viewport()
casper.test.comment('viewport');
casper.viewport(1337, 999);
casper.test.assertEquals(casper.page.viewportSize.width, 1337, 'Casper.viewport() can change the width of page viewport');
casper.test.assertEquals(casper.page.viewportSize.height, 999, 'Casper.viewport() can change the height of page viewport');
casper.test.assertRaises(casper.viewport, ['a', 'b'], 'Casper.viewport() validates viewport size data');
// Casper#then()
casper.test.comment('then');
casper.then(function(self) {
self.test.assertTitle('CasperJS test target', 'Casper.click() casper can click on a text link and react when it is loaded 1/2');
self.click('a[href="form.html"]');
});
casper.test.assert(casper.steps.length === 2, 'Casper.then() adds a new navigation step');
// Casper#capture()
casper.test.comment('capturing');
casper.viewport(300, 200);
var testFile = '/tmp/__casper_test_capture.png';
if (fs.isFile(testFile)) {
fs.remove(testFile);
}
casper.capture(testFile);
casper.test.assert(fs.isFile(testFile), 'Casper.capture() captured a screenshot');
fs.remove(testFile);
// Casper#evaluate()
casper.then(function(self) {
self.test.comment('evaluating');
var params = {
"boolean true": true,
"boolean false": false,
"int number": 42,
"float number": 1337.42,
"string": "plop! \"Ÿ£$\" 'no'",
"array": [1, 2, 3],
"object": {a: 1, b: 2}
};
var casperParams = self.evaluate(function() {
return __casper_params__;
}, params);
self.test.assertType(casperParams, "object", 'Casper.evaluate() exposes parameters in a dedicated object');
self.test.assertEquals(Object.keys(casperParams).length, 7, 'Casper.evaluate() exposes parameters object has the correct length');
for (var param in casperParams) {
self.test.assertEquals(JSON.stringify(casperParams[param]), JSON.stringify(params[param]), 'Casper.evaluate() can pass a ' + param);
self.test.assertEquals(typeof casperParams[param], typeof params[param], 'Casper.evaluate() preserves the ' + param + ' type');
}
});
// Casper#fill()
casper.then(function(self) {
self.test.assertTitle('CasperJS test form', 'Casper.click() casper can click on a text link and react when it is loaded 2/2');
self.test.comment('filling a form');
self.fill('form[action="result.html"]', {
email: 'chuck@norris.com',
content: 'Am watching thou',
check: true,
choice: 'no',
topic: 'bar',
file: phantom.libraryPath + '/README.md',
'checklist[]': ['1', '3']
});
self.test.assertEvalEquals(function() {
return document.querySelector('input[name="email"]').value;
}, 'chuck@norris.com', 'Casper.fill() can fill an input[type=text] form field');
self.test.assertEvalEquals(function() {
return document.querySelector('textarea[name="content"]').value;
}, 'Am watching thou', 'Casper.fill() can fill a textarea form field');
self.test.assertEvalEquals(function() {
return document.querySelector('select[name="topic"]').value;
}, 'bar', 'Casper.fill() can pick a value from a select form field');
self.test.assertEvalEquals(function() {
return document.querySelector('input[name="check"]').checked;
}, true, 'Casper.fill() can check a form checkbox');
self.test.assertEvalEquals(function() {
return document.querySelector('input[name="choice"][value="no"]').checked;
}, true, 'Casper.fill() can check a form radio button 1/2');
self.test.assertEvalEquals(function() {
return document.querySelector('input[name="choice"][value="yes"]').checked;
}, false, 'Casper.fill() can check a form radio button 2/2');
self.test.assertEvalEquals(function() {
return document.querySelector('input[name="file"]').files.length === 1;
}, true, 'Casper.fill() can select a file to upload');
self.test.assertEvalEquals(function() {
return (document.querySelector('input[name="checklist[]"][value="1"]').checked &&
!document.querySelector('input[name="checklist[]"][value="2"]').checked &&
document.querySelector('input[name="checklist[]"][value="3"]').checked);
}, true, 'Casper.fill() can fill a list of checkboxes');
self.click('input[type="submit"]');
});
// Casper#click()
casper.then(function(self) {
self.test.assertTitle('CasperJS test form result', 'Casper.click() casper can click on a submit button');
self.test.assertUrlMatch(/email=chuck@norris.com/, 'Casper.fill() input[type=email] field was submitted');
self.test.assertUrlMatch(/content=Am\+watching\+thou/, 'Casper.fill() textarea field was submitted');
self.test.assertUrlMatch(/check=on/, 'Casper.fill() input[type=checkbox] field was submitted');
self.test.assertUrlMatch(/choice=no/, 'Casper.fill() input[type=radio] field was submitted');
self.test.assertUrlMatch(/topic=bar/, 'Casper.fill() select field was submitted');
});
// Casper#thenClick()
casper.thenClick('body a', function(self) {
self.test.comment('Casper.thenClick()');
self.test.assertTitle('CasperJS test index', 'Casper.thenClick() casper can add a step for clicking a link');
});
// Casper#each()
casper.test.comment('each');
casper.each([1, 2, 3], function(self, item, i) {
self.test.assertEquals(i, item - 1, 'Casper.each() passes a contextualized index');
});
// Casper.XUnitExporter
casper.test.comment('phantom.Casper.XUnitExporter');
xunit = new phantom.Casper.XUnitExporter();
xunit.addSuccess('foo', 'bar');
casper.test.assertMatch(xunit.getXML(), /<testcase classname="foo" name="bar"/, 'XUnitExporter.addSuccess() adds a successful testcase');
xunit.addFailure('bar', 'baz', 'wrong', 'chucknorriz');
casper.test.assertMatch(xunit.getXML(), /<testcase classname="bar" name="baz"><failure type="chucknorriz">wrong/, 'XUnitExporter.addFailure() adds a failed testcase');
// Casper.ClientUtils.log()
casper.then(function(self) {
casper.test.comment('client utils log');
var oldLevel = casper.options.logLevel;
casper.options.logLevel = 'debug';
casper.options.verbose = false;
casper.evaluate(function() {
__utils__.log('debug message');
__utils__.log('info message', 'info');
});
casper.test.assert(casper.result.log.some(function(e) {
return e.message === 'debug message' && e.level === 'debug' && e.space === 'remote';
}), 'ClientUtils.log() adds a log entry');
casper.test.assert(casper.result.log.some(function(e) {
return e.message === 'info message' && e.level === 'info' && e.space === 'remote';
}), 'ClientUtils.log() adds a log entry at a given level');
casper.options.logLevel = oldLevel;
casper.options.verbose = true;
});
// Casper.wait()
var waitStart;
casper.then(function() {
waitStart = new Date().getTime();
}).wait(1000, function(self) {
self.test.comment('wait');
self.test.assert(new Date().getTime() - waitStart > 1000, 'Casper.wait() can wait for a given amount of time');
// Casper.waitFor()
casper.thenOpen('tests/site/waitFor.html', function(self) {
casper.test.comment('waitFor');
self.waitFor(function(self) {
return self.evaluate(function() {
return document.querySelectorAll('li').length === 4;
});
}, function(self) {
self.test.pass('Casper.waitFor() can wait for something to happen');
}, function(self) {
self.test.fail('Casper.waitFor() can wait for something to happen');
});
});
});
// Casper.getGlobal()
casper.thenOpen('tests/site/global.html', function(self) {
self.test.comment('Casper.getGlobal()');
self.test.assertEquals(self.getGlobal('myGlobal'), 'awesome string', 'Casper.getGlobal() can retrieve a remote global variable');
self.test.assertRaises(self.getGlobal, ['myUnencodableGlobal'], 'Casper.getGlobal() does not fail trying to encode an unencodable global');
});
// Casper.options.onStepComplete
casper.then(function(self) {
self.options.onStepComplete = function(self, stepResult) {
self.test.comment('Casper.options.onStepComplete()');
self.test.assertEquals(stepResult, 'ok', 'Casper.options.onStepComplete() is called on step complete');
self.options.onStepComplete = null;
};
return 'ok';
});
// Casper.options.onResourceRequested & Casper.options.onResourceReceived
casper.then(function(self) {
self.options.onResourceReceived = function(self, resource) {
self.test.comment('Casper.options.onResourceReceived()');
self.test.assertType(resource, 'object', 'Casper.options.onResourceReceived() retrieve a resource object');
self.test.assert('status' in resource, 'Casper.options.onResourceReceived() retrieve a valid resource object');
self.options.onResourceReceived = null;
};
self.options.onResourceRequested = function(self, request) {
self.test.comment('Casper.options.onResourceRequested()');
self.test.assertType(request, 'object', 'Casper.options.onResourceRequested() retrieve a request object');
self.test.assert('method' in request, 'Casper.options.onResourceRequested() retrieve a valid request object');
self.options.onResourceRequested = null;
};
self.thenOpen('tests/site/page1.html');
});
// History
casper
.thenOpen('tests/site/page1.html')
.thenOpen('tests/site/page2.html')
.thenOpen('tests/site/page3.html')
.back()
.then(function(self) {
self.test.comment('navigating history backward');
self.test.assertMatch(self.getCurrentUrl(), /tests\/site\/page2\.html$/, 'Casper.back() can go back an history step');
})
.forward()
.then(function(self) {
self.test.comment('navigating history forward');
self.test.assertMatch(self.getCurrentUrl(), /tests\/site\/page3\.html$/, 'Casper.forward() can go forward an history step');
})
;
// Casper.options.onAlert()
casper.then(function(self) {
self.options.onAlert = function(self, message) {
self.test.assertEquals(message, 'plop', 'Casper.options.onAlert() can intercept an alert message');
};
});
casper.thenOpen('tests/site/alert.html').click('button', function(self) {
self.options.onAlert = null;
});
// run suite
casper.run(function(self) {
casper.test.comment('history');
casper.test.assert(self.history.length > 0, 'Casper.history contains urls');
casper.test.assertMatch(self.history[0], /tests\/site\/index\.html$/, 'Casper.history has the correct first url');
self.test.comment('logging, again');
self.test.assertEquals(self.result.log.length, 3, 'Casper.log() logged messages');
self.test.renderResults(true, 0, save);
});