Reputation: 4625
I have a feature file as below
Feature: Test send API request
In order to test my API
As a Tester
I want to be able to perform HTTP request
Scenario:Sending GET request to activate user after registration api to verify whether the response code is 403 when 'X-Auth-Token' is missing
When I have a request "GET /api/activateuser?token=:tokenhash"
And I set the "Accept" header to "application/json"
And I set the "X-Auth-Token" header to "0125ee8dfe42bafbec95aa0d2676c91d8a780715b76504cf798aae6e74c08a30"
.
.
Scenario:Sending GET request to activate user after registration api to verify whether the response code is 403 when 'X-Auth-Token' is invalid
When I have a request "GET /api/activateuser?token=:tokenhash"
And I set the "Accept" header to "application/json"
And I set the "X-Auth-Token" header to "0125ee8dfe42bafbec95aa0d2676c91d8a780715b76504cf798aae6e74c08a30"
.
.
Scenario:Sending GET request to activate user after registration api to verify whether the response code is 404 when userid is invalid
When I have a request "GET /api/activateuser?token=:tokenhash"
And I set the "Accept" header to "application/json"
And I set the "X-Auth-Token" header to "0125ee8dfe42bafbec95aa0d2676c91d8a780715b76504cf798aae6e74c08a30"
.
.
In the request 'X-Auth-Token' parameter will be same for all scnerios, which will not change frequently. So I was thinking of setting it to some variable and use that variable in the scenarios. But havent found any method to do this in behat. It is okay evenif we can set the value in behat.yml and use it in the scenario, but even that was not possible.
Also i have more than one parameters that needed to be set like this.
So is there any method to set the value once and resuse it in every scenario?
Upvotes: 0
Views: 847
Reputation: 12740
You can use combination of two.
What happens below is this.
@BeforeScenario
tag runs prepare()
method before everything else to set your variable for current feature session.
Step definitions under Background
task run before each scenarios so you don't have to duplicate them in each scenarios.
NOTE: If your X-Auth-Token
won't change frequently then just hard-code the value in your FeatureContext file and don't implement step 2 above at all. My example is there to give you an idea of some useful features of Behat.
EXAMPLE
Adjust it for your need!
FeatureContext
namespace Your\Bundle\Features\Context;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
...
class FeatureContext ...
{
private static $xAuthToken;
/**
* @BeforeFeature
*/
public static function prepare()
{
self::setXAuthToken();
}
private static function setXAuthToken()
{
self::$xAuthToken = 123;
}
/**
* @Given /^I set the header "([^"]*)" to "([^"]*)"$/
*/
public function iSetTheHeader($header, $value)
{
// Do whatever you want
}
/**
* @Given /^I send "([^"]*)" request to "([^"]*)"$/
*/
public function iSendRequest($method, $url)
{
// Do whatever you want
}
/**
* @Given /^the X-Auth-Token is available$/
*/
public function theXAuthTokenIsAvailable()
{
echo self::$xAuthToken;
}
}
Feature file
Feature: Shared token
Background: I am common to all scenarios
Given I set the header "Accept" to "application/json"
When I send "GET" request to "/api/hello-world"
Scenario: 1
Given the X-Auth-Token is available
Scenario: 2
Given the X-Auth-Token is available
RESULT
Upvotes: 1