Commit 66093a30 66093a30a44f2ccefff3e36ea639abb9b4232c62 by nrabinowitz

Adding support for the full range of mouse events

1 parent f2d7dd2e
...@@ -252,22 +252,36 @@ Casper.prototype.clear = function clear() { ...@@ -252,22 +252,36 @@ Casper.prototype.clear = function clear() {
252 * @return Boolean 252 * @return Boolean
253 */ 253 */
254 Casper.prototype.click = function click(selector) { 254 Casper.prototype.click = function click(selector) {
255 this.log("Click on selector: " + selector, "debug"); 255 return this.mouseEvent('click', selector);
256 if (arguments.length > 1) { 256 };
257 this.emit("deprecated", "The click() method does not process the fallbackToHref argument since 0.6"); 257
258 } 258 /**
259 * Emulates an event on the element from the provided selector using the mouse
260 * pointer, if possible.
261 *
262 * In case of success, `true` is returned, `false` otherwise.
263 *
264 * @param String type Type of event to emulate
265 * @param String selector A DOM CSS3 compatible selector
266 * @return Boolean
267 */
268 Casper.prototype.mouseEvent = function mouseEvent(type, selector) {
269 this.log("Mouse event '" + type + "' on selector: " + selector, "debug");
259 if (!this.exists(selector)) { 270 if (!this.exists(selector)) {
260 throw new CasperError("Cannot click on unexistent selector: " + selector); 271 throw new CasperError("Cannot dispath an event on nonexistent selector: " + selector);
261 } 272 }
262 var clicked = this.evaluate(function(selector) { 273 var eventSuccess = this.evaluate(function(type, selector) {
263 return __utils__.click(selector); 274 return __utils__.mouseEvent(type, selector);
264 }, { selector: selector }); 275 }, {
265 if (!clicked) { 276 type: type,
277 selector: selector
278 });
279 if (!eventSuccess) {
266 // fallback onto native QtWebKit mouse events 280 // fallback onto native QtWebKit mouse events
267 try { 281 try {
268 this.mouse.click(selector); 282 this.mouse.processEvent(type, selector);
269 } catch (e) { 283 } catch (e) {
270 this.log(f("Error while trying to click on selector %s: %s", selector, e), "error"); 284 this.log(f("Error while trying to emulate event %s on selector %s: %s", type, selector, e), "error");
271 return false; 285 return false;
272 } 286 }
273 } 287 }
......
...@@ -49,25 +49,36 @@ ...@@ -49,25 +49,36 @@
49 ); 49 );
50 50
51 /** 51 /**
52 * Clicks on the DOM element behind the provided selector. 52 * Dispatches a mouse event to the DOM element behind the provided selector.
53 * 53 *
54 * @param String type Type of event to dispatch
54 * @param String selector A CSS3 selector to the element to click 55 * @param String selector A CSS3 selector to the element to click
55 * @return Boolean 56 * @return Boolean
56 */ 57 */
57 this.click = function(selector) { 58 this.mouseEvent = function(type, selector) {
58 var elem = this.findOne(selector); 59 var elem = this.findOne(selector);
59 if (!elem) { 60 if (!elem) {
60 this.log("click(): Couldn't find any element matching '" + selector + "' selector", "error"); 61 this.log("mouseEvent(): Couldn't find any element matching '" + selector + "' selector", "error");
61 return false; 62 return false;
62 } 63 }
63 var evt = document.createEvent("MouseEvents"); 64 var evt = document.createEvent("MouseEvents");
64 evt.initMouseEvent("click", true, true, window, 1, 1, 1, 1, 1, false, false, false, false, 0, elem); 65 evt.initMouseEvent(type, true, true, window, 1, 1, 1, 1, 1, false, false, false, false, 0, elem);
65 // dispatchEvent return value is false if at least one of the event 66 // dispatchEvent return value is false if at least one of the event
66 // handlers which handled this event called preventDefault 67 // handlers which handled this event called preventDefault
67 return elem.dispatchEvent(evt); 68 return elem.dispatchEvent(evt);
68 }; 69 };
69 70
70 /** 71 /**
72 * Clicks on the DOM element behind the provided selector.
73 *
74 * @param String selector A CSS3 selector to the element to click
75 * @return Boolean
76 */
77 this.click = function(selector) {
78 return this.mouseEvent('click', selector);
79 };
80
81 /**
71 * Decodes a base64 encoded string. Succeeds where window.atob() fails. 82 * Decodes a base64 encoded string. Succeeds where window.atob() fails.
72 * 83 *
73 * @param String str The base64 encoded contents 84 * @param String str The base64 encoded contents
......
...@@ -79,6 +79,8 @@ var Mouse = function(casper) { ...@@ -79,6 +79,8 @@ var Mouse = function(casper) {
79 } 79 }
80 } 80 }
81 81
82 this.processEvent = processEvent;
83
82 this.click = function click() { 84 this.click = function click() {
83 processEvent('click', arguments); 85 processEvent('click', arguments);
84 }; 86 };
......