Commit e3d59675 e3d59675d13f9bde63f2796d7acec72aa70921b7 by Nicolas Perriault

added Casper.mouseClick() method for native mouse click emulation using native events

1 parent 7a2f3420
......@@ -178,17 +178,7 @@ Casper.prototype = {
*/
captureSelector: function(targetFile, selector) {
return this.capture(targetFile, this.evaluate(function(selector) {
try {
var clipRect = document.querySelector(selector).getBoundingClientRect();
return {
top: clipRect.top,
left: clipRect.left,
width: clipRect.width,
height: clipRect.height
};
} catch (e) {
__utils__.log("Unable to fetch bounds for element " + selector, "warning");
}
return __utils__.getElementBounds();
}, { selector: selector }));
},
......@@ -597,6 +587,25 @@ Casper.prototype = {
},
/**
* Emulates a click on an HTML element matching a given CSS3 selector,
* using the mouse pointer.
*
* @param String selector A DOM CSS3 compatible selector
* @return Casper
*/
mouseClick: function(selector) {
var bounds = this.evaluate(function(selector) {
return __utils__.getElementBounds(selector);
}, { selector: selector });
if (utils.isClipRect(bounds)) {
var x = bounds.left + Math.floor(bounds.width / 2);
var y = bounds.top + Math.floor(bounds.height / 2);
this.page.sendEvent('click', x, y);
}
return this;
},
/**
* Opens a page. Takes only one argument, the url to open (using the
* callback argument would defeat the whole purpose of Casper
* actually).
......
......@@ -351,6 +351,27 @@
};
/**
* Retrieves bounding rect coordinates of the HTML element matching the
* provided CSS3 selector
*
* @param String selector
* @return Object or null
*/
this.getElementBounds = function(selector) {
try {
var clipRect = document.querySelector(selector).getBoundingClientRect();
return {
top: clipRect.top,
left: clipRect.left,
width: clipRect.width,
height: clipRect.height
};
} catch (e) {
this.log("Unable to fetch bounds for element " + selector, "warning");
}
};
/**
* Logs a message. Will format the message a way CasperJS will be able
* to log phantomjs side.
*
......
......@@ -85,6 +85,11 @@ function fillBlanks(text, pad) {
}
exports.fillBlanks = fillBlanks;
function isArray(value) {
return isType(value, "array");
}
exports.isArray = isArray;
/**
* Checks if passed argument is an instance of Capser object.
*
......@@ -96,6 +101,22 @@ function isCasperObject(value) {
}
exports.isCasperObject = isCasperObject;
function isClipRect(value) {
return isType(value, "cliprect") || (
isType(value, "object") &&
isType(value.top, "number") &&
isType(value.left, "number") &&
isType(value.width, "number") &&
isType(value.height, "number")
);
}
exports.isClipRect = isClipRect;
function isFunction(value) {
return isType(value, "function");
}
exports.isFunction = isFunction;
/**
* Checks if a file is apparently javascript compatible (.js or .coffee).
*
......@@ -108,6 +129,11 @@ function isJsFile(file) {
}
exports.isJsFile = isJsFile;
function isObject(value) {
return isType(value, "object");
}
exports.isObject = isObject;
/**
* Shorthands for checking if a value is of the given type. Can check for
* arrays.
......
......@@ -2,7 +2,6 @@
var utils = require('utils');
t.comment('fileExt()');
(function() {
var testCases = {
'foo.ext': 'ext',
......@@ -19,7 +18,6 @@
})();
t.comment('fillBlanks()');
(function() {
testCases = {
'foo': 'foo ',
......@@ -32,8 +30,22 @@
}
})();
t.comment('isJsFile()');
t.comment('isClipRect()');
(function() {
testCases = [
[{}, false],
[{top: 2}, false],
[{top: 2, left: 2, width: 2, height: 2}, true],
[{top: 2, left: 2, height: 2, width: 2}, true],
[{top: 2, left: 2, width: 2, height: new Date()}, false]
];
testCases.forEach(function(testCase) {
t.assertEquals(utils.isClipRect(testCase[0]), testCase[1], 'isClipRect() checks for a ClipRect');
});
})();
t.comment('isJsFile()');
(function() {
testCases = {
'': false,
......@@ -49,7 +61,6 @@
})();
t.comment('mergeObjects()');
(function() {
testCases = [
{
......