added Casper#each() and Casper#getTitle() + example
Showing
3 changed files
with
44 additions
and
0 deletions
... | @@ -231,6 +231,16 @@ | ... | @@ -231,6 +231,16 @@ |
231 | return this.exit(Number(status) > 0 ? Number(status) : 1); | 231 | return this.exit(Number(status) > 0 ? Number(status) : 1); |
232 | }, | 232 | }, |
233 | 233 | ||
234 | each: function(array, fn) { | ||
235 | var i = 0; | ||
236 | (function(self, i) { | ||
237 | array.forEach(function(item) { | ||
238 | fn(self, item, i); | ||
239 | }); | ||
240 | })(this, i); | ||
241 | return this; | ||
242 | }, | ||
243 | |||
234 | /** | 244 | /** |
235 | * Prints something to stdout. | 245 | * Prints something to stdout. |
236 | * | 246 | * |
... | @@ -378,6 +388,17 @@ | ... | @@ -378,6 +388,17 @@ |
378 | }, | 388 | }, |
379 | 389 | ||
380 | /** | 390 | /** |
391 | * Retrieves current page title, if any. | ||
392 | * | ||
393 | * @return String | ||
394 | */ | ||
395 | getTitle: function() { | ||
396 | return this.evaluate(function() { | ||
397 | return document.title; | ||
398 | }) | ||
399 | }, | ||
400 | |||
401 | /** | ||
381 | * Logs a message. | 402 | * Logs a message. |
382 | * | 403 | * |
383 | * @param String message The message to log | 404 | * @param String message The message to log | ... | ... |
samples/each.js
0 → 100644
1 | phantom.injectJs('casper.js'); | ||
2 | |||
3 | var links = [ | ||
4 | 'http://google.com/', | ||
5 | 'http://yahoo.com/', | ||
6 | 'http://bing.com/' | ||
7 | ]; | ||
8 | var casper = new phantom.Casper(); | ||
9 | var i = 0; | ||
10 | var titles = []; | ||
11 | |||
12 | casper.start(); | ||
13 | |||
14 | casper.each(links, function(self, link) { | ||
15 | self.thenOpen(link, function(self) { | ||
16 | self.echo(self.getTitle()); | ||
17 | }); | ||
18 | }); | ||
19 | |||
20 | casper.run(function(self) { | ||
21 | self.exit(); | ||
22 | }); |
-
Please register or sign in to post a comment