Commit d0ec8d84 d0ec8d841e59bb9847323577d94c431406bb7b92 by Nicolas Perriault

forthported Casper.mouseClick() from 0.6 branch

1 parent 83472789
......@@ -5,6 +5,7 @@ XXXX-XX-XX, v0.4.3
------------------
- fixed #32 - `ClientUtils.click()` handles `<a href='javascript:'>` links
- fixed #34 - avoid having previously loaded DOM contents being still active on run complete
2011-12-25, v0.4.2
------------------
......
......@@ -172,17 +172,7 @@
*/
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 }));
},
......@@ -591,6 +581,26 @@
},
/**
* 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 (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).
......
......@@ -347,6 +347,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.
*
* @param String message
......
......@@ -180,6 +180,16 @@ function fillBlanks(text, pad) {
return text;
}
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")
);
}
/**
* Checks if a file is apparently javascript compatible (.js or .coffee).
*
......
(function(t) {
casper.start('tests/site/index.html', function(self) {
self.mouseClick('a[href="test.html"]');
});
casper.then(function(self) {
t.comment('Casper.mouseClick()');
t.assertTitle('CasperJS test target', 'Casper.mouseClick() can click on a link');
});
// onclick variants tests
casper.thenOpen('tests/site/click.html', function(self) {
t.comment('Casper.mouseClick()');
self.mouseClick('#test1');
self.mouseClick('#test2');
self.mouseClick('#test3');
self.mouseClick('#test4');
var results = self.getGlobal('results');
self.test.assert(results.test1, 'Casper.mouseClick() has clicked an `href="javascript:` link');
self.test.assert(results.test2, 'Casper.mouseClick() has clicked an `href="#"` link');
self.test.assert(results.test3, 'Casper.mouseClick() has clicked an `onclick=".*; return false"` link');
self.test.assert(results.test4, 'Casper.mouseClick() has clicked an unobstrusive js handled link');
});
casper.run(function(self) {
t.done();
});
})(casper.test);
\ No newline at end of file
(function(t) {
t.comment('fileExt()');
(function() {
var testCases = {
'foo.ext': 'ext',
......@@ -17,7 +16,6 @@
})();
t.comment('fillBlanks()');
(function() {
testCases = {
'foo': 'foo ',
......@@ -30,8 +28,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(isClipRect(testCase[0]), testCase[1], 'isClipRect() checks for a ClipRect');
});
})();
t.comment('isJsFile()');
(function() {
testCases = {
'': false,
......@@ -47,7 +59,6 @@
})();
t.comment('mergeObjects()');
(function() {
testCases = [
{
......