initial attempt at bringing default test messages
Showing
3 changed files
with
45 additions
and
33 deletions
... | @@ -325,9 +325,7 @@ Casper.prototype.createStep = function createStep(fn, options) { | ... | @@ -325,9 +325,7 @@ Casper.prototype.createStep = function createStep(fn, options) { |
325 | * @return Casper | 325 | * @return Casper |
326 | */ | 326 | */ |
327 | Casper.prototype.debugHTML = function debugHTML() { | 327 | Casper.prototype.debugHTML = function debugHTML() { |
328 | this.echo(this.evaluate(function _evaluate() { | 328 | this.echo(this.page.content); |
329 | return document.body.innerHTML; | ||
330 | })); | ||
331 | return this; | 329 | return this; |
332 | }; | 330 | }; |
333 | 331 | ||
... | @@ -739,7 +737,7 @@ Casper.prototype.log = function log(message, level, space) { | ... | @@ -739,7 +737,7 @@ Casper.prototype.log = function log(message, level, space) { |
739 | Casper.prototype.mouseEvent = function mouseEvent(type, selector) { | 737 | Casper.prototype.mouseEvent = function mouseEvent(type, selector) { |
740 | this.log("Mouse event '" + type + "' on selector: " + selector, "debug"); | 738 | this.log("Mouse event '" + type + "' on selector: " + selector, "debug"); |
741 | if (!this.exists(selector)) { | 739 | if (!this.exists(selector)) { |
742 | throw new CasperError("Cannot dispatch an event on nonexistent selector: " + selector); | 740 | throw new CasperError(f("Cannot dispatch %s event on nonexistent selector: %s", type, selector)); |
743 | } | 741 | } |
744 | var eventSuccess = this.evaluate(function(type, selector) { | 742 | var eventSuccess = this.evaluate(function(type, selector) { |
745 | return __utils__.mouseEvent(type, selector); | 743 | return __utils__.mouseEvent(type, selector); |
... | @@ -752,7 +750,7 @@ Casper.prototype.mouseEvent = function mouseEvent(type, selector) { | ... | @@ -752,7 +750,7 @@ Casper.prototype.mouseEvent = function mouseEvent(type, selector) { |
752 | try { | 750 | try { |
753 | this.mouse.processEvent(type, selector); | 751 | this.mouse.processEvent(type, selector); |
754 | } catch (e) { | 752 | } catch (e) { |
755 | this.log(f("Couldn't emulate event '%s' on %s: %s", type, selector, e), "error"); | 753 | this.log(f("Couldn't emulate '%s' event on %s: %s", type, selector, e), "error"); |
756 | return false; | 754 | return false; |
757 | } | 755 | } |
758 | } | 756 | } | ... | ... |
... | @@ -28,12 +28,15 @@ | ... | @@ -28,12 +28,15 @@ |
28 | * | 28 | * |
29 | */ | 29 | */ |
30 | 30 | ||
31 | /*global exports:false, phantom:false, require:false, CasperError:false*/ | ||
32 | |||
31 | var fs = require('fs'); | 33 | var fs = require('fs'); |
32 | var events = require('events'); | 34 | var events = require('events'); |
33 | var utils = require('utils'); | 35 | var utils = require('utils'); |
34 | var f = utils.format; | 36 | var f = utils.format; |
35 | 37 | ||
36 | exports.create = function create(casper, options) { | 38 | exports.create = function create(casper, options) { |
39 | "use strict"; | ||
37 | return new Tester(casper, options); | 40 | return new Tester(casper, options); |
38 | }; | 41 | }; |
39 | 42 | ||
... | @@ -44,6 +47,8 @@ exports.create = function create(casper, options) { | ... | @@ -44,6 +47,8 @@ exports.create = function create(casper, options) { |
44 | * @param Object|null options Options object | 47 | * @param Object|null options Options object |
45 | */ | 48 | */ |
46 | var Tester = function Tester(casper, options) { | 49 | var Tester = function Tester(casper, options) { |
50 | "use strict"; | ||
51 | |||
47 | if (!utils.isCasperObject(casper)) { | 52 | if (!utils.isCasperObject(casper)) { |
48 | throw new CasperError("Tester needs a Casper instance"); | 53 | throw new CasperError("Tester needs a Casper instance"); |
49 | } | 54 | } |
... | @@ -86,15 +91,17 @@ var Tester = function Tester(casper, options) { | ... | @@ -86,15 +91,17 @@ var Tester = function Tester(casper, options) { |
86 | 91 | ||
87 | this.on('fail', function onFail(failure) { | 92 | this.on('fail', function onFail(failure) { |
88 | // export | 93 | // export |
89 | this.exporter.addFailure(fs.absolute(failure.file), failure.message, failure.details || "test failed", failure.type || "unknown"); | 94 | this.exporter.addFailure( |
95 | fs.absolute(failure.file), | ||
96 | failure.message || failure.standard, | ||
97 | failure.standard || "test failed", | ||
98 | failure.type || "unknown" | ||
99 | ); | ||
90 | this.testResults.failures.push(failure); | 100 | this.testResults.failures.push(failure); |
91 | // special printing | 101 | // special printing |
92 | if (failure.type) { | 102 | if (failure.type) { |
93 | this.comment(' type: ' + failure.type); | 103 | this.comment(' type: ' + failure.type); |
94 | } | 104 | } |
95 | if (failure.details) { | ||
96 | this.comment(' details: ' + failure.details); | ||
97 | } | ||
98 | if (failure.values && Object.keys(failure.values).length > 0) { | 105 | if (failure.values && Object.keys(failure.values).length > 0) { |
99 | for (var name in failure.values) { | 106 | for (var name in failure.values) { |
100 | this.comment(' ' + name + ': ' + utils.serialize(failure.values[name])); | 107 | this.comment(' ' + name + ': ' + utils.serialize(failure.values[name])); |
... | @@ -121,7 +128,7 @@ var Tester = function Tester(casper, options) { | ... | @@ -121,7 +128,7 @@ var Tester = function Tester(casper, options) { |
121 | return this.processAssertionResult(utils.mergeObjects({ | 128 | return this.processAssertionResult(utils.mergeObjects({ |
122 | success: subject === true, | 129 | success: subject === true, |
123 | type: "assert", | 130 | type: "assert", |
124 | details: "Subject's not a strict boolean true", | 131 | standard: "Subject is strictly true", |
125 | message: message, | 132 | message: message, |
126 | file: this.currentTestFile, | 133 | file: this.currentTestFile, |
127 | values: { | 134 | values: { |
... | @@ -141,7 +148,7 @@ var Tester = function Tester(casper, options) { | ... | @@ -141,7 +148,7 @@ var Tester = function Tester(casper, options) { |
141 | this.assertEquals = this.assertEqual = function assertEquals(subject, expected, message) { | 148 | this.assertEquals = this.assertEqual = function assertEquals(subject, expected, message) { |
142 | return this.assert(this.testEquals(subject, expected), message, { | 149 | return this.assert(this.testEquals(subject, expected), message, { |
143 | type: "assertEquals", | 150 | type: "assertEquals", |
144 | details: "Subject didn't equal the expected value", | 151 | standard: "Subject equals the expected value", |
145 | values: { | 152 | values: { |
146 | subject: subject, | 153 | subject: subject, |
147 | expected: expected | 154 | expected: expected |
... | @@ -160,7 +167,7 @@ var Tester = function Tester(casper, options) { | ... | @@ -160,7 +167,7 @@ var Tester = function Tester(casper, options) { |
160 | this.assertNotEquals = function assertNotEquals(subject, shouldnt, message) { | 167 | this.assertNotEquals = function assertNotEquals(subject, shouldnt, message) { |
161 | return this.assert(!this.testEquals(subject, shouldnt), message, { | 168 | return this.assert(!this.testEquals(subject, shouldnt), message, { |
162 | type: "assertNotEquals", | 169 | type: "assertNotEquals", |
163 | details: "Subject actually equals to what it shouldn't be", | 170 | standard: "Subject doesn't equal what it shouldn't be", |
164 | values: { | 171 | values: { |
165 | subject: subject, | 172 | subject: subject, |
166 | shouldnt: shouldnt | 173 | shouldnt: shouldnt |
... | @@ -179,7 +186,7 @@ var Tester = function Tester(casper, options) { | ... | @@ -179,7 +186,7 @@ var Tester = function Tester(casper, options) { |
179 | this.assertEval = this.assertEvaluate = function assertEval(fn, message, params) { | 186 | this.assertEval = this.assertEvaluate = function assertEval(fn, message, params) { |
180 | return this.assert(casper.evaluate(fn, params), message, { | 187 | return this.assert(casper.evaluate(fn, params), message, { |
181 | type: "assertEval", | 188 | type: "assertEval", |
182 | details: "Function didn't evaluate to true", | 189 | standard: "Evaluated function returns true", |
183 | values: { | 190 | values: { |
184 | fn: fn, | 191 | fn: fn, |
185 | params: params | 192 | params: params |
... | @@ -201,7 +208,7 @@ var Tester = function Tester(casper, options) { | ... | @@ -201,7 +208,7 @@ var Tester = function Tester(casper, options) { |
201 | var subject = casper.evaluate(fn, params); | 208 | var subject = casper.evaluate(fn, params); |
202 | return this.assert(this.testEquals(subject, expected), message, { | 209 | return this.assert(this.testEquals(subject, expected), message, { |
203 | type: "assertEvalEquals", | 210 | type: "assertEvalEquals", |
204 | details: "Evaluated function didn't return the expected value", | 211 | standard: "Evaluated function returns the expected value", |
205 | values: { | 212 | values: { |
206 | fn: fn, | 213 | fn: fn, |
207 | params: params, | 214 | params: params, |
... | @@ -222,7 +229,7 @@ var Tester = function Tester(casper, options) { | ... | @@ -222,7 +229,7 @@ var Tester = function Tester(casper, options) { |
222 | this.assertExists = this.assertExist = this.assertSelectorExists = this.assertSelectorExist = function assertExists(selector, message) { | 229 | this.assertExists = this.assertExist = this.assertSelectorExists = this.assertSelectorExist = function assertExists(selector, message) { |
223 | return this.assert(casper.exists(selector), message, { | 230 | return this.assert(casper.exists(selector), message, { |
224 | type: "assertExists", | 231 | type: "assertExists", |
225 | details: f("No element matching selector %s was found", selector), | 232 | standard: f("Found an element matching %s", selector), |
226 | values: { | 233 | values: { |
227 | selector: selector | 234 | selector: selector |
228 | } | 235 | } |
... | @@ -240,7 +247,7 @@ var Tester = function Tester(casper, options) { | ... | @@ -240,7 +247,7 @@ var Tester = function Tester(casper, options) { |
240 | this.assertDoesntExist = this.assertNotExists = function assertDoesntExist(selector, message) { | 247 | this.assertDoesntExist = this.assertNotExists = function assertDoesntExist(selector, message) { |
241 | return this.assert(!casper.exists(selector), message, { | 248 | return this.assert(!casper.exists(selector), message, { |
242 | type: "assertDoesntExist", | 249 | type: "assertDoesntExist", |
243 | details: f("At least one element matching selector %s was found", selector), | 250 | standard: f("No element matching selector %s is found", selector), |
244 | values: { | 251 | values: { |
245 | selector: selector | 252 | selector: selector |
246 | } | 253 | } |
... | @@ -258,7 +265,7 @@ var Tester = function Tester(casper, options) { | ... | @@ -258,7 +265,7 @@ var Tester = function Tester(casper, options) { |
258 | var currentHTTPStatus = casper.currentHTTPStatus; | 265 | var currentHTTPStatus = casper.currentHTTPStatus; |
259 | return this.assert(this.testEquals(casper.currentHTTPStatus, status), message, { | 266 | return this.assert(this.testEquals(casper.currentHTTPStatus, status), message, { |
260 | type: "assertHttpStatus", | 267 | type: "assertHttpStatus", |
261 | details: f("HTTP status code is not %s, but %s", status, currentHTTPStatus), | 268 | standard: f("HTTP status code is %s", status), |
262 | values: { | 269 | values: { |
263 | current: currentHTTPStatus, | 270 | current: currentHTTPStatus, |
264 | expected: status | 271 | expected: status |
... | @@ -277,7 +284,7 @@ var Tester = function Tester(casper, options) { | ... | @@ -277,7 +284,7 @@ var Tester = function Tester(casper, options) { |
277 | this.assertMatch = this.assertMatches = function assertMatch(subject, pattern, message) { | 284 | this.assertMatch = this.assertMatches = function assertMatch(subject, pattern, message) { |
278 | return this.assert(pattern.test(subject), message, { | 285 | return this.assert(pattern.test(subject), message, { |
279 | type: "assertMatch", | 286 | type: "assertMatch", |
280 | details: "Subject didn't match the provided pattern", | 287 | standard: "Subject matches the provided pattern", |
281 | values: { | 288 | values: { |
282 | subject: subject, | 289 | subject: subject, |
283 | pattern: pattern | 290 | pattern: pattern |
... | @@ -295,7 +302,7 @@ var Tester = function Tester(casper, options) { | ... | @@ -295,7 +302,7 @@ var Tester = function Tester(casper, options) { |
295 | this.assertNot = function assertNot(condition, message) { | 302 | this.assertNot = function assertNot(condition, message) { |
296 | return this.assert(!condition, message, { | 303 | return this.assert(!condition, message, { |
297 | type: "assertNot", | 304 | type: "assertNot", |
298 | details: "The condition is not falsy", | 305 | standard: "Subject is falsy", |
299 | values: { | 306 | values: { |
300 | condition: condition | 307 | condition: condition |
301 | } | 308 | } |
... | @@ -314,7 +321,7 @@ var Tester = function Tester(casper, options) { | ... | @@ -314,7 +321,7 @@ var Tester = function Tester(casper, options) { |
314 | this.assertRaises = this.assertRaise = this.assertThrows = function assertRaises(fn, args, message) { | 321 | this.assertRaises = this.assertRaise = this.assertThrows = function assertRaises(fn, args, message) { |
315 | var context = { | 322 | var context = { |
316 | type: "assertRaises", | 323 | type: "assertRaises", |
317 | details: "Function didn't raise any error" | 324 | standard: "Function raises an error" |
318 | }; | 325 | }; |
319 | try { | 326 | try { |
320 | fn.apply(null, args); | 327 | fn.apply(null, args); |
... | @@ -338,7 +345,7 @@ var Tester = function Tester(casper, options) { | ... | @@ -338,7 +345,7 @@ var Tester = function Tester(casper, options) { |
338 | this.assertResourceExists = this.assertResourceExist = function assertResourceExists(test, message) { | 345 | this.assertResourceExists = this.assertResourceExist = function assertResourceExists(test, message) { |
339 | return this.assert(casper.resourceExists(test), message, { | 346 | return this.assert(casper.resourceExists(test), message, { |
340 | type: "assertResourceExists", | 347 | type: "assertResourceExists", |
341 | details: "Resource was not found", | 348 | standard: "Expected resource has been found", |
342 | values: { | 349 | values: { |
343 | test: test | 350 | test: test |
344 | } | 351 | } |
... | @@ -355,10 +362,10 @@ var Tester = function Tester(casper, options) { | ... | @@ -355,10 +362,10 @@ var Tester = function Tester(casper, options) { |
355 | this.assertTextExists = this.assertTextExist = function assertTextExists(text, message) { | 362 | this.assertTextExists = this.assertTextExist = function assertTextExists(text, message) { |
356 | var textFound = (casper.evaluate(function _evaluate() { | 363 | var textFound = (casper.evaluate(function _evaluate() { |
357 | return document.body.innerText; | 364 | return document.body.innerText; |
358 | }).indexOf(text) != -1); | 365 | }).indexOf(text) !== -1); |
359 | return this.assert(textFound, message, { | 366 | return this.assert(textFound, message, { |
360 | type: "assertTextExists", | 367 | type: "assertTextExists", |
361 | details: "Text was not found within the document body textual contents", | 368 | standard: "Found expected text within the document body", |
362 | values: { | 369 | values: { |
363 | text: text | 370 | text: text |
364 | } | 371 | } |
... | @@ -376,7 +383,7 @@ var Tester = function Tester(casper, options) { | ... | @@ -376,7 +383,7 @@ var Tester = function Tester(casper, options) { |
376 | var currentTitle = casper.getTitle(); | 383 | var currentTitle = casper.getTitle(); |
377 | return this.assert(this.testEquals(currentTitle, expected), message, { | 384 | return this.assert(this.testEquals(currentTitle, expected), message, { |
378 | type: "assertTitle", | 385 | type: "assertTitle", |
379 | details: "Page title is not the one expected", | 386 | standard: f('Page title is "%s"', expected), |
380 | values: { | 387 | values: { |
381 | subject: currentTitle, | 388 | subject: currentTitle, |
382 | expected: expected | 389 | expected: expected |
... | @@ -396,7 +403,7 @@ var Tester = function Tester(casper, options) { | ... | @@ -396,7 +403,7 @@ var Tester = function Tester(casper, options) { |
396 | var actual = utils.betterTypeOf(subject); | 403 | var actual = utils.betterTypeOf(subject); |
397 | return this.assert(this.testEquals(actual, type), message, { | 404 | return this.assert(this.testEquals(actual, type), message, { |
398 | type: "assertType", | 405 | type: "assertType", |
399 | details: f("Expected type %s, got %s", typeof subject, actual), | 406 | standard: f('Subject type is "%s"', type), |
400 | values: { | 407 | values: { |
401 | subject: subject, | 408 | subject: subject, |
402 | type: type, | 409 | type: type, |
... | @@ -417,7 +424,7 @@ var Tester = function Tester(casper, options) { | ... | @@ -417,7 +424,7 @@ var Tester = function Tester(casper, options) { |
417 | var currentUrl = casper.getCurrentUrl(); | 424 | var currentUrl = casper.getCurrentUrl(); |
418 | return this.assert(pattern.test(currentUrl), message, { | 425 | return this.assert(pattern.test(currentUrl), message, { |
419 | type: "assertUrlMatch", | 426 | type: "assertUrlMatch", |
420 | details: "Current url did not match the provided pattern", | 427 | standard: "Current url matches the provided pattern", |
421 | values: { | 428 | values: { |
422 | currentUrl: currentUrl, | 429 | currentUrl: currentUrl, |
423 | pattern: pattern | 430 | pattern: pattern |
... | @@ -492,7 +499,7 @@ var Tester = function Tester(casper, options) { | ... | @@ -492,7 +499,7 @@ var Tester = function Tester(casper, options) { |
492 | this.fail = function fail(message) { | 499 | this.fail = function fail(message) { |
493 | return this.assert(false, message, { | 500 | return this.assert(false, message, { |
494 | type: "fail", | 501 | type: "fail", |
495 | details: "explicit call to fail()" | 502 | standard: "explicit call to fail()" |
496 | }); | 503 | }); |
497 | }; | 504 | }; |
498 | 505 | ||
... | @@ -552,7 +559,7 @@ var Tester = function Tester(casper, options) { | ... | @@ -552,7 +559,7 @@ var Tester = function Tester(casper, options) { |
552 | this.pass = function pass(message) { | 559 | this.pass = function pass(message) { |
553 | return this.assert(true, message, { | 560 | return this.assert(true, message, { |
554 | type: "pass", | 561 | type: "pass", |
555 | details: "explicit call to pass()" | 562 | standard: "explicit call to pass()" |
556 | }); | 563 | }); |
557 | }; | 564 | }; |
558 | 565 | ||
... | @@ -576,7 +583,8 @@ var Tester = function Tester(casper, options) { | ... | @@ -576,7 +583,8 @@ var Tester = function Tester(casper, options) { |
576 | status = this.options.failText; | 583 | status = this.options.failText; |
577 | this.testResults.failed++; | 584 | this.testResults.failed++; |
578 | } | 585 | } |
579 | casper.echo([this.colorize(status, style), this.formatMessage(result.message)].join(' ')); | 586 | var message = result.message || result.standard; |
587 | casper.echo([this.colorize(status, style), this.formatMessage(message)].join(' ')); | ||
580 | this.emit(eventName, result); | 588 | this.emit(eventName, result); |
581 | return result; | 589 | return result; |
582 | }; | 590 | }; | ... | ... |
... | @@ -27,24 +27,26 @@ | ... | @@ -27,24 +27,26 @@ |
27 | * DEALINGS IN THE SOFTWARE. | 27 | * DEALINGS IN THE SOFTWARE. |
28 | * | 28 | * |
29 | */ | 29 | */ |
30 | 30 | /*global exports, phantom, require, CasperError*/ | |
31 | var utils = require('utils'); | 31 | var utils = require('utils'); |
32 | var fs = require('fs'); | 32 | var fs = require('fs'); |
33 | 33 | ||
34 | exports.create = function create() { | 34 | exports.create = function create() { |
35 | return new XUnitExporter(); | 35 | "use strict"; |
36 | return new this.XUnitExporter(); | ||
36 | }; | 37 | }; |
37 | 38 | ||
38 | /** | 39 | /** |
39 | * JUnit XML (xUnit) exporter for test results. | 40 | * JUnit XML (xUnit) exporter for test results. |
40 | * | 41 | * |
41 | */ | 42 | */ |
42 | XUnitExporter = function XUnitExporter() { | 43 | function XUnitExporter() { |
44 | "use strict"; | ||
43 | this._xml = utils.node('testsuite'); | 45 | this._xml = utils.node('testsuite'); |
44 | this._xml.toString = function toString() { | 46 | this._xml.toString = function toString() { |
45 | return this.outerHTML; // ouch | 47 | return this.outerHTML; // ouch |
46 | }; | 48 | }; |
47 | }; | 49 | } |
48 | exports.XUnitExporter = XUnitExporter; | 50 | exports.XUnitExporter = XUnitExporter; |
49 | 51 | ||
50 | /** | 52 | /** |
... | @@ -54,6 +56,7 @@ exports.XUnitExporter = XUnitExporter; | ... | @@ -54,6 +56,7 @@ exports.XUnitExporter = XUnitExporter; |
54 | * @param String name | 56 | * @param String name |
55 | */ | 57 | */ |
56 | XUnitExporter.prototype.addSuccess = function addSuccess(classname, name) { | 58 | XUnitExporter.prototype.addSuccess = function addSuccess(classname, name) { |
59 | "use strict"; | ||
57 | this._xml.appendChild(utils.node('testcase', { | 60 | this._xml.appendChild(utils.node('testcase', { |
58 | classname: generateClassName(classname), | 61 | classname: generateClassName(classname), |
59 | name: name | 62 | name: name |
... | @@ -69,6 +72,7 @@ XUnitExporter.prototype.addSuccess = function addSuccess(classname, name) { | ... | @@ -69,6 +72,7 @@ XUnitExporter.prototype.addSuccess = function addSuccess(classname, name) { |
69 | * @param String type | 72 | * @param String type |
70 | */ | 73 | */ |
71 | XUnitExporter.prototype.addFailure = function addFailure(classname, name, message, type) { | 74 | XUnitExporter.prototype.addFailure = function addFailure(classname, name, message, type) { |
75 | "use strict"; | ||
72 | var fnode = utils.node('testcase', { | 76 | var fnode = utils.node('testcase', { |
73 | classname: generateClassName(classname), | 77 | classname: generateClassName(classname), |
74 | name: name | 78 | name: name |
... | @@ -91,6 +95,7 @@ XUnitExporter.prototype.addFailure = function addFailure(classname, name, messag | ... | @@ -91,6 +95,7 @@ XUnitExporter.prototype.addFailure = function addFailure(classname, name, messag |
91 | * @return String | 95 | * @return String |
92 | */ | 96 | */ |
93 | function generateClassName(classname) { | 97 | function generateClassName(classname) { |
98 | "use strict"; | ||
94 | classname = classname.replace(phantom.casperPath, "").trim(); | 99 | classname = classname.replace(phantom.casperPath, "").trim(); |
95 | var script = classname || phantom.casperScript; | 100 | var script = classname || phantom.casperScript; |
96 | if (script.indexOf(fs.workingDirectory) === 0) { | 101 | if (script.indexOf(fs.workingDirectory) === 0) { |
... | @@ -111,5 +116,6 @@ function generateClassName(classname) { | ... | @@ -111,5 +116,6 @@ function generateClassName(classname) { |
111 | * @return HTMLElement | 116 | * @return HTMLElement |
112 | */ | 117 | */ |
113 | XUnitExporter.prototype.getXML = function getXML() { | 118 | XUnitExporter.prototype.getXML = function getXML() { |
119 | "use strict"; | ||
114 | return this._xml; | 120 | return this._xml; |
115 | }; | 121 | }; | ... | ... |
-
Please register or sign in to post a comment