Created
July 19, 2014 19:06
-
-
Save gothamgator/ffb400e7533919e98521 to your computer and use it in GitHub Desktop.
casperjs - multiple user log-in sequence
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var casper = require('casper').create() | |
var colorizer = require('colorizer').create('Colorizer'); | |
// casper.userAgent('Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)'); | |
var userNames = ['username1','username2','username3','username4', 'username5']; | |
var passWords = ['password1','password2','password3','password4', 'password5']; | |
var url = 'http://mywebsitenet.com'; | |
var tracker = {Success: [], Fail: []}; | |
function login(username, password) { | |
casper.then(function () { | |
this.sendKeys('#log', username); | |
this.sendKeys('#pwd', password); | |
this.click('#wpmem_login > form > fieldset > div.button_div > input.buttons'); | |
// console.log(username + " has clicked the Log In button!") | |
}); | |
//Step 1 (of many; starting with just this one) | |
casper.waitFor(function check() { | |
return this.evaluate(function() { | |
return document.getElementById('logo'); | |
}); | |
}, function then() { // step to execute when check() is ok | |
this.click('#post-369 > article > div > div:nth-child(9) > div:nth-child(2) > p > a:nth-child(19)'); | |
}, function timeout() { // step to execute if check has failed | |
tracker.Fail.push(username) | |
this.echo("Warning: " + username + " could not be logged in.", "WARNING"); | |
this.capture('Fail_'+username+'.png'); | |
},10000); | |
//Step to Log Out | |
casper.waitFor(function check() { | |
return this.evaluate(function() { | |
return document.getElementById('wp-admin-bar-logout'); | |
}); | |
}, function then() { | |
this.click('#wp-admin-bar-logout > a'); | |
tracker.Success.push(username); | |
//If user can log out, then they successfully logged in | |
this.echo(this.fetchText('#wp-admin-bar-my-account > a') + " you logged in."); | |
this.capture('Success_'+username+'.png'); | |
}); | |
}; | |
casper.start(); // empty page | |
casper.viewport(1024, 768); | |
userNames.forEach(function(username, index){ | |
casper.thenOpen(url); // open the start page | |
login(username, passWords[index]); // schedule the steps | |
}); | |
casper.then(function () { | |
this.echo("Success: " + tracker.Success.length, "INFO"); | |
this.echo("Fail: " + tracker.Fail.length, "WARNING"); | |
this.echo(JSON.stringify(tracker)); | |
}); | |
casper.run(); // begin the execution |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment