Commit c4582f66 c4582f663eaf53b729a80e8af2b77b5077e112b3 by hexid

Add getElementsAttribute(selector) and getElementsInfo(selector)

1 parent a82c0cbd
......@@ -897,6 +897,28 @@ Casper.prototype.getElementAttr = function getElementAttr(selector, attribute) {
};
/**
* Retrieves the value of an attribute for each element matching the provided
* DOM CSS3/XPath selector.
*
* @param String selector A DOM CSS3/XPath selector
* @param String attribute The attribute name to lookup
* @return Array
*/
Casper.prototype.getElementsAttribute =
Casper.prototype.getElementsAttr = function getElementsAttr(selector, attribute) {
"use strict";
this.checkStarted();
return this.evaluate(function _evaluate(selector, attribute) {
var ele = [];
[].forEach.call(__utils__.findAll(selector), function(element) {
var e = element.getAttribute(attribute);
ele.push(e);
});
return ele;
}, selector, attribute);
}
/**
* Retrieves boundaries for a DOM element matching the provided DOM CSS3/XPath selector.
*
* @param String selector A DOM CSS3/XPath selector
......@@ -935,6 +957,23 @@ Casper.prototype.getElementInfo = function getElementInfo(selector) {
};
/**
* Retrieves information about the nodes matching the provided selector.
*
* @param String|Objects selector CSS3/XPath selector
* @return Array
*/
Casper.prototype.getElementsInfo = function getElementsInfo(selector) {
"use strict";
this.checkStarted();
if (!this.exists(selector)) {
throw new CasperError(f("Cannot get information from %s: no elements found.", selector));
}
return this.evaluate(function(selector) {
return __utils__.getElementsInfo(selector);
}, selector);
};
/**
* Retrieves boundaries for all the DOM elements matching the provided DOM CSS3/XPath selector.
*
* @param String selector A DOM CSS3/XPath selector
......
......@@ -420,6 +420,35 @@
};
/**
* Retrieves information about the nodes matching the provided selector.
*
* @param String|Object selector CSS3/XPath selector
* @return Array
*/
this.getElementsInfo = function getElementsInfo(selector) {
var bounds = this.getElementsBounds(selector);
var visibility = this.visible(selector);
return Array.prototype.map.call(this.findAll(selector), function(element, index) {
var attributes = {};
[].forEach.call(element.attributes, function(attr) {
attributes[attr.name.toLowerCase()] = attr.value;
});
return {
nodeName: element.nodeName.toLowerCase(),
attributes: attributes,
tag: element.outerHTML,
html: element.innerHTML,
text: element.innerText,
x: bounds[index].left,
y: bounds[index].top,
width: bounds[index].width,
height: bounds[index].height,
visible: visibility
};
});
};
/**
* Retrieves a single DOM element matching a given XPath expression.
*
* @param String expression The XPath expression
......