CasperJS + Authlogic (Rails) - 在测试之间维护登录会话



我有一个单页应用程序,它有一个Ruby on Rails后端,并使用Authlogic来促进用户身份验证。

我正在使用 CasperJS 实现功能测试,并且很难让登录会话在会话之间以及thenOpen命令之间持久存在。

我正在运行以下命令:

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

这是我当前代码的示例:

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();
  });
})

在查看我的开发日志时,我可以看到第一个测试(casper.start(成功登录,但在thenOpen之后,Authlogic UserSession不再维护,因此 PhantomJS 被重定向到 localhost:3000/login ,如果没有登录用户,就会发生这种情况。

如何在thenOpen之间以及多次测试运行之间使用 CasperJS 维护登录会话?我可以维护 Cookie 以便用户在测试之间保持登录状态吗?

似乎这条评论挽救了这一天

查看您this.click的代码,然后执行 casper.thenOpen 我会在提交表单后添加另一个步骤,并且 使用casper.waitForXXXX .

它看起来像一个时间问题

最新更新