Commit 3e7c65fe 3e7c65fe4847e867f3b63081890fe19222a3963b by Nicolas Perriault

Merge pull request #918 from ry5n/captureselector-zoom

Make captureSelector aware of the page zoom factor.
2 parents e14544ab a8f292bc
......@@ -1047,7 +1047,15 @@ Casper.prototype.getElementBounds = function getElementBounds(selector) {
if (!this.exists(selector)) {
throw new CasperError("No element matching selector found: " + selector);
}
var zoomFactor = this.page.zoomFactor || 1;
var clipRect = this.callUtils("getElementBounds", selector);
if (zoomFactor !== 1) {
for (var prop in clipRect) {
if (clipRect.hasOwnProperty(prop)) {
clipRect[prop] = clipRect[prop] * zoomFactor;
}
}
}
if (!utils.isClipRect(clipRect)) {
throw new CasperError('Could not fetch boundaries for element matching selector: ' + selector);
}
......@@ -1090,13 +1098,24 @@ Casper.prototype.getElementsInfo = function getElementsInfo(selector) {
* @param String selector A DOM CSS3/XPath selector
* @return Array
*/
Casper.prototype.getElementsBounds = function getElementBounds(selector) {
Casper.prototype.getElementsBounds = function getElementsBounds(selector) {
"use strict";
this.checkStarted();
if (!this.exists(selector)) {
throw new CasperError("No element matching selector found: " + selector);
}
return this.callUtils("getElementsBounds", selector);
var zoomFactor = this.page.zoomFactor || 1;
var clipRects = this.callUtils("getElementsBounds", selector);
if (zoomFactor !== 1) {
Array.prototype.forEach.call(clipRects, function(clipRect) {
for (var prop in clipRect) {
if (clipRect.hasOwnProperty(prop)) {
clipRect[prop] = clipRect[prop] * zoomFactor;
}
}
});
}
return clipRects;
};
/**
......
......@@ -142,6 +142,35 @@ casper.test.begin('ClientUtils.getElementBounds() tests', 3, function(test) {
});
});
casper.test.begin('ClientUtils.getElementBounds() page zoom factor tests', 3, function(test) {
casper.start().zoom(2).then(function() {
var html = '<div id="boxes">';
html += ' <div id="b1" style="position:fixed;top:10px;left:11px;width:50px;height:60px"></div>';
html += ' <div style="position:fixed;top:20px;left:21px;width:70px;height:80px"></div>';
html += '</div>';
this.page.content = html;
test.assertEquals(
this.getElementBounds('#b1'),
{ top: 20, left: 22, width: 100, height: 120 },
'ClientUtils.getElementBounds() is aware of the page zoom factor'
);
var bounds = this.getElementsBounds('#boxes div');
test.assertEquals(
bounds[0],
{ top: 20, left: 22, width: 100, height: 120 },
'ClientUtils.getElementsBounds() is aware of the page zoom factor'
);
test.assertEquals(
bounds[1],
{ top: 40, left: 42, width: 140, height: 160 },
'ClientUtils.getElementsBounds() is aware of the page zoom factor'
);
});
casper.run(function() {
test.done();
});
});
casper.test.begin('ClientUtils.getElementInfo() tests', 10, function(test) {
casper.page.content = '<a href="plop" class="plip plup"><i>paf</i></a>';
var info = casper.getElementInfo('a.plip');
......