Commit 11a936d0 11a936d09aa01c72290d0091a633e1ab6149fa68 by Nicolas Perriault

Merge pull request #21 from rubysolo/master

Added new sample in which steps are not all known in advance, new waitWhileSelector() method
2 parents 33f335a6 d2a77668
...@@ -904,6 +904,23 @@ ...@@ -904,6 +904,23 @@
904 return this.waitFor(function(self) { 904 return this.waitFor(function(self) {
905 return self.exists(selector); 905 return self.exists(selector);
906 }, then, onTimeout, timeout); 906 }, then, onTimeout, timeout);
907 },
908
909 /**
910 * Waits until an element matching the provided CSS3 selector does not exist in
911 * remote DOM to process a next step.
912 *
913 * @param String selector A CSS3 selector
914 * @param Function then The next step to perform (optional)
915 * @param Function onTimeout A callback function to call on timeout (optional)
916 * @param Number timeout The max amount of time to wait, in milliseconds (optional)
917 * @return Casper
918 */
919 waitWhileSelector: function(selector, then, onTimeout, timeout) {
920 timeout = timeout ? timeout : this.defaultWaitTimeout;
921 return this.waitFor(function(self) {
922 return ! self.exists(selector);
923 }, then, onTimeout, timeout);
907 } 924 }
908 }; 925 };
909 926
......
1 # get multiple pages of google search results
2 #
3 # usage: phantomjs googlepagination.coffee my search terms
4 #
5 # (all arguments will be used as the query)
6 #
7 phantom.injectJs('casper.js')
8
9 links = []
10 casper = new phantom.Casper
11
12 casper.start 'http://google.com', ->
13 @fill 'form[name=f]', q: phantom.args.join(' '), true
14 @click 'input[value="Google Search"]'
15
16 casper.then ->
17 # google's being all ajaxy, wait for results to load...
18 @waitForSelector 'table#nav', ->
19 processPage = (cspr) ->
20 currentPage = Number cspr.evaluate(-> document.querySelector('table#nav td.cur').innerText), 10
21 currentPage = 1 if currentPage == 0
22 cspr.capture "google-results-p#{ currentPage }.png"
23
24 # don't go too far down the rabbit hole
25 return if currentPage >= 5
26
27 cspr.evaluate ->
28 if nextLink = document.querySelector('table#nav td.cur').nextElementSibling?.querySelector('a')
29 nextLink.setAttribute 'id', 'next-page-of-results'
30
31 nextPage = 'a#next-page-of-results'
32 if cspr.exists nextPage
33 cspr.echo 'requesting next page...'
34 cspr.thenClick(nextPage).then(processPage)
35 else
36 cspr.echo "that's all, folks."
37
38 processPage(casper)
39
40 casper.run -> @exit()