Showing
4 changed files
with
0 additions
and
103 deletions
... | @@ -869,38 +869,6 @@ Casper.prototype.mouseEvent = function mouseEvent(type, selector) { | ... | @@ -869,38 +869,6 @@ Casper.prototype.mouseEvent = function mouseEvent(type, selector) { |
869 | }; | 869 | }; |
870 | 870 | ||
871 | /** | 871 | /** |
872 | * Emulates an keyboard event on the element from the provided selector | ||
873 | * | ||
874 | * In case of success, `true` is returned, `false` otherwise. | ||
875 | * | ||
876 | * @param String type Type of event to emulate | ||
877 | * @param String selector A DOM CSS3 compatible selector | ||
878 | * @param String character Character value of the key pressed | ||
879 | * @param String key Key value of the key pressed | ||
880 | * @return Boolean | ||
881 | */ | ||
882 | Casper.prototype.keyboardEvent = function keyboardEvent(type, selector, character, key) { | ||
883 | "use strict"; | ||
884 | this.log("Keyboard event '" + type + "' on selector: " + selector, "debug"); | ||
885 | if (!this.exists(selector)) { | ||
886 | throw new CasperError(f("Cannot dispatch %s event on nonexistent selector: %s", type, selector)); | ||
887 | } | ||
888 | var eventSuccess = this.evaluate(function(type, selector, character, key) { | ||
889 | return window.__utils__.keyboardEvent(type, selector, character, key); | ||
890 | }, { | ||
891 | type: type, | ||
892 | selector: selector, | ||
893 | character: character, | ||
894 | key: key | ||
895 | }); | ||
896 | if (!eventSuccess) { | ||
897 | this.log(f("Couldn't emulate '%s' event on %s: %s", type, selector, e), "error"); | ||
898 | return false; | ||
899 | } | ||
900 | return true; | ||
901 | }; | ||
902 | |||
903 | /** | ||
904 | * Performs an HTTP request, with optional settings. | 872 | * Performs an HTTP request, with optional settings. |
905 | * | 873 | * |
906 | * Available settings are: | 874 | * Available settings are: | ... | ... |
... | @@ -415,31 +415,6 @@ | ... | @@ -415,31 +415,6 @@ |
415 | }; | 415 | }; |
416 | 416 | ||
417 | /** | 417 | /** |
418 | * Dispatches a keyboard event to the DOM element behind the provided selector. | ||
419 | * | ||
420 | * @see http://www.w3.org/TR/DOM-Level-3-Events/#events-KeyboardEvent | ||
421 | * | ||
422 | * @param String type Type of event to dispatch | ||
423 | * @param String selector A CSS3 selector to the element to click | ||
424 | * @param String character Character value of the key pressed | ||
425 | * @param String key Key value of the key pressed | ||
426 | * @return Boolean | ||
427 | */ | ||
428 | this.keyboardEvent = function keyboardEvent(type, selector, character, key) { | ||
429 | var elem = this.findOne(selector); | ||
430 | if (!elem) { | ||
431 | this.log("keyboardEvent(): Couldn't find any element matching '" + selector + "' selector", "error"); | ||
432 | return false; | ||
433 | } | ||
434 | var evt = document.createEvent("KeyboardEvents"); | ||
435 | evt.initKeyboardEvent(type, true, true, window, character, key, false, false, false, false, false); | ||
436 | elem.value += character; | ||
437 | // dispatchEvent return value is false if at least one of the event | ||
438 | // handlers which handled this event called preventDefault | ||
439 | return elem.dispatchEvent(evt); | ||
440 | }; | ||
441 | |||
442 | /** | ||
443 | * Processes a selector input, either as a string or an object. | 418 | * Processes a selector input, either as a string or an object. |
444 | * | 419 | * |
445 | * If passed an object, if must be of the form: | 420 | * If passed an object, if must be of the form: | ... | ... |
tests/site/keyboard-events.html
deleted
100644 → 0
1 | <!DOCTYPE html> | ||
2 | <html> | ||
3 | <head> | ||
4 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | ||
5 | <title>CasperJS test keyboard events</title> | ||
6 | </head> | ||
7 | <body> | ||
8 | <form> | ||
9 | <textarea id="test1"></textarea> | ||
10 | <select id="test2"> | ||
11 | <option value="1">1</option> | ||
12 | <option value="2">2</option> | ||
13 | <option value="3">3</option> | ||
14 | <option value="4">4</option> | ||
15 | <option value="5">5</option> | ||
16 | <option value="6">6</option> | ||
17 | </select> | ||
18 | </form> | ||
19 | </body> | ||
20 | </html> | ||
21 |
1 | casper.start('tests/site/keyboard-events.html'); | ||
2 | |||
3 | casper.then(function() { | ||
4 | this.test.comment('CasperUtils.keyboardEvent()'); | ||
5 | |||
6 | this.test.assert(this.keyboardEvent('keypress', '#test1', '0'), 'CasperUtils.keyboardEvent() can dispatch a keypress event'); | ||
7 | this.test.assert(this.keyboardEvent('keydown', '#test1', '1'), 'CasperUtils.keyboardEvent() can dispatch a keydown event'); | ||
8 | this.test.assert(this.keyboardEvent('keyup', '#test1', '2'), 'CasperUtils.keyboardEvent() can dispatch a keyup event'); | ||
9 | this.test.assert(this.keyboardEvent('keydown', '#test2', 'Down'), 'CasperUtils.keyboardEvent() can dispatch an arrow key'); | ||
10 | this.keyboardEvent('keydown', '#test2', 'Down'); | ||
11 | this.keyboardEvent('keydown', '#test2', 'Down'); | ||
12 | |||
13 | var results = this.getGlobal('results'); | ||
14 | this.test.assertEvalEquals(function() { | ||
15 | return document.querySelector('#test1').value; | ||
16 | }, "012", 'CasperUtils.keyboardEvent() can set the value of a textarea'); | ||
17 | this.test.assertEvalEquals(function() { | ||
18 | return document.querySelector('#test2').selectedIndex; | ||
19 | }, 3, 'CasperUtils.keyboardEvent() can update the value of a select'); | ||
20 | }); | ||
21 | |||
22 | casper.run(function() { | ||
23 | this.test.done(); | ||
24 | }); | ||
25 |
-
Please register or sign in to post a comment