Hi All
Specflow - which brings developer and business domain expert on the same platform and provides the base of Behavior driven development(BDD). SpecFlow also supports the concepts of Acceptance Test Driven Development (ATDD) and Behavior Driven Development (BDD), which are often used synonymously with Specification-By-Example.
While working on the complex scenarios i had to generate the data randomly and use them for next step. Using session variables are obviously not a choice, as it won't be available inside your sepcflow test project(ofcourse System.Web.HttpContext.Current will be null). So i used something called ScenarioContext. It comes with Specflow. ScenarioContext is a class that stores information relative to the state of the current scenario. One of its greatest uses is to pass values between steps. Please note that I am specifically talking about Scenario, not feature. So the ScenarioContext is not valid outside the scope of scenario.
Just like session variables, it is quite easy to use. You need to store the variable in current context and you can retrieve the value later in some other step wherever its required.
ScenarioContext.Current.Add("name",firstName);
Specflow - which brings developer and business domain expert on the same platform and provides the base of Behavior driven development(BDD). SpecFlow also supports the concepts of Acceptance Test Driven Development (ATDD) and Behavior Driven Development (BDD), which are often used synonymously with Specification-By-Example.
While working on the complex scenarios i had to generate the data randomly and use them for next step. Using session variables are obviously not a choice, as it won't be available inside your sepcflow test project(ofcourse System.Web.HttpContext.Current will be null). So i used something called ScenarioContext. It comes with Specflow. ScenarioContext is a class that stores information relative to the state of the current scenario. One of its greatest uses is to pass values between steps. Please note that I am specifically talking about Scenario, not feature. So the ScenarioContext is not valid outside the scope of scenario.
Just like session variables, it is quite easy to use. You need to store the variable in current context and you can retrieve the value later in some other step wherever its required.
ScenarioContext.Current.Add("name",firstName);
Now "name" can be retrieved in the current context / any of the step definition
var name = ScenarioContext.Current.Get<string>("name");
We can make use to generics as well. So nice and simple, give a try and reuse the steps. Hope it helps
Thanks
Nishant
I would recommend to take a look at the "context injection" option too! http://go.specflow.org/doc-sharingdata
ReplyDeleteHello Gaspar, thanks for sharing the details. Looks good.
Delete