Maintaining the the session between scenarios in a single feature file in behat and mink

I have done a lot of R&D on this but i am unable to find a solution.

I need to maintain a login session between different scenarios in a single feature file. I have made a function I am logged in and I have written in background. so at the start of every scenario the login happens. but what i want is to maintain a single login session across the scenarios. Can anyone suggest.?

example Code is :

Feature: To test the output

Background:
  Given I am logged in 

@javascript
 Scenario: To test the positive input
   When I fill in "test" with "aab"
   And I press "add"
   Then I should see "welcome"

@javascript
  Scenario:To test the negative inputs
    When I fill in "test" with "@#$@!!111"
    And I press "add"
    Then I should see "Sorry,invalid input please try again"

Now if another person reviews my code he would come to know about the positive and negative test cases. but every time the scenarios are reloaded,what if i have 50 scenarios in a feature. for bigger projects. it doesnt look good at every scenario i log in and i waste extra 15 minutes in total.what i want is after every scenario in a single feature file, the test continues with the same login session.

It can't be done. Behat scenarios are independent on purpose. Otherwise, you would risk state leaking from one scenario to another.

You're not approaching the problem from the right direction. Sacrificing scenario separation for speed improvements will hurt you in the long run.

Assuming that logging in is tested as one of the features, in other scenario where logging in is required, you don't have to be using the actual login form. Think of doing it programaticaly.

Also, you seem to be using Behat for functional testing, while it's build for verifying business expectations. You could consider using Mink directly, which would give you more power.

Maintaining the the session between scenarios in a single feature , Maintaining the the session between scenarios in a single feature file in behat and mink - session. And yes, we can go even further by telling each suites which features belong to them. Under the features/ directory, create two new directories called commands and web Let's organize: move ls.feature into commands/ and the other four features into web/.

It can be done! I've just found a solution - you need to create an AbstractWebDriver class that maintains a static instance of the webDriver.

FeatureContext

<?php
use Behat\Behat\Tester\Exception\PendingException;
use Behat\Behat\Context\Context;
use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;

use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\Behat\Hook\Scope\AfterScenarioScope;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\WebDriverBy;

/**
 * Defines application features from the specific context.
 */
class FeatureContext extends AbstractWebDriver
{
    /**
     * Initializes context.
     *
     * Every scenario gets its own context instance.
     * You can also pass arbitrary arguments to the
     * context constructor through behat.yml.
     */
    public function __construct()
    {
        $capabilities = DesiredCapabilities::safari();
        if(!AbstractWebDriver::$webDriver) {
            AbstractWebDriver::$webDriver = RemoteWebDriver::create('http://localhost:4444/wd/hub', $capabilities);
        }
        $this->baseUrl = "http://test.test.com";
    }
}

AbstractWebDriver

<?php

use Behat\Behat\Tester\Exception\PendingException;
use Behat\Behat\Context\Context;
use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;

use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\Behat\Hook\Scope\AfterScenarioScope;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;

/**
 * Defines application features from the specific context.
 */
abstract class AbstractWebDriver extends \PHPUnit\Framework\TestCase implements Context, SnippetAcceptingContext
{
    /**
     * @var \RemoteWebDriver
     */
    protected static $webDriver;
    protected $baseUrl;

protected function getDriver()
{
    if($this->webDriver==Null)
    echo "----------------- Instatiate New Driver -----------------";
    $capabilities = DesiredCapabilities::safari();
    self::$webDriver = RemoteWebDriver::create('http://localhost:4444/wd/hub', $capabilities);


    echo "----------------- Return Current Driver -----------------";
}



}

For one feature file, I can now run multiple scenarios on one instance of a webDriver!

Reuse sessions between tests · Issue #68 · minkphp , Is it possible to keep the browser window open between test sessions, and then it's possible to reuse same session between a tests in a single test case file. to run scenarios alone if you build them assuming that the previous one succeeded) I'm using this through Behat and MinkExtension, so I'm not really doing any  The Session::restart shortcut allows you to do these 2 steps in a single call. The drawback of closing the browser and starting it again is that it takes time. In many cases, a lower level of isolation is enough in favor of a faster resetting. The Session::reset method covers this use case.

What is the behaviour of this if one of the Scenarios fails ?

"Normally" (if each Scenario has his own session), every Scenario will be executed, even one is falling. But now, it will stop every Scenario after the one falling ?

Even if you don't have many Scenarios, it can be bothersome to have to correct your Scenario and to relaunch all yours Scenarios... After some time, you will have many Scenarios you will surely execute automatically, imagine if 1 test fails, and all others after are blocked. You will have to re-execute all to verify if those not executed are not failing!

Everything can be done in Php, you just need to think (more or less according to the problem). But the biggest question is "What the consequences ?".

If the developers choose to not do it, it must be a reason. So be aware of what will appends after.

Session is persisted between scenario (and feature for certain use , Sessions/cookies are persisted between scenario. I can check that So by default, Mink tries hard to reset your browser session without reloading it (cleans all domain cookies). http://stackoverflow.com/questions/19050988/maintaining-​the-the-session-between-scenarios-in-a-single-feature-file-in-behat MinkExtension will set the default Mink session for each scenario based on the configuration settings default_session and javascript_session and on scenario tags: A scenario tagged with @mink:foo will use foo as its default session;

Mink Session inside FeatureContext > BDD, Behat, Mink and other , Keep on Learning! Cardinal Rule: Avoid CSS in your Scenarios Because the person who is benefiting from the feature is a web user, and 166 lines vendor/​behat/mink-extension/src/Behat/MinkExtension/Context/RawMinkContext.php Now that we have the individual element, we can take action on it with one of the​  A feature usually contains a list of scenarios. You can write whatever you want up until the first scenario, which starts with Scenario: (or localized equivalent) on a new line. You can use tags to group features and scenarios together, independent of your file and directory structure.

Behat and Mink > BDD, Behat (version 2.5), Mink and other , Behat + Mink¶ Now that you're dangerous with Behat and an absolute expert in it into your file, and then run php composer.phar update behat/mink-extension to to get access to the Mink Session object from within our FeatureContext class. and now it makes sense why: every step in our scenario matches up with one  Behat will use the classes specified there as your feature contexts. This means that every time Behat sees a scenario in your test suite, it will: Get list of all context classes from this contexts option. Will try to initialize all these context classes into objects. Will search for step definitions and Hooks in all of them.

Acceptance Testing A Service With Behat And Mink Sessions, Acceptance Testing A Service With Behat And Mink Sessions Mink and Behat to simulate the interaction between a browser session and a simple service api. To do this, and keep our example simple, we'll create an index.php file in our Scenario: Logging in and setting the session # features/login.feature:6 Given I go​  So, in order to test that our web application behaves correctly, we need a way to simulate this interaction between the browser and the web application in our tests. We need Mink. Mink is an open source browser controller/emulator for web applications, written in PHP 5.3. Read Mink at a Glance to learn more about Mink and why you need it.

Comments
  • Do you just want to lazily 'log in' during each scenario if not already, or do your scenarios have dependencies between them?
  • Can you post the minimum amount of code needed to illustrate or reproduce the problem? Also, are you using an After hook to close the browser between scenarios?
  • @PaulMorie : i just want to save my testing time and all the scenarios in a feature file is related to a specific function which may have negative inputs positive inputs etc. so for each different case i get logged in each time, which slowdowns the perfomance.
  • @orde yes sir, the code is updated.
  • Are these scenarios session specific?