Nagendra Kakarla
Nagendra Kakarla

Reputation: 1071

How to maintain session in casperjs?

In CasperJS how do you maintain session when using casper.thenOpen()

For example:

var casper = require('casper').create();

casper.start('http://chaseonline.com/', function() {
    this.echo(this.getTitle());
    this.evaluate(function() {
       document.getElementById("userid").value = "[email protected]";
       document.getElementById("password").value = "asdf";

     });
    this.click("#btnSubmit");
});

casper.thenOpen('http://chaseonline.com/section/1/module/2/abc.jsp', function() {
  // now this page never loads because the page requires a logged in session 
  // but casperjs doesn't appear to automatically propagate the session
  this.echo(this.getTitle());
});

casper.run();

Upvotes: 3

Views: 943

Answers (1)

dasmelch
dasmelch

Reputation: 532

The session is still open in your example. Probably you are not logged in correct.
Could be if it works Step by step (seems the site i see is not the one in your example, there is no login):

var casper = require('casper').create();
var x = require('casper').selectXPath;

casper.start('http://youraddess.com/', function() {
  casper.then(function() {
    casper.waitForSelector(x("xpath_selector"));
  });
  var data = {};
  casper.then(function() {
    data["//input[@id='userid']"] = "[email protected]";
    data["//input[@id='password']"] = "asdf";
    casper.fillXPath(x("//form[]"), data, false);
  });
  casper.then(function() {
    casper.click(x("//button[@id='btnSubmit']"));
  });
  casper.then(function() {
    casper.waitWhileSelector(x("xpath_selector"));
  });
});
casper.thenOpen('http://chaseonline.com/section/1/module/2/abc.jsp', function() {
  // now this page never loads because the page requires a logged in session 
  // but casperjs doesn't appear to automatically propagate the session
  casper.then(function() {
    casper.echo(this.getTitle());
    casper.capture('test.png');
  });
});

casper.run();

The session is till the run() always the same. There are possiblities to open new ones, but thats hard.

Upvotes: 2

Related Questions