1 | | /*! |
2 | | * |
3 | | * Copyright (c) 2013 Sebastian Golasch |
4 | | * |
5 | | * Permission is hereby granted, free of charge, to any person obtaining a |
6 | | * copy of this software and associated documentation files (the "Software"), |
7 | | * to deal in the Software without restriction, including without limitation |
8 | | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
9 | | * and/or sell copies of the Software, and to permit persons to whom the |
10 | | * Software is furnished to do so, subject to the following conditions: |
11 | | * |
12 | | * The above copyright notice and this permission notice shall be included |
13 | | * in all copies or substantial portions of the Software. |
14 | | * |
15 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
16 | | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
17 | | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
18 | | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
19 | | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
20 | | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
21 | | * DEALINGS IN THE SOFTWARE. |
22 | | */ |
23 | | |
24 | 1 | 'use strict'; |
25 | | |
26 | | // ext. libs |
27 | 1 | var Q = require('q'); |
28 | 1 | var uuid = require('node-uuid'); |
29 | 1 | var chai = require('chai'); |
30 | | |
31 | | // Module variable |
32 | 1 | var Assertions; |
33 | | |
34 | | /** |
35 | | * @module Assertions |
36 | | * @namespace Dalek.Internal |
37 | | */ |
38 | | |
39 | 1 | module.exports = function () { |
40 | 1 | return Assertions; |
41 | | }; |
42 | | |
43 | | /** |
44 | | * Assertions check if the assumptions you made about a website are correct. |
45 | | * For example they might check if the title of a page or the content text of |
46 | | * an element is as expected, or if your mobile website version only displays |
47 | | * a certain amount of elements. |
48 | | * |
49 | | * @class Assertions |
50 | | * @constructor |
51 | | * @part Assertions |
52 | | * @api |
53 | | */ |
54 | | |
55 | 1 | Assertions = function (opts) { |
56 | 0 | this.test = opts.test; |
57 | 0 | this.proceeded = []; |
58 | 0 | this.chaining = false; |
59 | | }; |
60 | | |
61 | | /** |
62 | | * It can be really cumbersome to always write assert, assert & assert |
63 | | * all over the place when your doing multiple assertions. |
64 | | * To avoid this, open an assertion context in your test which allows |
65 | | * you to write (n) assertions without having to write 'assert' before each. |
66 | | * |
67 | | * So, instead of writing this: |
68 | | * |
69 | | * ```javascript |
70 | | * test.open('http://doctorwhotv.co.uk/') |
71 | | * .assert.text('#nav').is('Navigation') |
72 | | * .assert.visible('#nav') |
73 | | * .assert.attr('#nav', 'data-nav', 'true') |
74 | | * .done(); |
75 | | * ``` |
76 | | * |
77 | | * you can write this: |
78 | | * |
79 | | * ```javascript |
80 | | * test.open('http://doctorwhotv.co.uk/') |
81 | | * .assert.chain() |
82 | | * .text('#nav').is('Navigation') |
83 | | * .visible('#nav') |
84 | | * .attr('#nav', 'data-nav', 'true') |
85 | | * .end() |
86 | | * .done(); |
87 | | * ``` |
88 | | * |
89 | | * to make it even more concise, you can combine this with the [query](actions.html#meth-query) method: |
90 | | * |
91 | | * ```javascript |
92 | | * test.open('http://doctorwhotv.co.uk/') |
93 | | * .assert.chain() |
94 | | * .query('#nav') |
95 | | * .text().is('Navigation') |
96 | | * .visible() |
97 | | * .attr('data-nav', 'true') |
98 | | * .end() |
99 | | * .end() |
100 | | * .done(); |
101 | | * ``` |
102 | | * |
103 | | * Always make sure, you terminate it with the [end](#meth-end) method! |
104 | | * |
105 | | * @api |
106 | | * @method chain |
107 | | * @chainable |
108 | | */ |
109 | | |
110 | 1 | Assertions.prototype.chain = function () { |
111 | 0 | this.test.lastChain.push('chaining'); |
112 | 0 | this.chaining = true; |
113 | 0 | return this; |
114 | | }; |
115 | | |
116 | | /** |
117 | | * Terminates an assertion chain or a query |
118 | | * |
119 | | * ```javascript |
120 | | * test.open('http://doctorwhotv.co.uk/') |
121 | | * .assert.chain() |
122 | | * .query('#nav') |
123 | | * .text().is('Navigation') |
124 | | * .visible() |
125 | | * .attr('data-nav', 'true') |
126 | | * .end() |
127 | | * .end() |
128 | | * .done(); |
129 | | * ``` |
130 | | * |
131 | | * @api |
132 | | * @method end |
133 | | * @chainable |
134 | | */ |
135 | | |
136 | 1 | Assertions.prototype.end = function () { |
137 | 0 | var lastAction = this.test.lastChain.pop(); |
138 | 0 | if (lastAction === 'chaining') { |
139 | 0 | this.chaining = false; |
140 | | } |
141 | | |
142 | 0 | if (lastAction === 'querying') { |
143 | 0 | this.test.querying = false; |
144 | | } |
145 | 0 | return this.test; |
146 | | }; |
147 | | |
148 | | /** |
149 | | * Asserts that a given resource does exist in the environment. |
150 | | * |
151 | | * @method resourceExists |
152 | | * @param {string} url URL of the resource to check |
153 | | * @param {string} message Message for the test reporter |
154 | | * @chainable |
155 | | */ |
156 | | |
157 | 1 | Assertions.prototype.resourceExists = function (url, message) { |
158 | 0 | var hash = uuid.v4(); |
159 | 0 | var cb = this._generateCallbackAssertion('resourceExists', 'resourceExists', this._testTruthy, hash, {url: url, message: message}).bind(this.test); |
160 | 0 | this._addToActionQueue([url, hash], 'resourceExists', cb); |
161 | 0 | return this.chaining ? this : this.test; |
162 | | }; |
163 | | |
164 | | /** |
165 | | * Asserts that a given element appears n times on the page. |
166 | | * |
167 | | * Given this portion of HTML, you would like to assure that all of these elements |
168 | | * are ending up in your rendered markup on your page. |
169 | | * |
170 | | * ```html |
171 | | * <section id="blog-overview"> |
172 | | * <article class="teaser"></article> |
173 | | * <article class="teaser"></article> |
174 | | * <article class="teaser"></article> |
175 | | * <article class="teaser"></article> |
176 | | * </section> |
177 | | * ``` |
178 | | * |
179 | | * The simple solution is to check if all these elements are present |
180 | | * |
181 | | * ```javascript |
182 | | * test.assert.numberOfElements('#blog-overview .teaser', 4, '4 blog teasers are present') |
183 | | * ``` |
184 | | * The alternate syntax for this is: |
185 | | * |
186 | | * ```javascript |
187 | | * test.assert.numberOfElements('#blog-overview .teaser') |
188 | | * .is(4, '4 blog teasers are present') |
189 | | * ``` |
190 | | * |
191 | | * If you are not sure how many elements will exactly end up in your markup, |
192 | | * you could use the between assertion handler |
193 | | * |
194 | | * ```javascript |
195 | | * test.assert.numberOfElements('#blog-overview .teaser') |
196 | | * .is.between([2, 6], 'Between 2 and 6 blog teasers are present') |
197 | | * ``` |
198 | | * |
199 | | * If you dealing with the situation that you have a minimum of elements, |
200 | | * you expect, you can use this helper: |
201 | | * |
202 | | * ```javascript |
203 | | * test.assert.numberOfElements('#blog-overview .teaser') |
204 | | * .is.gt(2, 'At least 3 blog teasers are present') |
205 | | * ``` |
206 | | * If you want to know if its 'greater than equal', use this one |
207 | | * |
208 | | * ```javascript |
209 | | * test.assert.numberOfElements('#blog-overview .teaser') |
210 | | * .is.gte(2, 'At least 2 blog teasers are present') |
211 | | * ``` |
212 | | * as well as their 'lower than' and 'lower than equal' equivalents. |
213 | | * |
214 | | * ```javascript |
215 | | * test.assert.numberOfElements('#blog-overview .teaser') |
216 | | * .is.lt(5, 'Less than 5 blog teasers are present') |
217 | | * ``` |
218 | | * |
219 | | * ```javascript |
220 | | * test.assert.numberOfElements('#blog-overview .teaser') |
221 | | * .is.lte(5, 'Less than, or 5 blog teasers are present') |
222 | | * ``` |
223 | | * And if you just want to know, if a certain amount of teasers isnʼt present, |
224 | | * you can still use the not() assertion helper |
225 | | * |
226 | | * ```javascript |
227 | | * test.assert.numberOfElements('#blog-overview .teaser') |
228 | | * .is.not(5, 'There are more or less than 5 teasers present') |
229 | | * ``` |
230 | | * |
231 | | * @api |
232 | | * @method numberOfElements |
233 | | * @param {string} selector Selector that matches the elements to test |
234 | | * @param {string} expected Expected test result |
235 | | * @param {string} message Message for the test reporter |
236 | | * @chainable |
237 | | */ |
238 | | |
239 | 1 | Assertions.prototype.numberOfElements = function (selector, expected, message) { |
240 | 0 | var hash = uuid.v4(); |
241 | | |
242 | 0 | if (this.test.querying === true) { |
243 | 0 | message = expected; |
244 | 0 | expected = selector; |
245 | 0 | selector = this.test.selector; |
246 | | } |
247 | | |
248 | 0 | var cb = this._generateCallbackAssertion('numberOfElements', 'numberOfElements', this._testShallowEquals, hash, {expected: expected, selector: selector, message: message}).bind(this.test); |
249 | 0 | this._addToActionQueue([selector, expected, hash], 'getNumberOfElements', cb); |
250 | 0 | return this.chaining ? this : this.test; |
251 | | }; |
252 | | |
253 | | /** |
254 | | * |
255 | | * Asserts that a given element is visible n times in the current viewport. |
256 | | * |
257 | | * |
258 | | * Given this portion of HTML, you would like to assure that all of these elements |
259 | | * are ending up in your rendered markup on your page. |
260 | | * |
261 | | * ```html |
262 | | * <section id="blog-overview"> |
263 | | * <article class="teaser"></article> |
264 | | * <article class="teaser"></article> |
265 | | * <article class="teaser"></article> |
266 | | * <article class="teaser"></article> |
267 | | * </section> |
268 | | * ``` |
269 | | * |
270 | | * The simple solution is to check if all these elements are visible |
271 | | * |
272 | | * ```javascript |
273 | | * test.assert.numberOfVisibleElements('#blog-overview .teaser', 4, '4 blog teasers are visible') |
274 | | * ``` |
275 | | * The alternate syntax for this is: |
276 | | * |
277 | | * ```javascript |
278 | | * test.assert.numberOfVisibleElements('#blog-overview .teaser') |
279 | | * .is(4, '4 blog teasers are visible') |
280 | | * ``` |
281 | | * |
282 | | * If you are not sure how many elements will exactly be shown in the current viewport, |
283 | | * you could use the between assertion handler |
284 | | * |
285 | | * ```javascript |
286 | | * test.assert.numberOfVisibleElements('#blog-overview .teaser') |
287 | | * .is.between(2, 6, 'Between 2 and 6 blog teasers are visible') |
288 | | * ``` |
289 | | * |
290 | | * If you dealing with the situation that you have a minimum of elements, |
291 | | * you expect, use this helper: |
292 | | * |
293 | | * ```javascript |
294 | | * test.assert.numberOfVisibleElements('#blog-overview .teaser') |
295 | | * .is.gt(2, 'At least 3 blog teasers are visible') |
296 | | * ``` |
297 | | * If you want to know if its 'greater than equal', you can use this one |
298 | | * |
299 | | * ```javascript |
300 | | * test.assert.numberOfVisibleElements('#blog-overview .teaser') |
301 | | * .is.gte(2, 'At least 2 blog teasers are visible') |
302 | | * ``` |
303 | | * as well as their 'lower than' and 'lower than equal' equivalents. |
304 | | * |
305 | | * ```javascript |
306 | | * test.assert.numberOfVisibleElements('#blog-overview .teaser') |
307 | | * .is.lt(5, 'Less than 5 blog teasers are visible') |
308 | | * ``` |
309 | | * |
310 | | * ```javascript |
311 | | * test.assert.numberOfVisibleElements('#blog-overview .teaser') |
312 | | * .is.lte(5, 'Less than, or 5 blog teasers are visible') |
313 | | * ``` |
314 | | * And if you just want to know, if a certain amount of teasers isnʼt visible, |
315 | | * you can still use the ':not(): assertion helper |
316 | | * |
317 | | * ```javascript |
318 | | * test.assert.numberOfVisibleElements('#blog-overview .teaser') |
319 | | * .is.not(5, 'There are more or less than 5 teasers visible') |
320 | | * ``` |
321 | | * |
322 | | * > NOTE: Buggy on all browsers |
323 | | * |
324 | | * @api |
325 | | * @method numberOfVisibleElements |
326 | | * @param {string} selector Selector that matches the elements to test |
327 | | * @param {string} expected Expected test result |
328 | | * @param {string} message Message for the test reporter |
329 | | * @chainable |
330 | | */ |
331 | | |
332 | 1 | Assertions.prototype.numberOfVisibleElements = function (selector, expected, message) { |
333 | 0 | var hash = uuid.v4(); |
334 | | |
335 | 0 | if (this.test.querying === true) { |
336 | 0 | message = expected; |
337 | 0 | expected = selector; |
338 | 0 | selector = this.test.selector; |
339 | | } |
340 | | |
341 | 0 | var cb = this._generateCallbackAssertion('numberOfVisibleElements', 'numberOfVisibleElements', this._testShallowEquals, hash, {expected: expected, selector: selector, message: message}).bind(this.test); |
342 | 0 | this._addToActionQueue([selector, expected, hash], 'getNumberOfVisibleElements', cb); |
343 | 0 | return this.chaining ? this : this.test; |
344 | | }; |
345 | | |
346 | | /** |
347 | | * Asserts that a given form field has the provided value. |
348 | | * |
349 | | * Given this portion of HTML, we would like to get the information which option element |
350 | | * is currently selected. |
351 | | * |
352 | | * ```html |
353 | | * <form name="fav-doctor" id="fav-doctor"> |
354 | | * <select id="the-doctors"> |
355 | | * <option value="9">Eccleston</option> |
356 | | * <option selected value="10">Tennant</option> |
357 | | * <option value="11">Smith</option> |
358 | | * </select> |
359 | | * </form> |
360 | | * ``` |
361 | | * |
362 | | * ```javascript |
363 | | * test |
364 | | * .assert.val('#the-doctors', 10, 'David is the favourite') |
365 | | * // lets change the favourite by selection the last option |
366 | | * .click('#the-doctors option:last') |
367 | | * .assert.val('#the-doctors', 11, 'Matt is now my favourite, bow ties are cool') |
368 | | * ``` |
369 | | * |
370 | | * This assertion is capable of getting the values from every form element |
371 | | * that holds a value attribute |
372 | | * |
373 | | * Getting texts out of normal input fields is pretty straight forward |
374 | | * |
375 | | * ```html |
376 | | * <label for="fav-enemy">Tell my your favourity Who enemy:</label> |
377 | | * <input id="fav-enemy" name="fav-enemy" type="text" value="Daleks" /> |
378 | | * ``` |
379 | | * |
380 | | * ```javascript |
381 | | * test |
382 | | * .assert.val('#fav-enemy', 'Daleks', 'Daleks are so cute') |
383 | | * // lets change the favourite by typing smth. new |
384 | | * .type('#fav-enemy', 'Cyberman') |
385 | | * .assert.val('#fav-enemy', 'Cyberman', 'Cyberman are so cyber') |
386 | | * ``` |
387 | | * |
388 | | * @api |
389 | | * @method val |
390 | | * @param {string} selector Selector that matches the elements to test |
391 | | * @param {string} expected Expected test result |
392 | | * @param {string} message Message for the test reporter |
393 | | * @chainable |
394 | | */ |
395 | | |
396 | 1 | Assertions.prototype.val = function (selector, expected, message) { |
397 | 0 | var hash = uuid.v4(); |
398 | | |
399 | 0 | if (this.test.querying === true) { |
400 | 0 | message = expected; |
401 | 0 | expected = selector; |
402 | 0 | selector = this.test.selector; |
403 | | } |
404 | | |
405 | 0 | var cb = this._generateCallbackAssertion('val', 'val', this._testShallowEquals, hash, {expected: expected, selector: selector, message: message}).bind(this.test); |
406 | 0 | this._addToActionQueue([selector, expected, hash], 'val', cb); |
407 | 0 | return this.chaining ? this : this.test; |
408 | | }; |
409 | | |
410 | | /** |
411 | | * Checks the computed style. |
412 | | * |
413 | | * ```html |
414 | | * <div id="superColoredElement">Rose</div> |
415 | | * ``` |
416 | | * |
417 | | * ```css |
418 | | * #superColoredElement { |
419 | | * background-color: rgba(255, 0, 0, 1); |
420 | | * color: rgba(0, 128, 0, 1); |
421 | | * } |
422 | | * ``` |
423 | | * |
424 | | * ```javascript |
425 | | * test |
426 | | * .open('http://unicorns.rainbows.io') |
427 | | * .assert.css('#superColoredElement', 'background-color', 'rgba(255, 0, 0, 1)') |
428 | | * .assert.css('#superColoredElement', 'color', 'rgba(0, 128, 0, 1)') |
429 | | * .done(); |
430 | | * ``` |
431 | | * |
432 | | * @api |
433 | | * @method css |
434 | | * @param {string} selector Selector that matches the elements to test |
435 | | * @param {string} property CSS property to check |
436 | | * @param {string} expected Expected test result |
437 | | * @param {string} message Message for the test reporter |
438 | | * @chainable |
439 | | */ |
440 | | |
441 | 1 | Assertions.prototype.css = function (selector, property, expected, message) { |
442 | 0 | var hash = uuid.v4(); |
443 | | |
444 | 0 | if (this.test.querying === true) { |
445 | 0 | message = expected; |
446 | 0 | expected = property; |
447 | 0 | property = selector; |
448 | 0 | selector = this.test.selector; |
449 | | } |
450 | | |
451 | 0 | var cb = this._generateCallbackAssertion('css', 'css', this._testShallowEquals, hash, {expected: expected, selector: selector, porperty: property, message: message}).bind(this.test); |
452 | 0 | this._addToActionQueue([selector, property, expected, hash], 'css', cb); |
453 | 0 | return this.chaining ? this : this.test; |
454 | | }; |
455 | | |
456 | | /** |
457 | | * Checks the actual width of an element. |
458 | | * |
459 | | * ```html |
460 | | * <div id="fixed-dimensions" style="width: 100px"></div> |
461 | | * ``` |
462 | | * |
463 | | * ```javascript |
464 | | * test |
465 | | * .open('http://localhost:5000/index.html') |
466 | | * // all true, all pixel |
467 | | * .assert.width('#fixed-dimensions', 100) |
468 | | * .assert.width('#fixed-dimensions').is(100) |
469 | | * .assert.width('#fixed-dimensions').is.not(100) |
470 | | * .assert.width('#fixed-dimensions').is.gt(90) |
471 | | * .assert.width('#fixed-dimensions').is.gte(97) |
472 | | * .assert.width('#fixed-dimensions').is.lt(120) |
473 | | * .assert.width('#fixed-dimensions').is.lte(110) |
474 | | * .assert.width('#fixed-dimensions').is.between([90, 110]) |
475 | | * .done(); |
476 | | * |
477 | | * ``` |
478 | | * |
479 | | * @api |
480 | | * @method width |
481 | | * @param {string} selector Selector that matches the elements to test |
482 | | * @param {string} expected Expected test result |
483 | | * @param {string} message Message for the test reporter |
484 | | * @chainable |
485 | | */ |
486 | | |
487 | 1 | Assertions.prototype.width = function (selector, expected, message) { |
488 | 0 | var hash = uuid.v4(); |
489 | | |
490 | 0 | if (this.test.querying === true) { |
491 | 0 | message = expected; |
492 | 0 | expected = selector; |
493 | 0 | selector = this.test.selector; |
494 | | } |
495 | | |
496 | 0 | var cb = this._generateCallbackAssertion('width', 'width', this._testShallowEquals, hash, {expected: expected, selector: selector, message: message}).bind(this.test); |
497 | 0 | this._addToActionQueue([selector, expected, hash], 'width', cb); |
498 | 0 | return this.chaining ? this : this.test; |
499 | | }; |
500 | | |
501 | | /** |
502 | | * Checks the actual height of an element. |
503 | | * |
504 | | * ```html |
505 | | * <div id="fixed-dimensions" style="height: 100px"></div> |
506 | | * ``` |
507 | | * |
508 | | * ```javascript |
509 | | * test |
510 | | * .open('http://localhost:5000/index.html') |
511 | | * // all true, all pixel |
512 | | * .assert.height('#fixed-dimensions', 100) |
513 | | * .assert.height('#fixed-dimensions').is(100) |
514 | | * .assert.height('#fixed-dimensions').is.not(100) |
515 | | * .assert.height('#fixed-dimensions').is.gt(90) |
516 | | * .assert.height('#fixed-dimensions').is.gte(97) |
517 | | * .assert.height('#fixed-dimensions').is.lt(120) |
518 | | * .assert.height('#fixed-dimensions').is.lte(110) |
519 | | * .assert.height('#fixed-dimensions').is.between([90, 110]) |
520 | | * .done(); |
521 | | * |
522 | | * ``` |
523 | | * |
524 | | * @api |
525 | | * @method height |
526 | | * @param {string} selector Selector that matches the elements to test |
527 | | * @param {string} expected Expected test result |
528 | | * @param {string} message Message for the test reporter |
529 | | * @chainable |
530 | | */ |
531 | | |
532 | 1 | Assertions.prototype.height = function (selector, expected, message) { |
533 | 0 | var hash = uuid.v4(); |
534 | | |
535 | 0 | if (this.test.querying === true) { |
536 | 0 | message = expected; |
537 | 0 | expected = selector; |
538 | 0 | selector = this.test.selector; |
539 | | } |
540 | | |
541 | 0 | var cb = this._generateCallbackAssertion('height', 'height', this._testShallowEquals, hash, {expected: expected, selector: selector, message: message}).bind(this.test); |
542 | 0 | this._addToActionQueue([selector, expected, hash], 'height', cb); |
543 | 0 | return this.chaining ? this : this.test; |
544 | | }; |
545 | | |
546 | | /** |
547 | | * Determine if an <option> element, or an <input> element of type checkbox or radio is currently selected. |
548 | | * |
549 | | * ```html |
550 | | * <input type="checkbox" id="unchecked_checkbox" name="unchecked_checkbox"/> |
551 | | * <input type="checkbox" id="checked_checkbox" name="checked_checkbox" checked="checked"/> |
552 | | * <select id="select_elm" name="select_elm"> |
553 | | * <option value="9">Eccleston</option> |
554 | | * <option selected value="10">Tennant</option> |
555 | | * <option value="11">Smith</option> |
556 | | * </select> |
557 | | * ``` |
558 | | * |
559 | | * Checking radio and checkboxes: |
560 | | * |
561 | | * ```javascript |
562 | | * test |
563 | | * .open('http://selectables.org') |
564 | | * .assert.selected('#checked_checkbox') |
565 | | * .done(); |
566 | | * ``` |
567 | | * |
568 | | * Checking option elements: |
569 | | * |
570 | | * ```javascript |
571 | | * test |
572 | | * .open('http://selectables.org') |
573 | | * .assert.selected('#select_elm option:nth-child(2)') |
574 | | * .done(); |
575 | | * ``` |
576 | | * |
577 | | * @api |
578 | | * @method selected |
579 | | * @param {string} selector Selector that matches the elements to test |
580 | | * @param {string} message Message for the test reporter |
581 | | * @chainable |
582 | | */ |
583 | | |
584 | 1 | Assertions.prototype.selected = function (selector, message) { |
585 | 0 | var hash = uuid.v4(); |
586 | | |
587 | 0 | if (this.test.querying === true) { |
588 | 0 | message = selector; |
589 | 0 | selector = this.test.selector; |
590 | | } |
591 | | |
592 | 0 | var cb = this._generateCallbackAssertion('selected', 'selected', this._testShallowEquals, hash, {expected: true, selector: selector, message: message}).bind(this.test); |
593 | 0 | this._addToActionQueue([selector, true, hash], 'selected', cb); |
594 | 0 | return this.chaining ? this : this.test; |
595 | | }; |
596 | | |
597 | | /** |
598 | | * Determine if an <option> element, or an <input> element of type |
599 | | * checkbox or radio is currently not selected. |
600 | | * |
601 | | * ```html |
602 | | * <input type="checkbox" id="unchecked_checkbox" name="unchecked_checkbox"/> |
603 | | * <input type="checkbox" id="checked_checkbox" name="checked_checkbox" checked="checked"/> |
604 | | * <select id="select_elm" name="select_elm"> |
605 | | * <option value="9">Eccleston</option> |
606 | | * <option selected value="10">Tennant</option> |
607 | | * <option value="11">Smith</option> |
608 | | * </select> |
609 | | * ``` |
610 | | * |
611 | | * Checking radio and checkboxes: |
612 | | * |
613 | | * ```javascript |
614 | | * test |
615 | | * .open('http://selectables.org') |
616 | | * .assert.notSelected('#unchecked_checkbox') |
617 | | * .done(); |
618 | | * ``` |
619 | | * |
620 | | * Checking option elements: |
621 | | * |
622 | | * ```javascript |
623 | | * test |
624 | | * .open('http://selectables.org') |
625 | | * .assert.notSelected('#select_elm option:last-child') |
626 | | * .done(); |
627 | | * ``` |
628 | | * |
629 | | * @api |
630 | | * @method notSelected |
631 | | * @param {string} selector Selector that matches the elements to test |
632 | | * @param {string} message Message for the test reporter |
633 | | * @chainable |
634 | | */ |
635 | | |
636 | 1 | Assertions.prototype.notSelected = function (selector, message) { |
637 | 0 | var hash = uuid.v4(); |
638 | | |
639 | 0 | if (this.test.querying === true) { |
640 | 0 | message = selector; |
641 | 0 | selector = this.test.selector; |
642 | | } |
643 | | |
644 | 0 | var cb = this._generateCallbackAssertion('selected', 'selected', this._testShallowEquals, hash, {expected: false, selector: selector, message: message}).bind(this.test); |
645 | 0 | this._addToActionQueue([selector, false, hash], 'selected', cb); |
646 | 0 | return this.chaining ? this : this.test; |
647 | | }; |
648 | | |
649 | | /** |
650 | | * Determine if an element is currently enabled. |
651 | | * |
652 | | * ```html |
653 | | * <input id="onmars" type="text" size="50" name="onmars" placeholder="State your name, rank and intention!"></input> |
654 | | * ``` |
655 | | * |
656 | | * ```javascript |
657 | | * test |
658 | | * .open('http://doctor.thedoctor.com/doctor') |
659 | | * .assert.enabled('#onmars', 'Is enabled!') |
660 | | * .done(); |
661 | | * ``` |
662 | | * |
663 | | * @api |
664 | | * @method enabled |
665 | | * @param {string} selector Selector that matches the elements to test |
666 | | * @param {string} message Message for the test reporter |
667 | | * @chainable |
668 | | */ |
669 | | |
670 | 1 | Assertions.prototype.enabled = function (selector, message) { |
671 | 0 | var hash = uuid.v4(); |
672 | | |
673 | 0 | if (this.test.querying === true) { |
674 | 0 | message = selector; |
675 | 0 | selector = this.test.selector; |
676 | | } |
677 | | |
678 | 0 | var cb = this._generateCallbackAssertion('enabled', 'enabled', this._testShallowEquals, hash, {expected: true, selector: selector, message: message}).bind(this.test); |
679 | 0 | this._addToActionQueue([selector, true, hash], 'enabled', cb); |
680 | 0 | return this.chaining ? this : this.test; |
681 | | }; |
682 | | |
683 | | /** |
684 | | * Determine if an element is currently disabled. |
685 | | * |
686 | | * ```html |
687 | | * <input disabled id="onearth" type="text" size="50" name="onearth" placeholder="State your name, rank and intention!"></input> |
688 | | * ``` |
689 | | * |
690 | | * ```javascript |
691 | | * test |
692 | | * .open('http://doctor.thedoctor.com/doctor') |
693 | | * .assert.disabled('#onearth', 'Is disabled!') |
694 | | * .done(); |
695 | | * ``` |
696 | | * |
697 | | * @api |
698 | | * @method disabled |
699 | | * @param {string} selector Selector that matches the elements to test |
700 | | * @param {string} message Message for the test reporter |
701 | | * @chainable |
702 | | */ |
703 | | |
704 | 1 | Assertions.prototype.disabled = function (selector, message) { |
705 | 0 | var hash = uuid.v4(); |
706 | | |
707 | 0 | if (this.test.querying === true) { |
708 | 0 | message = selector; |
709 | 0 | selector = this.test.selector; |
710 | | } |
711 | | |
712 | 0 | var cb = this._generateCallbackAssertion('enabled', 'enabled', this._testShallowEquals, hash, {expected: false, selector: selector, message: message}).bind(this.test); |
713 | 0 | this._addToActionQueue([selector, false, hash], 'enabled', cb); |
714 | 0 | return this.chaining ? this : this.test; |
715 | | }; |
716 | | |
717 | | /** |
718 | | * Checks the contents of a cookie. |
719 | | * |
720 | | * ```javascript |
721 | | * test |
722 | | * .open('http://cookiejar.io/not_your_mothers_javascript.html') |
723 | | * .setCookie('atestcookie', 'foobar=baz') |
724 | | * .assert.cookie('atestcookie', 'foobar=baz') |
725 | | * .done(); |
726 | | * ``` |
727 | | * |
728 | | * @api |
729 | | * @method cookie |
730 | | * @param {string} name Name of the cookie |
731 | | * @param {string} expect Expected testresult |
732 | | * @param {string} message Message for the test reporter |
733 | | * @chainable |
734 | | */ |
735 | | |
736 | 1 | Assertions.prototype.cookie = function (name, expected, message) { |
737 | 0 | var hash = uuid.v4(); |
738 | 0 | var cb = this._generateCallbackAssertion('cookie', 'cookie', this._testShallowEquals, hash, {expected: expected, name: name, message: message}).bind(this.test); |
739 | 0 | this._addToActionQueue([name, expected, hash], 'cookie', cb); |
740 | 0 | return this.chaining ? this : this.test; |
741 | | }; |
742 | | |
743 | | /** |
744 | | * Asserts that current HTTP status code is the same as the one passed as argument. |
745 | | * TODO: Needs some work to be implement (maybe JavaScript, Webdriver ha no method for this) |
746 | | * |
747 | | * @method httpStatus |
748 | | * @param {integer} status HTTP status code |
749 | | * @param {string} message Message for the test reporter |
750 | | * @chainable |
751 | | */ |
752 | | |
753 | 1 | Assertions.prototype.httpStatus = function (status, message) { |
754 | 0 | var hash = uuid.v4(); |
755 | 0 | var cb = this._generateCallbackAssertion('httpStatus', 'httpStatus', this._testShallowEquals, hash, {expected: status, message: message}).bind(this.test); |
756 | 0 | this._addToActionQueue([status, hash], 'httpStatus', cb); |
757 | 0 | return this.chaining ? this : this.test; |
758 | | }; |
759 | | |
760 | | /** |
761 | | * Asserts that an element matching the provided selector expression exists in remote DOM environment. |
762 | | * |
763 | | * ```html |
764 | | * <body> |
765 | | * <p id="so-lonely">Last of the timelords</p> |
766 | | * </body> |
767 | | * ``` |
768 | | * |
769 | | * ```javascript |
770 | | * test |
771 | | * .open('http://doctor.thedoctor.com/doctor') |
772 | | * .assert.exists('#so-lonely', 'The loneliest element in the universe exists') |
773 | | * .done() |
774 | | * ``` |
775 | | * |
776 | | * @api |
777 | | * @method exists |
778 | | * @param {string} selector Selector that matches the elements to test |
779 | | * @param {string} message Message for the test reporter |
780 | | * @chainable |
781 | | */ |
782 | | |
783 | 1 | Assertions.prototype.exists = function (selector, message) { |
784 | 0 | var hash = uuid.v4(); |
785 | | |
786 | 0 | if (this.test.querying === true) { |
787 | 0 | message = selector; |
788 | 0 | selector = this.test.selector; |
789 | | } |
790 | | |
791 | 0 | var cb = this._generateCallbackAssertion('exists', 'exists', this._testTruthy, hash, {selector: selector, message: message}).bind(this.test); |
792 | 0 | this._addToActionQueue([selector, hash], 'exists', cb); |
793 | 0 | return this.chaining ? this : this.test; |
794 | | }; |
795 | | |
796 | | /** |
797 | | * Asserts that an element matching the provided selector expression doesnʼt |
798 | | * exists within the remote DOM environment. |
799 | | * |
800 | | * ```html |
801 | | * <body> |
802 | | * <p id="so-lonely">Last of the time lords</p> |
803 | | * </body> |
804 | | * ``` |
805 | | * |
806 | | * ```javascript |
807 | | * test |
808 | | * .open('http://doctor.thedoctor.com/doctor') |
809 | | * .assert.doesntExist('#the-master', 'The master element has not been seen') |
810 | | * .done(); |
811 | | * ``` |
812 | | * |
813 | | * @api |
814 | | * @method doesntExist |
815 | | * @param {string} selector Selector that matches the elements to test |
816 | | * @param {string} message Message for the test reporter |
817 | | * @chainable |
818 | | */ |
819 | | |
820 | 1 | Assertions.prototype.doesntExist = function (selector, message) { |
821 | 0 | var hash = uuid.v4(); |
822 | | |
823 | 0 | if (this.test.querying === true) { |
824 | 0 | message = selector; |
825 | 0 | selector = this.test.selector; |
826 | | } |
827 | | |
828 | 0 | var cb = this._generateCallbackAssertion('exists', '!exists', this._testFalsy, hash, {selector: selector, message: message}).bind(this.test); |
829 | 0 | this._addToActionQueue([selector, hash], 'exists', cb); |
830 | 0 | return this.chaining ? this : this.test; |
831 | | }; |
832 | | |
833 | | /** |
834 | | * Asserts that the element matching the provided selector expression is not visible. |
835 | | * |
836 | | * ```html |
837 | | * <body> |
838 | | * <h1 style="display: none">Me? So hidden …</h1> |
839 | | * <h2>Me? So in viewport...</h2> |
840 | | * </body> |
841 | | * ``` |
842 | | * |
843 | | * ```javascript |
844 | | * test |
845 | | * .open('http://allyourviewportsbelongto.us') |
846 | | * .assert.notVisible('h1', 'Element is not visible') |
847 | | * .done(); |
848 | | * ``` |
849 | | * |
850 | | * |
851 | | * > NOTE: Buggy on all browsers |
852 | | * |
853 | | * @api |
854 | | * @method notVisible |
855 | | * @param {string} selector Selector that matches the elements to test |
856 | | * @param {string} message Message for the test reporter |
857 | | * @chainable |
858 | | */ |
859 | | |
860 | 1 | Assertions.prototype.notVisible = function (selector, message) { |
861 | 0 | var hash = uuid.v4(); |
862 | | |
863 | 0 | if (this.test.querying === true) { |
864 | 0 | message = selector; |
865 | 0 | selector = this.test.selector; |
866 | | } |
867 | | |
868 | 0 | var cb = this._generateCallbackAssertion('visible', '!visible', this._testFalsy, hash, {selector: selector, message: message}).bind(this.test); |
869 | 0 | this._addToActionQueue([selector, hash], 'visible', cb); |
870 | 0 | return this.chaining ? this : this.test; |
871 | | }; |
872 | | |
873 | | /** |
874 | | * Asserts that the element matching the provided selector expression is visible. |
875 | | * |
876 | | * ```html |
877 | | * <body> |
878 | | * <h1>Me? So in viewport …</h1> |
879 | | * </body> |
880 | | * ``` |
881 | | * |
882 | | * ```javascript |
883 | | * test |
884 | | * .open('http://allyourviewportsbelongto.us') |
885 | | * .assert.visible('h1', 'Element is visible') |
886 | | * .done(); |
887 | | * ``` |
888 | | * |
889 | | * > NOTE: Buggy on all browsers |
890 | | * |
891 | | * @api |
892 | | * @method visible |
893 | | * @param {string} selector Selector that matches the elements to test |
894 | | * @param {string} message Message for the test reporter |
895 | | * @chainable |
896 | | */ |
897 | | |
898 | 1 | Assertions.prototype.visible = function (selector, message) { |
899 | 0 | var hash = uuid.v4(); |
900 | | |
901 | 0 | if (this.test.querying === true) { |
902 | 0 | message = selector; |
903 | 0 | selector = this.test.selector; |
904 | | } |
905 | | |
906 | 0 | var cb = this._generateCallbackAssertion('visible', 'visible', this._testTruthy, hash, {selector: selector, message: message}).bind(this.test); |
907 | 0 | this._addToActionQueue([selector, hash], 'visible', cb); |
908 | 0 | return this.chaining ? this : this.test; |
909 | | }; |
910 | | |
911 | | /** |
912 | | * Asserts that given text does not exist in the provided selector. |
913 | | * |
914 | | * ```html |
915 | | * <body> |
916 | | * <h1>This is a CasperJS sandbox</h1> |
917 | | * </body> |
918 | | * ``` |
919 | | * |
920 | | * ```javascript |
921 | | * test |
922 | | * .open('http://dalekjs.com/guineapig/') |
923 | | * .assert.doesntHaveText('h1', 'This page is a Dalek sandbox', 'It´s a sandbox!') |
924 | | * .done(); |
925 | | * ``` |
926 | | * |
927 | | * @api |
928 | | * @method doesntHaveText |
929 | | * @param {string} selector Selector that matches the elements to test |
930 | | * @param {string} expected Expected test result |
931 | | * @param {string} message Message for the test reporter |
932 | | * @chainable |
933 | | */ |
934 | | |
935 | 1 | Assertions.prototype.doesntHaveText = function (selector, expected, message) { |
936 | 0 | var hash = uuid.v4(); |
937 | 0 | if (this.test.querying === true) { |
938 | 0 | message = expected; |
939 | 0 | expected = selector; |
940 | 0 | selector = this.test.selector; |
941 | | } |
942 | | |
943 | 0 | var cb = this._generateCallbackAssertion('text', '!text', this._testShallowUnequals, hash, {selector: selector, expected: expected, message: message}).bind(this.test); |
944 | 0 | this._addToActionQueue([selector, expected, hash], 'text', cb); |
945 | 0 | return this.chaining ? this : this.test; |
946 | | }; |
947 | | |
948 | | /** |
949 | | * Asserts that given text does not exist in the current alert/prompt/confirm dialog. |
950 | | * |
951 | | * ```html |
952 | | * <a href="#" id="alert_confirm" onclick="confirm('Confirm me!')">I make confirm</a> |
953 | | * ``` |
954 | | * |
955 | | * ```javascript |
956 | | * test |
957 | | * .open('http://skaaro.com/index.html') |
958 | | * .click('#alert_confirm') |
959 | | * .assert.dialogDoesntHaveText('I am an alert') |
960 | | * .accept() |
961 | | * .done(); |
962 | | * ``` |
963 | | * |
964 | | * > NOTE: Does not work in Firefox & PhantomJS |
965 | | * |
966 | | * @api |
967 | | * @method dialogDoesntHaveText |
968 | | * @param {string} expected Expected test result |
969 | | * @param {string} message Message for the test reporter |
970 | | * @chainable |
971 | | */ |
972 | | |
973 | 1 | Assertions.prototype.dialogDoesntHaveText = function (expected, message) { |
974 | 0 | var hash = uuid.v4(); |
975 | 0 | var cb = this._generateCallbackAssertion('alertText', '!alertText', this._testShallowUnequals, hash, {expected: expected, message: message}).bind(this.test); |
976 | 0 | this._addToActionQueue([expected, hash], 'alertText', cb); |
977 | 0 | return this.chaining ? this : this.test; |
978 | | }; |
979 | | |
980 | | /** |
981 | | * Asserts that given text does exist in the provided selector. |
982 | | * |
983 | | * ```html |
984 | | * <body> |
985 | | * <h1>This is a Dalek sandbox</h1> |
986 | | * </body> |
987 | | * ``` |
988 | | * |
989 | | * ```javascript |
990 | | * test |
991 | | * .open('http://dalekjs.com/guineapig/') |
992 | | * .assert.text('h1', 'This page is a Dalek sandbox', 'Exterminate!') |
993 | | * .done(); |
994 | | * ``` |
995 | | * |
996 | | * of course, text works also with the assertion helpers is() and not() |
997 | | * |
998 | | * ```javascript |
999 | | * test |
1000 | | * .open('http://dalekjs.com/guineapig/') |
1001 | | * .assert.text('h1').is('This page is a Dalek sandbox', 'Exterminate!') |
1002 | | * .done(); |
1003 | | * ``` |
1004 | | * |
1005 | | * ```javascript |
1006 | | * test |
1007 | | * .open('http://dalekjs.com/guineapig/') |
1008 | | * .assert.text('h1').is.not('This page is a CasperJS sandbox', 'Exterminate!') |
1009 | | * .done(); |
1010 | | * ``` |
1011 | | * |
1012 | | * @api |
1013 | | * @method text |
1014 | | * @param {string} selector Selector that matches the elements to test |
1015 | | * @param {string} expected Expected test result |
1016 | | * @param {string} message Message for the test reporter |
1017 | | * @chainable |
1018 | | */ |
1019 | | |
1020 | 1 | Assertions.prototype.text = function (selector, expected, message) { |
1021 | 0 | var hash = uuid.v4(); |
1022 | 0 | if (this.test.querying === true) { |
1023 | 0 | message = expected; |
1024 | 0 | expected = selector; |
1025 | 0 | selector = this.test.selector; |
1026 | | } |
1027 | | |
1028 | 0 | var cb = this._generateCallbackAssertion('text', 'text', this._testShallowEquals, hash, {selector: selector, expected: expected, message: message}).bind(this.test); |
1029 | 0 | this._addToActionQueue([selector, expected, hash], 'text', cb); |
1030 | 0 | return (this.chaining || this.test.querying) ? this : this.test; |
1031 | | }; |
1032 | | |
1033 | | /** |
1034 | | * Asserts that given alertText does exist in the provided alert/confirm or prompt dialog. |
1035 | | * |
1036 | | * ```html |
1037 | | * <a href="#" id="alert" onclick="alert('I am an alert')">I make alerts!</a> |
1038 | | * ``` |
1039 | | * |
1040 | | * ```javascript |
1041 | | * test |
1042 | | * .open('http://skaaro.com/index.html') |
1043 | | * .click('#alert_confirm') |
1044 | | * .assert.dialogText('I am an alert') |
1045 | | * .accept() |
1046 | | * .done(); |
1047 | | * ``` |
1048 | | * |
1049 | | * of course, text works also with the assertion helpers is() and not() |
1050 | | * |
1051 | | * ```javascript |
1052 | | * test |
1053 | | * .open('http://dalekjs.com/guineapig/') |
1054 | | * .assert.dialogText().is('I am an alert', 'Exterminate!') |
1055 | | * .done(); |
1056 | | * ``` |
1057 | | * |
1058 | | * ```javascript |
1059 | | * test |
1060 | | * .open('http://dalekjs.com/guineapig/') |
1061 | | * .assert.dialogText().is.not('I am an prompt', 'Exterminate!') |
1062 | | * .done(); |
1063 | | * ``` |
1064 | | * |
1065 | | * |
1066 | | * > NOTE: Does not work in Firefox & PhantomJS |
1067 | | * |
1068 | | * @api |
1069 | | * @method dialogText |
1070 | | * @param {string} expected Expected testresult |
1071 | | * @param {string} message Message for the test reporter |
1072 | | * @chainable |
1073 | | */ |
1074 | | |
1075 | 1 | Assertions.prototype.dialogText = function (expected, message) { |
1076 | 0 | var hash = uuid.v4(); |
1077 | 0 | var cb = this._generateCallbackAssertion('alertText', 'alertText', this._testShallowEquals, hash, {expected: expected, message: message}).bind(this.test); |
1078 | 0 | this._addToActionQueue([expected, hash], 'alertText', cb); |
1079 | 0 | return (this.chaining || this.test.querying) ? this : this.test; |
1080 | | }; |
1081 | | |
1082 | | /** |
1083 | | * Asserts that the page title is as expected. |
1084 | | * |
1085 | | * ```javascript |
1086 | | * test.open('http://doctorwhotv.co.uk/') |
1087 | | * .assert.title('Doctor Who TV', 'Not your Daleks TV') |
1088 | | * .done(); |
1089 | | * ``` |
1090 | | * |
1091 | | * Yep, using assertion helpers is also possible: |
1092 | | * |
1093 | | * ```javascript |
1094 | | * test.open('http://doctorwhotv.co.uk/') |
1095 | | * .assert.title().is('Doctor Who TV', 'Not your Daleks TV') |
1096 | | * .done(); |
1097 | | * ``` |
1098 | | * |
1099 | | * and the not() helper is available too: |
1100 | | * |
1101 | | * ```javascript |
1102 | | * test.open('http://doctorwhotv.co.uk/') |
1103 | | * .assert.title().is.not('Dalek Emperor TV', 'Not your Daleks TV') |
1104 | | * .done(); |
1105 | | * ``` |
1106 | | * |
1107 | | * @api |
1108 | | * @method title |
1109 | | * @param {string} expected Expected test result |
1110 | | * @param {string} message Message for the test reporter |
1111 | | * @chainable |
1112 | | */ |
1113 | | |
1114 | 1 | Assertions.prototype.title = function (expected, message) { |
1115 | 0 | var hash = uuid.v4(); |
1116 | 0 | var cb = this._generateCallbackAssertion('title', 'title', this._testShallowEquals, hash, {expected: expected, message: message}).bind(this.test); |
1117 | 0 | this._addToActionQueue([expected, hash], 'title', cb); |
1118 | 0 | return this.chaining ? this : this.test; |
1119 | | }; |
1120 | | |
1121 | | /** |
1122 | | * Asserts that given title does not match the given expectations. |
1123 | | * |
1124 | | * ```javascript |
1125 | | * test.open('http://doctorwhotv.co.uk/') |
1126 | | * .assert.doesntHaveTitle('Dalek Emperor TV', 'Not your Daleks TV') |
1127 | | * .done(); |
1128 | | * ``` |
1129 | | * |
1130 | | * @api |
1131 | | * @method doesntHaveTitle |
1132 | | * @param {string} expected Expected test result |
1133 | | * @param {string} message Message for the test reporter |
1134 | | * @chainable |
1135 | | */ |
1136 | | |
1137 | 1 | Assertions.prototype.doesntHaveTitle = function (expected, message) { |
1138 | 0 | var hash = uuid.v4(); |
1139 | 0 | var cb = this._generateCallbackAssertion('title', '!title', this._testShallowUnequals, hash, {expected: expected, message: message}).bind(this.test); |
1140 | 0 | this._addToActionQueue([expected, hash], 'title', cb); |
1141 | 0 | return this.chaining ? this : this.test; |
1142 | | }; |
1143 | | |
1144 | | /** |
1145 | | * Asserts that the pages url is as expected. |
1146 | | * |
1147 | | * ```javascript |
1148 | | * test.open('http://doctorwhotv.co.uk/') |
1149 | | * .assert.url('http://doctorwhotv.co.uk/', 'URL is as expected') |
1150 | | * .done(); |
1151 | | * ``` |
1152 | | * |
1153 | | * You can also check if the protocol changed, |
1154 | | * nice to see when you open GitHub with 'http' instead of 'https' |
1155 | | * |
1156 | | * ```javascript |
1157 | | * test.open('http://github.com') |
1158 | | * .assert.url('https://github.com/', 'Changed prototcols') |
1159 | | * .done(); |
1160 | | * ``` |
1161 | | * |
1162 | | * Yep, using assertion helpers is also possible: |
1163 | | * |
1164 | | * ```javascript |
1165 | | * test.open('http://github.com') |
1166 | | * .assert.url().is('http://doctorwhotv.co.uk/', 'URL is as expected') |
1167 | | * .done(); |
1168 | | * ``` |
1169 | | * |
1170 | | * and the not() helper is available too: |
1171 | | * |
1172 | | * ```javascript |
1173 | | * test.open('http://doctorwhotv.co.uk/') |
1174 | | * .assert.url().is.not('http://doctorwhotv.co.uk/', 'URL is as expected') |
1175 | | * .done(); |
1176 | | * ``` |
1177 | | * |
1178 | | * @api |
1179 | | * @method url |
1180 | | * @param {string} expected Expected test result |
1181 | | * @param {string} message Message for the test reporter |
1182 | | * @chainable |
1183 | | */ |
1184 | | |
1185 | 1 | Assertions.prototype.url = function (expected, message) { |
1186 | 0 | var hash = uuid.v4(); |
1187 | 0 | var cb = this._generateCallbackAssertion('url', 'url', this._testShallowEquals, hash, {expected: expected, message: message}).bind(this.test); |
1188 | 0 | this._addToActionQueue([expected, hash], 'url', cb); |
1189 | 0 | return this.chaining ? this : this.test; |
1190 | | }; |
1191 | | |
1192 | | /** |
1193 | | * Asserts that the pages URL does not match the expectation. |
1194 | | * |
1195 | | * ```javascript |
1196 | | * test.open('http://doctorwhotv.co.uk/') |
1197 | | * .assert.doesntHaveUrl('http://doctorwhotv.co.uk/', 'URL is not expected') |
1198 | | * .done(); |
1199 | | * ``` |
1200 | | * |
1201 | | * @api |
1202 | | * @method doesntHaveUrl |
1203 | | * @param {string} expected Expected test result |
1204 | | * @param {string} message Message for the test reporter |
1205 | | * @chainable |
1206 | | */ |
1207 | | |
1208 | 1 | Assertions.prototype.doesntHaveUrl = function (expected, message) { |
1209 | 0 | var hash = uuid.v4(); |
1210 | 0 | var cb = this._generateCallbackAssertion('url', '!url', this._testShallowUnequals, hash, {expected: expected, message: message}).bind(this.test); |
1211 | 0 | this._addToActionQueue([expected, hash], 'url', cb); |
1212 | 0 | return this.chaining ? this : this.test; |
1213 | | }; |
1214 | | |
1215 | | /** |
1216 | | * Asserts that an elements attribute is as expected. |
1217 | | * |
1218 | | * ```html |
1219 | | * <form> |
1220 | | * <button class="jumpButton" type="submit">Fire</button> |
1221 | | * </form> |
1222 | | * ``` |
1223 | | * |
1224 | | * ```javascript |
1225 | | * test |
1226 | | * .open('http://dalekjs.com/guineapig/') |
1227 | | * .assert.attr('.jumpButton', 'type', 'submit') |
1228 | | * .done(); |
1229 | | * ``` |
1230 | | * |
1231 | | * ```html |
1232 | | * <div id="dataDiv" data-spot="cat"></div> |
1233 | | * ``` |
1234 | | * |
1235 | | * ```javascript |
1236 | | * test |
1237 | | * .open('http://dalekjs.com/guineapig/') |
1238 | | * .assert.attr('#dataDiv').is('data-spot', 'cat', 'We found Dataʼs cat!') |
1239 | | * .done(); |
1240 | | * ``` |
1241 | | * |
1242 | | * ```javascript |
1243 | | * test |
1244 | | * .open('http://dalekjs.com/guineapig/') |
1245 | | * .assert.attr('#dataDiv').is.not('data-spot', 'doc', 'Spot is not a dog!') |
1246 | | * .done(); |
1247 | | * ``` |
1248 | | * |
1249 | | * @api |
1250 | | * @method attr |
1251 | | * @param {string} selector Selector that matches the elements to test |
1252 | | * @param {string} attribute The attribute to test |
1253 | | * @param {string} expected Expected test result |
1254 | | * @param {string} message Message for the test reporter |
1255 | | * @chainable |
1256 | | */ |
1257 | | |
1258 | 1 | Assertions.prototype.attr = function (selector, attribute, expected, message) { |
1259 | 0 | var hash = uuid.v4(); |
1260 | | |
1261 | 0 | if (this.test.querying === true) { |
1262 | 0 | message = expected; |
1263 | 0 | expected = attribute; |
1264 | 0 | attribute = selector; |
1265 | 0 | selector = this.test.selector; |
1266 | | } |
1267 | | |
1268 | 0 | var cb = this._generateCallbackAssertion('attribute', 'attribute', this._testShallowEquals, hash, {expected: expected, message: message, selector: selector, attribute: attribute}).bind(this.test); |
1269 | 0 | this._addToActionQueue([selector, attribute, expected, hash], 'attribute', cb); |
1270 | 0 | return this.chaining ? this : this.test; |
1271 | | }; |
1272 | | |
1273 | | // TEST HELPER |
1274 | | // ----------- |
1275 | | |
1276 | | /** |
1277 | | * Is helper |
1278 | | * |
1279 | | * @method is |
1280 | | * @param {mixed} expected Value to check |
1281 | | * @param {string} message Test message |
1282 | | * @chainable |
1283 | | */ |
1284 | | |
1285 | 1 | Assertions.prototype.is = function (expected, message) { |
1286 | 0 | return this.generateTestHelper('is', '_testShallowEquals', false)(expected, message); |
1287 | | }; |
1288 | | |
1289 | | /** |
1290 | | * Not helper |
1291 | | * |
1292 | | * @method not |
1293 | | * @param {mixed} expected Value to check |
1294 | | * @param {string} message Test message |
1295 | | * @chainable |
1296 | | */ |
1297 | | |
1298 | 1 | Assertions.prototype.not = function (expected, message) { |
1299 | 0 | return this.generateTestHelper('not', '_testShallowEquals', true)(expected, message); |
1300 | | }; |
1301 | | |
1302 | | /** |
1303 | | * Between helper |
1304 | | * |
1305 | | * @method between |
1306 | | * @param {mixed} expected Value to check |
1307 | | * @param {string} message Test message |
1308 | | * @chainable |
1309 | | */ |
1310 | | |
1311 | 1 | Assertions.prototype.between = function (expected, message) { |
1312 | 0 | return this.generateTestHelper('between', '_testBetween', false)(expected, message); |
1313 | | }; |
1314 | | |
1315 | | /** |
1316 | | * Gt helper |
1317 | | * |
1318 | | * @method gt |
1319 | | * @param {mixed} expected Value to check |
1320 | | * @param {string} message Test message |
1321 | | * @chainable |
1322 | | */ |
1323 | | |
1324 | 1 | Assertions.prototype.gt = function (expected, message) { |
1325 | 0 | return this.generateTestHelper('gt', '_testGreaterThan', false)(expected, message); |
1326 | | }; |
1327 | | |
1328 | | /** |
1329 | | * Gte helper |
1330 | | * |
1331 | | * @method gte |
1332 | | * @param {mixed} expected Value to check |
1333 | | * @param {string} message Test message |
1334 | | * @chainable |
1335 | | */ |
1336 | | |
1337 | 1 | Assertions.prototype.gte = function (expected, message) { |
1338 | 0 | return this.generateTestHelper('gte', '_testGreaterThanEqual', false)(expected, message); |
1339 | | }; |
1340 | | |
1341 | | /** |
1342 | | * Lt helper |
1343 | | * |
1344 | | * @method lt |
1345 | | * @param {mixed} expected Value to check |
1346 | | * @param {string} message Test message |
1347 | | * @chainable |
1348 | | */ |
1349 | | |
1350 | 1 | Assertions.prototype.lt = function (expected, message) { |
1351 | 0 | return this.generateTestHelper('lt', '_testLowerThan', false)(expected, message); |
1352 | | }; |
1353 | | |
1354 | | /** |
1355 | | * Lte helper |
1356 | | * |
1357 | | * @method lte |
1358 | | * @param {mixed} expected Value to check |
1359 | | * @param {string} message Test message |
1360 | | * @chainable |
1361 | | */ |
1362 | | |
1363 | 1 | Assertions.prototype.lte = function (expected, message) { |
1364 | 0 | return this.generateTestHelper('lte', '_testLowerThanEqual', false)(expected, message); |
1365 | | }; |
1366 | | |
1367 | | /** |
1368 | | * Contain helper |
1369 | | * |
1370 | | * @method contain |
1371 | | * @param {mixed} expected Value to check |
1372 | | * @param {string} message Test message |
1373 | | * @chainable |
1374 | | */ |
1375 | | |
1376 | 1 | Assertions.prototype.contain = function (expected, message) { |
1377 | 0 | return this.generateTestHelper('contain', '_contain', false)(expected, message); |
1378 | | }; |
1379 | | |
1380 | | /** |
1381 | | * Match helper |
1382 | | * |
1383 | | * @method match |
1384 | | * @param {string} expected Regex to match on |
1385 | | * @param {string} message Test message |
1386 | | * @chainable |
1387 | | */ |
1388 | | |
1389 | 1 | Assertions.prototype.match = function (expected, message) { |
1390 | 0 | return this.generateTestHelper('match', '_match', false)(expected, message); |
1391 | | }; |
1392 | | |
1393 | | // HELPER METHODS |
1394 | | // -------------- |
1395 | | |
1396 | | /** |
1397 | | * Generates a callback that will be fired when the action has been completed. |
1398 | | * The callback itself will then validate the answer and will also emit an event |
1399 | | * that the action has been successfully executed. |
1400 | | * |
1401 | | * @method _generateCallbackAssertion |
1402 | | * @param {string} key Unique key of the action |
1403 | | * @param {string} type Type of the action (usually the actions name) |
1404 | | * @return {function} The generated callback function |
1405 | | * @private |
1406 | | */ |
1407 | | |
1408 | 1 | Assertions.prototype._generateCallbackAssertion = function (key, type, test, hash, opts) { |
1409 | 0 | var cb = function (data) { |
1410 | 0 | if (data && data.key === key && data.hash === hash) { |
1411 | | |
1412 | 0 | this._lastGeneratedAction = {key: key, type: type, test: test, hash: hash, opts: opts, data: data}; |
1413 | | |
1414 | 0 | if (!opts.expected && (key === 'title' || key === 'width' || key === 'height' || key === 'url' || key === 'text' || key === 'attribute' || key === 'numberOfElements' || key === 'numberOfVisibleElements')) { |
1415 | 0 | return false; |
1416 | | } |
1417 | | |
1418 | 0 | if (typeof opts.expected === 'function') { |
1419 | 0 | opts.expected = opts.expected(); |
1420 | | } |
1421 | | |
1422 | 0 | var testResult = test(data.value, opts.expected); |
1423 | | |
1424 | 0 | this.reporter.emit('report:assertion', { |
1425 | | success: testResult, |
1426 | | expected: opts.expected, |
1427 | | value: data.value, |
1428 | | message: opts.message, |
1429 | | type: type |
1430 | | }); |
1431 | | |
1432 | 0 | this.incrementExpectations(); |
1433 | 0 | if (!testResult) { |
1434 | 0 | this.incrementFailedAssertions(); |
1435 | | } |
1436 | | } |
1437 | | }; |
1438 | 0 | return cb; |
1439 | | }; |
1440 | | |
1441 | | /** |
1442 | | * Adds a method to the queue of actions/assertions to execute |
1443 | | * |
1444 | | * @method _addToActionQueue |
1445 | | * @param {object} opts Options of the action to invoke |
1446 | | * @param {string} driverMethod Name of the method to call on the driver |
1447 | | * @param {function} A callback function that will be executed when the action has been executed |
1448 | | * @private |
1449 | | * @chainable |
1450 | | */ |
1451 | | |
1452 | 1 | Assertions.prototype._addToActionQueue = function (opts, driverMethod, cb) { |
1453 | 0 | this._lastGeneratedShit = {opts: opts, driverMethod: driverMethod}; |
1454 | 0 | this.test.actionPromiseQueue.push(function () { |
1455 | 0 | var deferredAction = Q.defer(); |
1456 | 0 | this.test.driver[driverMethod].apply(this.test.driver, opts); |
1457 | 0 | deferredAction.resolve(); |
1458 | 0 | this.test.driver.events.on('driver:message', cb); |
1459 | 0 | return deferredAction.promise; |
1460 | | }.bind(this)); |
1461 | 0 | return this; |
1462 | | }; |
1463 | | |
1464 | | /** |
1465 | | * Generates a function that can be used |
1466 | | * |
1467 | | * @method generateTestHelper |
1468 | | * @param name |
1469 | | * @param assertionFn |
1470 | | * @param negate |
1471 | | * @return |
1472 | | * @private |
1473 | | */ |
1474 | | |
1475 | 1 | Assertions.prototype.generateTestHelper = function (name, assertionFn, negate) { |
1476 | 0 | return function (expected, message) { |
1477 | 0 | var gen = this._lastGeneratedShit; |
1478 | | |
1479 | 0 | this.test.actionPromiseQueue.push(function () { |
1480 | 0 | var deferredAction = Q.defer(); |
1481 | 0 | deferredAction.resolve(); |
1482 | 0 | this.test.driver.events.on('driver:message', function () { |
1483 | | |
1484 | 0 | if (gen.opts && gen.opts[(gen.opts.length - 1)] && this.test._lastGeneratedAction && this.test._lastGeneratedAction.hash) { |
1485 | 0 | if (gen.opts[(gen.opts.length - 1)] === this.test._lastGeneratedAction.hash && !this.proceeded[this.test._lastGeneratedAction.hash + name]) { |
1486 | 0 | var testResult = this[assertionFn](expected, this.test._lastGeneratedAction.data.value); |
1487 | | |
1488 | 0 | if (negate) { |
1489 | 0 | testResult = !testResult; |
1490 | | } |
1491 | | |
1492 | 0 | this.proceeded[this.test._lastGeneratedAction.hash + name] = true; |
1493 | | |
1494 | 0 | this.test.reporter.emit('report:assertion', { |
1495 | | success: testResult, |
1496 | | expected: expected, |
1497 | | value: this.test._lastGeneratedAction.data.value, |
1498 | | message: message, |
1499 | | type: this.test._lastGeneratedAction.type |
1500 | | }); |
1501 | | |
1502 | 0 | this.test.incrementExpectations(); |
1503 | | |
1504 | 0 | if (!testResult) { |
1505 | 0 | this.test.incrementFailedAssertions(); |
1506 | | } |
1507 | | } |
1508 | | } |
1509 | | }.bind(this)); |
1510 | 0 | return deferredAction.promise; |
1511 | | }.bind(this)); |
1512 | | |
1513 | 0 | return this.chaining ? this : this.test; |
1514 | | }.bind(this); |
1515 | | }; |
1516 | | |
1517 | | // ASSERT METHODS |
1518 | | // -------------- |
1519 | | |
1520 | | /** |
1521 | | * Assert if a given value shallow equals a second given value |
1522 | | * |
1523 | | * @method _testShallowEquals |
1524 | | * @param {mixed} a Value to test |
1525 | | * @param {mixed} b Value to test |
1526 | | * @return {bool} false if values donʼt match, true if they match |
1527 | | * @private |
1528 | | */ |
1529 | | |
1530 | 1 | Assertions.prototype._testShallowEquals = function (a, b) { |
1531 | 0 | try { |
1532 | 0 | chai.assert.equal(a, b); |
1533 | | } catch (e) { |
1534 | 0 | return false; |
1535 | | } |
1536 | | |
1537 | 0 | return true; |
1538 | | }; |
1539 | | |
1540 | | /** |
1541 | | * Assert if a given value shallow does not equal a second given value |
1542 | | * |
1543 | | * @method _testShallowUnequals |
1544 | | * @param {mixed} a Value to test |
1545 | | * @param {mixed} b Value to test |
1546 | | * @return {bool} true if values donʼt match, false if they match |
1547 | | * @private |
1548 | | */ |
1549 | | |
1550 | 1 | Assertions.prototype._testShallowUnequals = function (a, b) { |
1551 | 0 | try { |
1552 | 0 | chai.assert.notEqual(a, b); |
1553 | | } catch (e) { |
1554 | 0 | return false; |
1555 | | } |
1556 | | |
1557 | 0 | return true; |
1558 | | }; |
1559 | | |
1560 | | /** |
1561 | | * Assert if a given value matches a range |
1562 | | * |
1563 | | * @method _testBetween |
1564 | | * @param {array} a Range to test |
1565 | | * @param {bool} b Value to compare |
1566 | | * @return {bool} test result |
1567 | | * @private |
1568 | | */ |
1569 | | |
1570 | 1 | Assertions.prototype._testBetween = function (a, b) { |
1571 | 0 | try { |
1572 | 0 | chai.expect(b).to.be.within(a[0], a[1]); |
1573 | | } catch (e) { |
1574 | 0 | return false; |
1575 | | } |
1576 | | |
1577 | 0 | return true; |
1578 | | }; |
1579 | | |
1580 | | /** |
1581 | | * Assert if a given value is greater than the value to compare |
1582 | | * |
1583 | | * @method _testGreaterThan |
1584 | | * @param {bool} a Value to test |
1585 | | * @param {bool} b Value to compare |
1586 | | * @return {bool} test result |
1587 | | * @private |
1588 | | */ |
1589 | | |
1590 | 1 | Assertions.prototype._testGreaterThan = function (a, b) { |
1591 | 0 | try { |
1592 | 0 | chai.expect(b).to.be.above(a); |
1593 | | } catch (e) { |
1594 | 0 | return false; |
1595 | | } |
1596 | | |
1597 | 0 | return true; |
1598 | | }; |
1599 | | |
1600 | | /** |
1601 | | * Assert if a given value is greater or equal than the value to compare |
1602 | | * |
1603 | | * @method _testGreaterThanEqual |
1604 | | * @param {bool} a Value to test |
1605 | | * @param {bool} b Value to compare |
1606 | | * @return {bool} test result |
1607 | | * @private |
1608 | | */ |
1609 | | |
1610 | 1 | Assertions.prototype._testGreaterThanEqual = function (a, b) { |
1611 | 0 | return this._testGreaterThan(a - 1, b); |
1612 | | }; |
1613 | | |
1614 | | /** |
1615 | | * Assert if a given value is lower than the value to compare |
1616 | | * |
1617 | | * @method _testLowerThan |
1618 | | * @param {bool} a Value to test |
1619 | | * @param {bool} b Value to compare |
1620 | | * @return {bool} test result |
1621 | | * @private |
1622 | | */ |
1623 | | |
1624 | 1 | Assertions.prototype._testLowerThan = function (a, b) { |
1625 | 0 | try { |
1626 | 0 | chai.expect(b).to.be.below(a); |
1627 | | } catch (e) { |
1628 | 0 | return false; |
1629 | | } |
1630 | | |
1631 | 0 | return true; |
1632 | | }; |
1633 | | |
1634 | | /** |
1635 | | * Assert if a given value contain another value |
1636 | | * |
1637 | | * @method _contain |
1638 | | * @param {bool} a Value to test |
1639 | | * @param {bool} b Value to compare |
1640 | | * @return {bool} test result |
1641 | | * @private |
1642 | | */ |
1643 | | |
1644 | 1 | Assertions.prototype._contain = function (a, b) { |
1645 | 0 | try { |
1646 | 0 | chai.expect(b).to.include(a); |
1647 | | } catch (e) { |
1648 | 0 | return false; |
1649 | | } |
1650 | | |
1651 | 0 | return true; |
1652 | | }; |
1653 | | |
1654 | | /** |
1655 | | * Assert if a given value is lower or equal than the value to compare |
1656 | | * |
1657 | | * @method _testLowerThanEqual |
1658 | | * @param {bool} a Value to test |
1659 | | * @param {bool} b Value to compare |
1660 | | * @return {bool} test result |
1661 | | * @private |
1662 | | */ |
1663 | | |
1664 | 1 | Assertions.prototype._testLowerThanEqual = function (a, b) { |
1665 | 0 | return this._testLowerThan(a + 1, b); |
1666 | | }; |
1667 | | |
1668 | | /** |
1669 | | * Assert if a given value is boolean 'true' |
1670 | | * |
1671 | | * @method _testTruthy |
1672 | | * @param {bool} a Value to test |
1673 | | * @return {bool} false if value is false, true if value is true |
1674 | | * @private |
1675 | | */ |
1676 | | |
1677 | 1 | Assertions.prototype._testTruthy = function (a) { |
1678 | 0 | return a === 'true' || a === true; |
1679 | | }; |
1680 | | |
1681 | | /** |
1682 | | * Assert if a given value is boolean 'false' |
1683 | | * |
1684 | | * @method _testFalsy |
1685 | | * @param {bool} a Value to test |
1686 | | * @return {bool} true if value is false, false if value is true |
1687 | | * @private |
1688 | | */ |
1689 | | |
1690 | 1 | Assertions.prototype._testFalsy = function (a) { |
1691 | 0 | return a === 'false' || a === false; |
1692 | | }; |
1693 | | |
1694 | | /** |
1695 | | * Assert a given value matches a RegEx |
1696 | | * |
1697 | | * @method _contain |
1698 | | * @param {mixed} a Value to test |
1699 | | * @param {string} b Value to compare |
1700 | | * @return {bool} test result |
1701 | | * @private |
1702 | | */ |
1703 | | |
1704 | 1 | Assertions.prototype._match = function (a, b) { |
1705 | 0 | try { |
1706 | 0 | chai.expect(b).to.match(a); |
1707 | | } catch (e) { |
1708 | 0 | return false; |
1709 | | } |
1710 | | |
1711 | 0 | return true; |
1712 | | }; |
1713 | | |