Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When running a Behat login test through PhantomJS it only passes on the first run

I created a very simple login test in Behat to login to our internal application. There are two login types (done from the same screen and box). There is basic and advanced. The only difference is that in advanced the user that is logging in has access to more than one company and is asked to pick a company to work on.

I find that whenever I run the scenarios through PhantomJS instead of Selenium they run a lot faster (obviously since nothing has to be rendered in a headless browser emulator). When I first launch PhantomJS it runs both scenarios just fine. But then I run it a second time and it fails the first scenario (the basic login).

If I restart PhantomJS it works again until the second try. I am very confused by this. Why is it that I need to restart PhantomJS to get both tests to pass again on the first call.

If I specify that I just want it to run the advanced login on subsequent runs then it fails that login as well. If I specify the basic login (still preventing it from running both scenarios) it fails this as well. But when I run both scenarios in the same call the first one always fails and the second one always passes.

I even switched the order so that basic is run second instead of first and the first scenario (in this case advanced) fails and the second passes. So what in the world is going on?

As an extra bonus I went ahead and added a line that reads 'Then I follow "Log Out"' and all the sudden both scenarios pass again. Which would make sense if it weren't for the fact that it works fine during the second scenario. Shouldn't it fail both if it is already logged in? Is there a way to make sure that the cache for PhantomJS is cleared at the end of each scenario?

Feature: Login
    Login to app using a basic user
    Login to app using a multi-company user

    @javascript
    Scenario: Basic Login
        Given I am on "/"
        And I wait a few seconds
        When I fill in "login" with "ijones"
        And I fill in "password" with "SomeP@ssword"
        And I press "LoginButton"
        And I wait a few seconds
        Then I should see "By clicking below"
        And I press "OK"
        Then I should see "Welcome"

    @javascript
    Scenario: Advanced Login
        Given I am on "/"
        When I fill in "login" with "rtyler"
        And I fill in "password" with "SomeP@ssword"
        And I press "LoginButton"
        Then I wait for the company selection box
        And I click a field "company"
        And I pick "Test Company 2"
        And I press "CompanyLoginButton"
        Then I should see "By clicking below"
        And I press "OK"
        Then I should see "Welcome"
like image 431
Patrick Avatar asked Jun 24 '13 15:06

Patrick


1 Answers

Ok, I finally figured this bugger out. At least partially. It seems to be maintaining the session between scenarios. This doesn't fully explain why it is only the first call. But I suspect that PhantomJS is keeping the previous session open when the last scenario closes and that Mink rebuilds the session between scenarios, but not after the end of the last scenario.

Anyway, the solution is to create a context or a hook to reset the session at the end of each scenario call. For testing purposes I created a context that reads "And I reset the session".

I defined this as such:

/**
 * @Given /^I reset the session$/
 */
public function iResetTheSession() {
    $this->getSession()->reset();
}

When I call that context at the end of both scenarios it fixes the problem. So I think I will go ahead and make a post-scenario hook that takes care of this for me.

like image 165
Patrick Avatar answered Nov 02 '22 23:11

Patrick