Add getElementsAttribute(selector) and getElementsInfo(selector)
Showing
2 changed files
with
68 additions
and
0 deletions
... | @@ -897,6 +897,28 @@ Casper.prototype.getElementAttr = function getElementAttr(selector, attribute) { | ... | @@ -897,6 +897,28 @@ Casper.prototype.getElementAttr = function getElementAttr(selector, attribute) { |
897 | }; | 897 | }; |
898 | 898 | ||
899 | /** | 899 | /** |
900 | * Retrieves the value of an attribute for each element matching the provided | ||
901 | * DOM CSS3/XPath selector. | ||
902 | * | ||
903 | * @param String selector A DOM CSS3/XPath selector | ||
904 | * @param String attribute The attribute name to lookup | ||
905 | * @return Array | ||
906 | */ | ||
907 | Casper.prototype.getElementsAttribute = | ||
908 | Casper.prototype.getElementsAttr = function getElementsAttr(selector, attribute) { | ||
909 | "use strict"; | ||
910 | this.checkStarted(); | ||
911 | return this.evaluate(function _evaluate(selector, attribute) { | ||
912 | var ele = []; | ||
913 | [].forEach.call(__utils__.findAll(selector), function(element) { | ||
914 | var e = element.getAttribute(attribute); | ||
915 | ele.push(e); | ||
916 | }); | ||
917 | return ele; | ||
918 | }, selector, attribute); | ||
919 | } | ||
920 | |||
921 | /** | ||
900 | * Retrieves boundaries for a DOM element matching the provided DOM CSS3/XPath selector. | 922 | * Retrieves boundaries for a DOM element matching the provided DOM CSS3/XPath selector. |
901 | * | 923 | * |
902 | * @param String selector A DOM CSS3/XPath selector | 924 | * @param String selector A DOM CSS3/XPath selector |
... | @@ -935,6 +957,23 @@ Casper.prototype.getElementInfo = function getElementInfo(selector) { | ... | @@ -935,6 +957,23 @@ Casper.prototype.getElementInfo = function getElementInfo(selector) { |
935 | }; | 957 | }; |
936 | 958 | ||
937 | /** | 959 | /** |
960 | * Retrieves information about the nodes matching the provided selector. | ||
961 | * | ||
962 | * @param String|Objects selector CSS3/XPath selector | ||
963 | * @return Array | ||
964 | */ | ||
965 | Casper.prototype.getElementsInfo = function getElementsInfo(selector) { | ||
966 | "use strict"; | ||
967 | this.checkStarted(); | ||
968 | if (!this.exists(selector)) { | ||
969 | throw new CasperError(f("Cannot get information from %s: no elements found.", selector)); | ||
970 | } | ||
971 | return this.evaluate(function(selector) { | ||
972 | return __utils__.getElementsInfo(selector); | ||
973 | }, selector); | ||
974 | }; | ||
975 | |||
976 | /** | ||
938 | * Retrieves boundaries for all the DOM elements matching the provided DOM CSS3/XPath selector. | 977 | * Retrieves boundaries for all the DOM elements matching the provided DOM CSS3/XPath selector. |
939 | * | 978 | * |
940 | * @param String selector A DOM CSS3/XPath selector | 979 | * @param String selector A DOM CSS3/XPath selector | ... | ... |
... | @@ -420,6 +420,35 @@ | ... | @@ -420,6 +420,35 @@ |
420 | }; | 420 | }; |
421 | 421 | ||
422 | /** | 422 | /** |
423 | * Retrieves information about the nodes matching the provided selector. | ||
424 | * | ||
425 | * @param String|Object selector CSS3/XPath selector | ||
426 | * @return Array | ||
427 | */ | ||
428 | this.getElementsInfo = function getElementsInfo(selector) { | ||
429 | var bounds = this.getElementsBounds(selector); | ||
430 | var visibility = this.visible(selector); | ||
431 | return Array.prototype.map.call(this.findAll(selector), function(element, index) { | ||
432 | var attributes = {}; | ||
433 | [].forEach.call(element.attributes, function(attr) { | ||
434 | attributes[attr.name.toLowerCase()] = attr.value; | ||
435 | }); | ||
436 | return { | ||
437 | nodeName: element.nodeName.toLowerCase(), | ||
438 | attributes: attributes, | ||
439 | tag: element.outerHTML, | ||
440 | html: element.innerHTML, | ||
441 | text: element.innerText, | ||
442 | x: bounds[index].left, | ||
443 | y: bounds[index].top, | ||
444 | width: bounds[index].width, | ||
445 | height: bounds[index].height, | ||
446 | visible: visibility | ||
447 | }; | ||
448 | }); | ||
449 | }; | ||
450 | |||
451 | /** | ||
423 | * Retrieves a single DOM element matching a given XPath expression. | 452 | * Retrieves a single DOM element matching a given XPath expression. |
424 | * | 453 | * |
425 | * @param String expression The XPath expression | 454 | * @param String expression The XPath expression | ... | ... |
-
Please register or sign in to post a comment