Reputation: 615
I am trying to run multiple instances of CasperJS on the same process, my test code looks something like this:
['user1,pass1', 'user2,pass2'].forEach(function( account ) {
var casperInstance = require('casper').create({
verbose: true,
logLevel: "debug",
webSecurityEnabled: false,
pageSettings: {
loadImages: true,
loadPlugins: true,
userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4'
}
});
runit(casperInstance, account);
});
function runit(casperi, account) {
casperi.options.viewportSize = {width: 1920, height: 1080};
var account = account.split(',');
casperi.start( 'https://www.example.com/login/', function ( ) {
var x = require('casper').selectXPath;
this.fill('form.loginForm', {
'username': account[0],
'password': account[1]
}, false);
this.click(x('//*[text()="Log in"]'));
}).waitForUrl(/https:\/\/www.example.com\/$/, function then() { // have to use this because the form is submitted with ajax, is there a better way?
//this.echo(this.getHTML());
this.echo('Logged in');
}, function OnTimeout() {
this.echo('Login Failed');
}, 20000);
casperi.run();
}
With only one account on the array it works fine, but with two or more, it always times out, I don't think my code is running multiple instances of casper, because if I replace all the login code to just print the html (or anything else) it just prints it once.
Is there a way to run multiple instances of CasperJS on the same process, or is the only solution to create a launcher to start multiple processes?
Upvotes: 3
Views: 1717
Reputation: 61892
Yes, you can use multiple casper
instances, but you have to keep in mind that they all run in the same PhantomJS process, which is comparable to a single desktop browser window. You can have many tabs, but the tabs will share the same cookies/localStorage and therefore the same session.
Since, you're trying to test the login, which is presumably implemented through cookies, this cannot work. The first "tab" opens the login page, tries to login, may succeed. But the second "tab" tries the same thing at the same time. The server probably detects this as though the user made a double click and doesn't proceed with either login or just one of them (just guessing at this point).
The key point is that if you need different sessions, then you cannot use multiple casper
instances in a single process. The obvious solution would be to let the test run for each username and password combination in multiple PhantomJS instances (a pool of some sort maybe).
Upvotes: 2