0

Is there a reason why .then() would cause a session to drop / (re-)load a page without the active session?

In the example below, you can see how the session is established with a login, then a search is performed and the result page is loaded. The session survives this navigation, but when separating tests on the now displayed page with .then(), the DOM no longer corresponds to what the logged in user would see. I fail to see why.

casper.start("http://example.dev", function() {
  this.test.assertExists('form[name="login"]', 'login form is shown'); // PASS
  this.fill('form[name="login"]', {
    user: "username",
    password: "Password1"
  }, true);
});

casper.then(function() {
  this.test.assertExists('body[data-userid="42"]', 'User is logged in'); // PASS
  this.sendKeys("input#search", "12345");
  this.click("button#submit_search"); // form submit causes redirect
});

casper.then(function() {
  this.test.assertExists('body.page_id_12345', 'search for "12345" loads page?id=12345'); // PASS
  this.test.assertExists('body[data-userid="42"]', 'User is logged in'); // PASS
});

// PROBLEM BELOW:

casper.then(function() {
  this.test.assertExists('body[data-userid="42"]', 'User is logged in'); // FAIL
});

casper.run();

Using CasperJS 1.1.0-beta4 on OS X with PhantomJS

cjoy
  • 406
  • 5
  • 14
  • No, that should not fail. Have you tried to move the failing assertion into the previous step to see if having two assertions in the same test also fails. Which PhantomJS version do you use? – Artjom B. Jun 03 '16 at 13:24
  • Hi Artjom, the assertion can be tested multiple times in the previous step without a problem. In other words, if I comment out the ```});``` of the previous step and the ```casper.then(function() {``` before the failing assertion, the entire suite passes. PhantomJS version is 1.9.8 – cjoy Jun 03 '16 at 13:53
  • 2
    Have you tried taking a screenshot (`casper.capture(fn)`) shortly before the fail? Have you tried to print the page source (`casper.getHTML()`) to see if you're on the correct page? Have you tried to see what the current URL is (`casper.getCurrentUrl()`) – Artjom B. Jun 03 '16 at 13:58
  • doing all of the above, confirms that the second last ```then block``` shows the correct page while the last ```then block``` shows the login page, as if the session was dumped and a reload of the current page (access restricted) was attempted. I fail to see why casper would add a navigation step of any kind? – cjoy Jun 03 '16 at 14:18
  • That's quite strange. You can try to upgrade to PhantomJS 2.1.1 and CasperJS 1.1-beta5. – Artjom B. Jun 03 '16 at 15:03
  • See if this solves your issue: http://stackoverflow.com/a/37351436/1816580 – Artjom B. Jun 03 '16 at 15:07
  • thanks for the input artjom, unfortunately the problem persists using the phantomjs 2.1.4 with casperjs 1.1.0-beta5 (latest releases) – cjoy Jun 10 '16 at 11:20

1 Answers1

-1

What does your test output says?

You can try doing something like this to see if it'll throw the same error:

casper.waitForSelector('body[data-userid="42"]',

    function success() {

        test.assertExsists('body[data-userid="42"]', 'User is logged in');
        //do something...
    }
);
Awesim
  • 99
  • 9
  • using waitForSelector I get a timeout: ```FAIL "body[data-userid="42"]" still did not exist in 5000ms```. Looking at the DOM via ```casper.evaluate()```, I see the markup that would be expected for an anonymous user (i.e. no content for the test runner to interact with) – cjoy Jun 03 '16 at 13:59