Commit fec3cbb5 fec3cbb549563fded7d41f66e9759516a69a7b43 by Nicolas Perriault

closes #230 - added Casper.getElementsBound()

1 parent c8eba140
CasperJS Changelog
==================
XXXX-XX-XX, v1.0.0
------------------
This version is yet to be released.
- closes [#230](https://github.com/n1k0/casperjs/issues/230) - added [`ClientUtils.getElementsBound()`](http://casperjs.org/api.html#clientutils.getElementsBounds) and [`Casper.getElementsBound()`](http://casperjs.org/api.html#casper.getElementsBounds)
2012-10-01, v1.0.0-RC2
----------------------
......
Subproject commit a6bf37ddbf7216ba7cdebd9c0bad87e6be2d8f03
Subproject commit 722549a0a483f19fc768d304d98050389d0c038a
......
......@@ -768,6 +768,22 @@ Casper.prototype.getElementBounds = function getElementBounds(selector) {
};
/**
* Retrieves boundaries for all the DOM elements matching the provided DOM CSS3/XPath selector.
*
* @param String selector A DOM CSS3/XPath selector
* @return Array
*/
Casper.prototype.getElementsBounds = function getElementBounds(selector) {
"use strict";
if (!this.exists(selector)) {
throw new CasperError("No element matching selector found: " + selector);
}
return this.evaluate(function _evaluate(selector) {
return window.__utils__.getElementsBounds(selector);
}, { selector: selector });
};
/**
* Retrieves global variable.
*
* @param String name The name of the global variable to retrieve
......
......@@ -350,7 +350,9 @@
/**
* Retrieves bounding rect coordinates of the HTML element matching the
* provided CSS3 selector
* provided CSS3 selector in the following form:
*
* {top: y, left: x, width: w, height:, h}
*
* @param String selector
* @return Object or null
......@@ -370,6 +372,35 @@
};
/**
* Retrieves the list of bounding rect coordinates for all the HTML elements matching the
* provided CSS3 selector, in the following form:
*
* [{top: y, left: x, width: w, height:, h},
* {top: y, left: x, width: w, height:, h},
* ...]
*
* @param String selector
* @return Array
*/
this.getElementsBounds = function getElementsBounds(selector) {
var elements = this.findAll(selector);
var self = this;
try {
return Array.prototype.map.call(elements, function(element) {
var clipRect = element.getBoundingClientRect();
return {
top: clipRect.top,
left: clipRect.left,
width: clipRect.width,
height: clipRect.height
};
});
} catch (e) {
this.log("Unable to fetch bounds for elements matching " + selector, "warning");
}
};
/**
* Retrieves a single DOM element matching a given XPath expression.
*
* @param String expression The XPath expression
......
......@@ -890,7 +890,7 @@ var Tester = function Tester(casper, options) {
if (utils.isFunction(v1)) {
return v1.toString() === v2.toString();
}
if (v1 instanceof Object && v2 instanceof Object) {
if (v1 instanceof Object) {
if (Object.keys(v1).length !== Object.keys(v2).length) {
return false;
}
......
......@@ -81,4 +81,31 @@ function fakeDocument(html) {
casper.test.assertEquals(xpathSelector.path, '//li[text()="blah"]', 'ClientUtils.processSelector() can process a XPath selector');
})(casper);
casper.test.done();
(function(casper) {
casper.start();
// getElementBounds
casper.then(function() {
this.page.content = '<div id="b1" style="position:fixed;top:10px;left:11px;width:50px;height:60px"></div>';
this.test.assertEquals(this.getElementBounds('#b1'),
{ top: 10, left: 11, width: 50, height: 60 },
'ClientUtils.getElementBounds() retrieves element boundaries');
});
// getElementsBounds
casper.start();
casper.then(function() {
var html = '<div id="boxes">';
html += ' <div 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;
var bounds = this.getElementsBounds('#boxes div');
this.test.assertEquals(bounds[0], { top: 10, left: 11, width: 50, height: 60 },
'ClientUtils.getElementsBounds() retrieves multiple elements boundaries');
this.test.assertEquals(bounds[1], { top: 20, left: 21, width: 70, height: 80 },
'ClientUtils.getElementsBounds() retrieves multiple elements boundaries');
});
})(casper);
casper.run(function() {
this.test.done();
});
......
......@@ -27,6 +27,7 @@ t.assert(t.testEquals({1:{name:"bob",age:28}, 2:{name:"john",age:26}}, {1:{name:
t.assertNot(t.testEquals({1:{name:"bob",age:28}, 2:{name:"john",age:26}}, {1:{name:"bob",age:28}, 2:{name:"john",age:27}}), 'Tester.testEquals() complex object inequality');
t.assert(t.testEquals(function(x){return x;}, function(x){return x;}), 'Tester.testEquals() function equality');
t.assertNot(t.testEquals(function(x){return x;}, function(y){return y+2;}), 'Tester.testEquals() function inequality');
t.assert(t.testEquals([{a:1, b:2}, {c:3, d:4}], [{a:1, b:2}, {c:3, d:4}]), 'Tester.testEquals() arrays of objects');
t.comment('Tester.sortFiles()');
var testDirRoot = fs.pathJoin(phantom.casperPath, 'tests', 'testdir');
......