'Is it possible to use variables in beforeFeature Hook as part of the Specflow scenario statement?
I am pretty new to Specflow and I'm trying see if this is possible with Specflow BeforeFeature Hook. I have some dynamic test data setup that is only for a particular feature. I want to use these test data values as part of the scenario test. This is one of the scenario tests of the feature file:
@logintest
Feature: User login
Scenario: User login with different product access
Given I am in <productType> login page
When I login as <user>
I should see <productType> logo in my account
Inside the BeforeFeature(), we have functions that generate: -a random user account that has a property of product access type - random urls for a product login page. (e.g: productA234234.dev.local)
[Binding]
public class LoginHooks
{
[BeforeFeature("logintest")]
public static void BeforeFeature()
{
UserAccount testUser = new UserAccount(productType1);
var product1Url = CreateProductUrl(productType1);
FeatureContext.Current.Set("testUser", testUser);
FeatureContext.Current.Set("productUrl", productUrl);
...
}
Because the username is generated randomly each time on tests run, I couldn't provide the username to the Gherkins as part of data tables etc. Is there a way to construct the Gherkins to take in values from FeatureContext?
[Binding]
public class UserLoginSteps
{
//var productUrl = ???
[Given(@"I am in (.*) login page")]
public void GivenIAmInLoginPage(string productUrl)
{ . . .}
}
Question:
- is it possible to get FeatureContext values for the productUrl and set in the step?
- I think the regex Given is incorrect in such case. Is there another way to construct it to take a variable as part of the data input?
Thanks!
Solution 1:[1]
Working with a random data when you are with a BDD is a bad approach AFAK so I think you need to change your feature to something like this
@alogintest
Feature: SpecFlowFeature1
try many user logins
Scenario Outline: User login with different product access
Given I am in <productType> login page
And the user is <user>
When I login
Then I should see <prouctResult> logo in my account
Examples:
| productType | userAccount | userPwd | productTypeResult |
| P1 | u1 | pwd1 | p1 |
| P2 | u2 | pwd2 | p2 |
is it possible to get FeatureContext values for the productUrl and set in the step?
yes you can just add a column for each specific productTypeUrl for example
Note
that you can add as many as you want scenarios
I think the regex Given is incorrect in such case. Is there another way to construct it to take a variable as part of the data input?
Yes because the placeholder is used only with Scenario Outlines
and here your steps generated
using TechTalk.SpecFlow;
namespace loginTest
{
[Binding]
public class SpecFlowFeature1Steps
{
[Given(@"I am in P(.*) login page")]
public void GivenIAmInPLoginPage(int p0)
{
ScenarioContext.Current.Pending();
}
[Given(@"the user is (.*)")]
public void GivenTheUserIs(string p0)
{
ScenarioContext.Current.Pending();
}
[When(@"I login")]
public void WhenILogin()
{
ScenarioContext.Current.Pending();
}
[Then(@"I should see (.*) logo in my account")]
public void ThenIShouldSeeLogoInMyAccount(string p0)
{
ScenarioContext.Current.Pending();
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
