tester.js
12.8 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*!
* Casper is a navigation utility for PhantomJS.
*
* Documentation: http://n1k0.github.com/casperjs/
* Repository: http://github.com/n1k0/casperjs
*
* Copyright (c) 2011 Nicolas Perriault
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
(function(phantom) {
/**
* Casper tester: makes assertions, stores test results and display then.
*
*/
phantom.Casper.Tester = function(casper, options) {
this.options = isType(options, "object") ? options : {};
if (!casper instanceof phantom.Casper) {
throw "phantom.Casper.Tester needs a phantom.Casper instance";
}
// locals
var exporter = new phantom.Casper.XUnitExporter();
var PASS = this.options.PASS || "PASS";
var FAIL = this.options.FAIL || "FAIL";
function compareArrays(a, b) {
if (a.length !== b.length) {
return false;
}
a.forEach(function(item, i) {
if (isType(item, "array") && !compareArrays(item, b[i])) {
return false;
}
if (item !== b[i]) {
return false;
}
});
return true;
}
// properties
this.testResults = {
passed: 0,
failed: 0
};
// methods
/**
* Asserts a condition resolves to true.
*
* @param Boolean condition
* @param String message Test description
*/
this.assert = function(condition, message) {
var status = PASS;
if (condition === true) {
style = 'INFO';
this.testResults.passed++;
exporter.addSuccess("unknown", message);
} else {
status = FAIL;
style = 'RED_BAR';
this.testResults.failed++;
exporter.addFailure("unknown", message, 'test failed', "assert");
}
casper.echo([this.colorize(status, style), this.formatMessage(message)].join(' '));
};
/**
* Asserts that two values are strictly equals.
*
* @param Mixed testValue The value to test
* @param Mixed expected The expected value
* @param String message Test description
*/
this.assertEquals = function(testValue, expected, message) {
if (this.testEquals(testValue, expected)) {
casper.echo(this.colorize(PASS, 'INFO') + ' ' + this.formatMessage(message));
this.testResults.passed++;
exporter.addSuccess("unknown", message);
} else {
casper.echo(this.colorize(FAIL, 'RED_BAR') + ' ' + this.formatMessage(message, 'WARNING'));
this.comment(' got: ' + testValue);
this.comment(' expected: ' + expected);
this.testResults.failed++;
exporter.addFailure("unknown", message, "test failed; expected: " + expected + "; got: " + testValue, "assertEquals");
}
};
/**
* Asserts that a code evaluation in remote DOM resolves to true.
*
* @param Function fn A function to be evaluated in remote DOM
* @param String message Test description
*/
this.assertEval = function(fn, message) {
return this.assert(casper.evaluate(fn), message);
};
/**
* Asserts that the result of a code evaluation in remote DOM equals
* an expected value.
*
* @param Function fn The function to be evaluated in remote DOM
* @param Boolean expected The expected value
* @param String message Test description
*/
this.assertEvalEquals = function(fn, expected, message) {
return this.assertEquals(casper.evaluate(fn), expected, message);
};
/**
* Asserts that an element matching the provided CSS3 selector exists in
* remote DOM.
*
* @param String selector CSS3 selectore
* @param String message Test description
*/
this.assertExists = function(selector, message) {
return this.assert(casper.exists(selector), message);
};
/**
* Asserts that a provided string matches a provided RegExp pattern.
*
* @param String subject The string to test
* @param RegExp pattern A RegExp object instance
* @param String message Test description
*/
this.assertMatch = function(subject, pattern, message) {
if (pattern.test(subject)) {
casper.echo(this.colorize(PASS, 'INFO') + ' ' + this.formatMessage(message));
this.testResults.passed++;
exporter.addSuccess("unknown", message);
} else {
casper.echo(this.colorize(FAIL, 'RED_BAR') + ' ' + this.formatMessage(message, 'WARNING'));
this.comment(' subject: ' + subject);
this.comment(' pattern: ' + pattern.toString());
this.testResults.failed++;
exporter.addFailure("unknown", message, "test failed; subject: " + subject + "; pattern: " + pattern.toString(), "assertMatch");
}
};
/**
* Asserts a condition resolves to false.
*
* @param Boolean condition
* @param String message Test description
*/
this.assertNot = function(condition, message) {
return this.assert(!condition, message);
};
/**
* Asserts that the provided function called with the given parameters
* will raise an exception.
*
* @param Function fn The function to test
* @param Array args The arguments to pass to the function
* @param String message Test description
*/
this.assertRaises = function(fn, args, message) {
try {
fn.apply(null, args);
this.fail(message);
} catch (e) {
this.pass(message);
}
};
/**
* Asserts that at least an element matching the provided CSS3 selector
* exists in remote DOM.
*
* @param String selector A CSS3 selector string
* @param String message Test description
*/
this.assertSelectorExists = function(selector, message) {
return this.assert(this.exists(selector), message);
};
/**
* Asserts that title of the remote page equals to the expected one.
*
* @param String expected The expected title string
* @param String message Test description
*/
this.assertTitle = function(expected, message) {
return this.assertEquals(casper.getTitle(), expected, message);
};
/**
* Asserts that the provided input is of the given type.
*
* @param mixed input The value to test
* @param String type The javascript type name
* @param String message Test description
*/
this.assertType = function(input, type, message) {
return this.assertEquals(betterTypeOf(input), type, message);
};
/**
* Asserts that a the current page url matches the provided RegExp
* pattern.
*
* @param RegExp pattern A RegExp object instance
* @param String message Test description
*/
this.assertUrlMatch = function(pattern, message) {
return this.assertMatch(casper.getCurrentUrl(), pattern, message);
};
/**
* Render a colorized output. Basically a proxy method for
* Casper.Colorizer#colorize()
*/
this.colorize = function(message, style) {
return casper.colorizer.colorize(message, style);
};
/**
* Writes a comment-style formatted message to stdout.
*
* @param String message
*/
this.comment = function(message) {
casper.echo('# ' + message, 'COMMENT');
};
/**
* Tests equality between the two passed arguments.
*
* @param Mixed v1
* @param Mixed v2
* @param Boolean
*/
this.testEquals = function(v1, v2) {
if (betterTypeOf(v1) !== betterTypeOf(v2)) {
return false;
}
if (isType(v1, "function")) {
return v1.toString() === v2.toString();
}
if (v1 instanceof Object && v2 instanceof Object) {
if (Object.keys(v1).length !== Object.keys(v2).length) {
return false;
}
for (var k in v1) {
if (!this.testEquals(v1[k], v2[k])) {
return false;
}
}
return true;
}
return v1 === v2;
};
/**
* Writes an error-style formatted message to stdout.
*
* @param String message
*/
this.error = function(message) {
casper.echo(message, 'ERROR');
};
/**
* Adds a failed test entry to the stack.
*
* @param String message
*/
this.fail = function(message) {
this.assert(false, message);
};
/**
* Formats a message to highlight some parts of it.
*
* @param String message
* @param String style
*/
this.formatMessage = function(message, style) {
var parts = /([a-z0-9_\.]+\(\))(.*)/i.exec(message);
if (!parts) {
return message;
}
return this.colorize(parts[1], 'PARAMETER') + this.colorize(parts[2], style);
};
/**
* Writes an info-style formatted message to stdout.
*
* @param String message
*/
this.info = function(message) {
casper.echo(message, 'PARAMETER');
};
/**
* Adds a successful test entry to the stack.
*
* @param String message
*/
this.pass = function(message) {
this.assert(true, message);
};
/**
* Render tests results, an optionnaly exit phantomjs.
*
* @param Boolean exit
*/
this.renderResults = function(exit, status, save) {
save = isType(save, "string") ? save : this.options.save;
var total = this.testResults.passed + this.testResults.failed, statusText, style, result;
if (this.testResults.failed > 0) {
statusText = FAIL;
style = 'RED_BAR';
} else {
statusText = PASS;
style = 'GREEN_BAR';
}
result = statusText + ' ' + total + ' tests executed, ' + this.testResults.passed + ' passed, ' + this.testResults.failed + ' failed.';
if (result.length < 80) {
result += new Array(80 - result.length + 1).join(' ');
}
casper.echo(this.colorize(result, style));
if (save && isType(require, "function")) {
try {
require('fs').write(save, exporter.getXML(), 'w');
casper.echo('result log stored in ' + save, 'INFO');
} catch (e) {
casper.echo('unable to write results to ' + save + '; ' + e, 'ERROR');
}
}
if (exit === true) {
casper.exit(status || 0);
}
};
};
})(phantom);