Question

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 doesn't 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.

Was it helpful?

Solution

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.

OTHER TIPS

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!

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top