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) { ...@@ -1047,7 +1047,15 @@ Casper.prototype.getElementBounds = function getElementBounds(selector) {
1047 if (!this.exists(selector)) { 1047 if (!this.exists(selector)) {
1048 throw new CasperError("No element matching selector found: " + selector); 1048 throw new CasperError("No element matching selector found: " + selector);
1049 } 1049 }
1050 var zoomFactor = this.page.zoomFactor || 1;
1050 var clipRect = this.callUtils("getElementBounds", selector); 1051 var clipRect = this.callUtils("getElementBounds", selector);
1052 if (zoomFactor !== 1) {
1053 for (var prop in clipRect) {
1054 if (clipRect.hasOwnProperty(prop)) {
1055 clipRect[prop] = clipRect[prop] * zoomFactor;
1056 }
1057 }
1058 }
1051 if (!utils.isClipRect(clipRect)) { 1059 if (!utils.isClipRect(clipRect)) {
1052 throw new CasperError('Could not fetch boundaries for element matching selector: ' + selector); 1060 throw new CasperError('Could not fetch boundaries for element matching selector: ' + selector);
1053 } 1061 }
...@@ -1090,13 +1098,24 @@ Casper.prototype.getElementsInfo = function getElementsInfo(selector) { ...@@ -1090,13 +1098,24 @@ Casper.prototype.getElementsInfo = function getElementsInfo(selector) {
1090 * @param String selector A DOM CSS3/XPath selector 1098 * @param String selector A DOM CSS3/XPath selector
1091 * @return Array 1099 * @return Array
1092 */ 1100 */
1093 Casper.prototype.getElementsBounds = function getElementBounds(selector) { 1101 Casper.prototype.getElementsBounds = function getElementsBounds(selector) {
1094 "use strict"; 1102 "use strict";
1095 this.checkStarted(); 1103 this.checkStarted();
1096 if (!this.exists(selector)) { 1104 if (!this.exists(selector)) {
1097 throw new CasperError("No element matching selector found: " + selector); 1105 throw new CasperError("No element matching selector found: " + selector);
1098 } 1106 }
1099 return this.callUtils("getElementsBounds", selector); 1107 var zoomFactor = this.page.zoomFactor || 1;
1108 var clipRects = this.callUtils("getElementsBounds", selector);
1109 if (zoomFactor !== 1) {
1110 Array.prototype.forEach.call(clipRects, function(clipRect) {
1111 for (var prop in clipRect) {
1112 if (clipRect.hasOwnProperty(prop)) {
1113 clipRect[prop] = clipRect[prop] * zoomFactor;
1114 }
1115 }
1116 });
1117 }
1118 return clipRects;
1100 }; 1119 };
1101 1120
1102 /** 1121 /**
......
...@@ -142,6 +142,35 @@ casper.test.begin('ClientUtils.getElementBounds() tests', 3, function(test) { ...@@ -142,6 +142,35 @@ casper.test.begin('ClientUtils.getElementBounds() tests', 3, function(test) {
142 }); 142 });
143 }); 143 });
144 144
145 casper.test.begin('ClientUtils.getElementBounds() page zoom factor tests', 3, function(test) {
146 casper.start().zoom(2).then(function() {
147 var html = '<div id="boxes">';
148 html += ' <div id="b1" style="position:fixed;top:10px;left:11px;width:50px;height:60px"></div>';
149 html += ' <div style="position:fixed;top:20px;left:21px;width:70px;height:80px"></div>';
150 html += '</div>';
151 this.page.content = html;
152 test.assertEquals(
153 this.getElementBounds('#b1'),
154 { top: 20, left: 22, width: 100, height: 120 },
155 'ClientUtils.getElementBounds() is aware of the page zoom factor'
156 );
157 var bounds = this.getElementsBounds('#boxes div');
158 test.assertEquals(
159 bounds[0],
160 { top: 20, left: 22, width: 100, height: 120 },
161 'ClientUtils.getElementsBounds() is aware of the page zoom factor'
162 );
163 test.assertEquals(
164 bounds[1],
165 { top: 40, left: 42, width: 140, height: 160 },
166 'ClientUtils.getElementsBounds() is aware of the page zoom factor'
167 );
168 });
169 casper.run(function() {
170 test.done();
171 });
172 });
173
145 casper.test.begin('ClientUtils.getElementInfo() tests', 10, function(test) { 174 casper.test.begin('ClientUtils.getElementInfo() tests', 10, function(test) {
146 casper.page.content = '<a href="plop" class="plip plup"><i>paf</i></a>'; 175 casper.page.content = '<a href="plop" class="plip plup"><i>paf</i></a>';
147 var info = casper.getElementInfo('a.plip'); 176 var info = casper.getElementInfo('a.plip');
......