added Casper.getElementBounds()
Showing
1 changed file
with
23 additions
and
8 deletions
... | @@ -151,8 +151,8 @@ Casper.prototype = { | ... | @@ -151,8 +151,8 @@ Casper.prototype = { |
151 | var previousClipRect; | 151 | var previousClipRect; |
152 | targetFile = fs.absolute(targetFile); | 152 | targetFile = fs.absolute(targetFile); |
153 | if (clipRect) { | 153 | if (clipRect) { |
154 | if (!utils.isType(clipRect, "object")) { | 154 | if (!utils.isClipRect(clipRect)) { |
155 | throw new Error("clipRect must be an Object instance."); | 155 | throw new Error("clipRect must be a valid ClipRect object."); |
156 | } | 156 | } |
157 | previousClipRect = this.page.clipRect; | 157 | previousClipRect = this.page.clipRect; |
158 | this.page.clipRect = clipRect; | 158 | this.page.clipRect = clipRect; |
... | @@ -177,9 +177,7 @@ Casper.prototype = { | ... | @@ -177,9 +177,7 @@ Casper.prototype = { |
177 | * @return Casper | 177 | * @return Casper |
178 | */ | 178 | */ |
179 | captureSelector: function(targetFile, selector) { | 179 | captureSelector: function(targetFile, selector) { |
180 | return this.capture(targetFile, this.evaluate(function(selector) { | 180 | return this.capture(targetFile, this.getElementBounds(selector)); |
181 | return __utils__.getElementBounds(); | ||
182 | }, { selector: selector })); | ||
183 | }, | 181 | }, |
184 | 182 | ||
185 | /** | 183 | /** |
... | @@ -513,6 +511,25 @@ Casper.prototype = { | ... | @@ -513,6 +511,25 @@ Casper.prototype = { |
513 | }, | 511 | }, |
514 | 512 | ||
515 | /** | 513 | /** |
514 | * Retrieves boundaries for a DOM element matching the provided CSS3 selector. | ||
515 | * | ||
516 | * @param String selector A CSS3 selector | ||
517 | * @return Object | ||
518 | */ | ||
519 | getElementBounds: function(selector) { | ||
520 | if (!this.exists(selector)) { | ||
521 | throw new Error("No element matching selector found: " + selector); | ||
522 | } | ||
523 | var clipRect = this.evaluate(function(selector) { | ||
524 | return __utils__.getElementBounds(); | ||
525 | }, { selector: selector }); | ||
526 | if (!utils.isClipRect(clipRect)) { | ||
527 | this.log('Could not fetch boundaries for element matching selector: ' + selector, "error"); | ||
528 | } | ||
529 | return clipRect; | ||
530 | }, | ||
531 | |||
532 | /** | ||
516 | * Retrieves global variable. | 533 | * Retrieves global variable. |
517 | * | 534 | * |
518 | * @param String name The name of the global variable to retrieve | 535 | * @param String name The name of the global variable to retrieve |
... | @@ -594,9 +611,7 @@ Casper.prototype = { | ... | @@ -594,9 +611,7 @@ Casper.prototype = { |
594 | * @return Casper | 611 | * @return Casper |
595 | */ | 612 | */ |
596 | mouseClick: function(selector) { | 613 | mouseClick: function(selector) { |
597 | var bounds = this.evaluate(function(selector) { | 614 | var bounds = this.getElementBounds(selector); |
598 | return __utils__.getElementBounds(selector); | ||
599 | }, { selector: selector }); | ||
600 | if (utils.isClipRect(bounds)) { | 615 | if (utils.isClipRect(bounds)) { |
601 | var x = bounds.left + Math.floor(bounds.width / 2); | 616 | var x = bounds.left + Math.floor(bounds.width / 2); |
602 | var y = bounds.top + Math.floor(bounds.height / 2); | 617 | var y = bounds.top + Math.floor(bounds.height / 2); | ... | ... |
-
Please register or sign in to post a comment