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 @@
return this.waitFor(function(self) {
return self.exists(selector);
}, then, onTimeout, timeout);
},
/**
* Waits until an element matching the provided CSS3 selector does not exist in
* remote DOM to process a next step.
*
* @param String selector A CSS3 selector
* @param Function then The next step to perform (optional)
* @param Function onTimeout A callback function to call on timeout (optional)
* @param Number timeout The max amount of time to wait, in milliseconds (optional)
* @return Casper
*/
waitWhileSelector: function(selector, then, onTimeout, timeout) {
timeout = timeout ? timeout : this.defaultWaitTimeout;
return this.waitFor(function(self) {
return ! self.exists(selector);
}, then, onTimeout, timeout);
}
};
......
# get multiple pages of google search results
#
# usage: phantomjs googlepagination.coffee my search terms
#
# (all arguments will be used as the query)
#
phantom.injectJs('casper.js')
links = []
casper = new phantom.Casper
casper.start 'http://google.com', ->
@fill 'form[name=f]', q: phantom.args.join(' '), true
@click 'input[value="Google Search"]'
casper.then ->
# google's being all ajaxy, wait for results to load...
@waitForSelector 'table#nav', ->
processPage = (cspr) ->
currentPage = Number cspr.evaluate(-> document.querySelector('table#nav td.cur').innerText), 10
currentPage = 1 if currentPage == 0
cspr.capture "google-results-p#{ currentPage }.png"
# don't go too far down the rabbit hole
return if currentPage >= 5
cspr.evaluate ->
if nextLink = document.querySelector('table#nav td.cur').nextElementSibling?.querySelector('a')
nextLink.setAttribute 'id', 'next-page-of-results'
nextPage = 'a#next-page-of-results'
if cspr.exists nextPage
cspr.echo 'requesting next page...'
cspr.thenClick(nextPage).then(processPage)
else
cspr.echo "that's all, folks."
processPage(casper)
casper.run -> @exit()