I am trying to write a script that will do the following:
- load a website and take a screenshot
- select a random
<a>
tag from all<a>
tags with the same class name - click the link
- wait for the new page to load
- capture the second screenshot
I'm stuck at selecting a random <a>
element and clicking it. Can anyone help me out please? It's my first day with casperjs. Here's what i have so far:
var casper = require('casper').create({
verbose: true,
logLevel: 'debug',
userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22',
pageSettings: {}
});
casper.options.viewportSize = {width: 1600, height: 950};
casper.start('http://www.myurl.com/', function() {
this.echo(this.getTitle());
this.capture('home.png');
});
casper.then(function() {
this.echo("Second test");
var random = Math.floor(Math.random() * document.querySelector(".showall").length);
var clicker = document.querySelector('.showall').eq(random);
this.mouseEvent('click', clicker);
this.wait(5000, function() {
this.capture('second.png');
});
});
casper.run();