lundi 15 mai 2017

CasperJS + Authlogic (Rails) - maintain login session between tests

I have a single-page app that has a Ruby on Rails backend and uses Authlogic to facilitate user authentication.

I am implementing functional testing using CasperJS and am having a hard time getting login sessions to persist between sessions but also between thenOpen commands.

I am running the following command:

casperjs --cookies-file=cookies.txt test ../../../foobar/test/casper/login/test.js

Here's an example of my current code:

phantom.cookiesEnabled = true;
var x = require('casper').selectXPath

casper.test.begin('Logging in', 2, function suite(test) {
  casper.start('http://localhost:3000/login', function() {
    console.log("Page loaded");

    test.assertExists('form', 'login form is found');
    this.fillSelectors('form', {
      '#email': "foo@bar.com",
      '#password': "foo_bar"
    }, false)

    this.click('#submit')

    casper.thenOpen('http://localhost:3000/my', function() {
      test.assertUrlMatch(this.getCurrentUrl(), 'http://localhost:3000/my', "Logged in and maintained login cookie")
    })
  })

  casper.run(function() {
    test.done();
  });
})

While watching my dev log, I can see that the first test (casper.start) logs in successfully but after the thenOpen, the Authlogic UserSession is no longer maintained so PhantomJS gets redirected to localhost:3000/login, which is what should happen if there is no logged-in user.

How can I maintain a logged-in session using CasperJS between thenOpen but also between multiple test runs? Can I maintain cookies so that the user remains logged-in between tests?

Aucun commentaire:

Enregistrer un commentaire