Skip to content

Instantly share code, notes, and snippets.

@siygle
Created August 30, 2012 16:57
Show Gist options
  • Select an option

  • Save siygle/3533209 to your computer and use it in GitHub Desktop.

Select an option

Save siygle/3533209 to your computer and use it in GitHub Desktop.
casperjs official example
var links = [];
var casper = require('casper').create();
function getLinks() {
var links = document.querySelectorAll('h3.r a');
return Array.prototype.map.call(links, function(e) {
return e.getAttribute('href')
});
}
casper.start('http://google.fr/', function() {
// search for 'casperjs' from google form
this.fill('form[action="/search"]', { q: 'casperjs' }, true);
});
casper.then(function() {
// aggregate results for the 'casperjs' search
links = this.evaluate(getLinks);
// now search for 'phantomjs' by filling the form again
this.fill('form[action="/search"]', { q: 'phantomjs' }, true);
});
casper.then(function() {
// aggregate results for the 'phantomjs' search
links = links.concat(this.evaluate(getLinks));
for (var i = 0; i < links.length; i++) {
this.test.assertMatch(links[i], /[casperjs|phantomjs]/, links[i] + ' contains casperjs keyword');
}
});
casper.run(function() {
// echo results in some pretty fashion
//this.echo(links.length + ' links found:');
//this.echo(' - ' + links.join('\n - ')).exit();
this.test.renderResults(true);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment