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() {
* @return Boolean
*/
Casper.prototype.click = function click(selector) {
this.log("Click on selector: " + selector, "debug");
if (arguments.length > 1) {
this.emit("deprecated", "The click() method does not process the fallbackToHref argument since 0.6");
}
return this.mouseEvent('click', selector);
};
/**
* Emulates an event on the element from the provided selector using the mouse
* pointer, if possible.
*
* In case of success, `true` is returned, `false` otherwise.
*
* @param String type Type of event to emulate
* @param String selector A DOM CSS3 compatible selector
* @return Boolean
*/
Casper.prototype.mouseEvent = function mouseEvent(type, selector) {
this.log("Mouse event '" + type + "' on selector: " + selector, "debug");
if (!this.exists(selector)) {
throw new CasperError("Cannot click on unexistent selector: " + selector);
throw new CasperError("Cannot dispath an event on nonexistent selector: " + selector);
}
var clicked = this.evaluate(function(selector) {
return __utils__.click(selector);
}, { selector: selector });
if (!clicked) {
var eventSuccess = this.evaluate(function(type, selector) {
return __utils__.mouseEvent(type, selector);
}, {
type: type,
selector: selector
});
if (!eventSuccess) {
// fallback onto native QtWebKit mouse events
try {
this.mouse.click(selector);
this.mouse.processEvent(type, selector);
} catch (e) {
this.log(f("Error while trying to click on selector %s: %s", selector, e), "error");
this.log(f("Error while trying to emulate event %s on selector %s: %s", type, selector, e), "error");
return false;
}
}
......
......@@ -49,25 +49,36 @@
);
/**
* Clicks on the DOM element behind the provided selector.
* Dispatches a mouse event to the DOM element behind the provided selector.
*
* @param String type Type of event to dispatch
* @param String selector A CSS3 selector to the element to click
* @return Boolean
*/
this.click = function(selector) {
this.mouseEvent = function(type, selector) {
var elem = this.findOne(selector);
if (!elem) {
this.log("click(): Couldn't find any element matching '" + selector + "' selector", "error");
this.log("mouseEvent(): Couldn't find any element matching '" + selector + "' selector", "error");
return false;
}
var evt = document.createEvent("MouseEvents");
evt.initMouseEvent("click", true, true, window, 1, 1, 1, 1, 1, false, false, false, false, 0, elem);
evt.initMouseEvent(type, true, true, window, 1, 1, 1, 1, 1, false, false, false, false, 0, elem);
// dispatchEvent return value is false if at least one of the event
// handlers which handled this event called preventDefault
return elem.dispatchEvent(evt);
};
/**
* Clicks on the DOM element behind the provided selector.
*
* @param String selector A CSS3 selector to the element to click
* @return Boolean
*/
this.click = function(selector) {
return this.mouseEvent('click', selector);
};
/**
* Decodes a base64 encoded string. Succeeds where window.atob() fails.
*
* @param String str The base64 encoded contents
......
......@@ -78,6 +78,8 @@ var Mouse = function(casper) {
throw new CasperError('Mouse.processEvent(): Too many arguments');
}
}
this.processEvent = processEvent;
this.click = function click() {
processEvent('click', arguments);
......