Specflow -Part5(Specflow Sharing Data Between Steps)
While automating applications you may come across scenarios where you may need to share data between steps, we will talk about how one can do so in this lecture
Let's say you are automating google search functionality and scenario is user types search string on the google search page and hits search and this takes him to the google result page where he asserts if the results match the search string. You can have a single step definition file for all these steps or you can have a separate one for each page.
Ideally, you should have a separate step definition file for each page.
We can share the data between steps using below ways
Here 'QA' is the data which we want to assert in Then step, below is the step definition file
Here we created an instance variable which holds empty value initially and is populated when the given step is executed. As the steps belong to the same class we can directly access the instance variable value in Then step.
Well, that's simple...
What if you need data in other step definition files? Use Context, check below
Context
We can store the data in Scenario or Feature Context and the steps can get the values from there
We can use ScnearioContext or FeatureContext depending on the requirement
We will modify our earlier step definition implementation and put the steps in their respective classes, check below updated code
Here we added the search string value in scenario context so that other upcoming steps of the same scenario can access the value
In Then step, we got the search string value from scenario context
Similarly, if you want to share data between scenario's we can use Feature context
We will talk about some more features for Scenario and Feature context in the next lecture(Hooks),
That's all for this lecture.
Let's say you are automating google search functionality and scenario is user types search string on the google search page and hits search and this takes him to the google result page where he asserts if the results match the search string. You can have a single step definition file for all these steps or you can have a separate one for each page.
Ideally, you should have a separate step definition file for each page.
We can share the data between steps using below ways
- Instance variables (Steps are in same file)
- Context
Instance variables
We will create a feature file for the above example and then we can talk over it
Feature: SpecFlowFeature4 To demo how one can share data between steps Scenario: Verify user can share data between steps contained in same step defination file Given User opens google search page and search for 'QA' Then He should see the respective search results
Here 'QA' is the data which we want to assert in Then step, below is the step definition file
namespace SpecflowTest { [Binding] public class SpecFlowFeature4Steps { string searchString = String.Empty; [Given(@"User opens google search page and search for '(.*)'")] public void GivenStep(string searchString) { this.searchString = searchString; //Code to open google and search goes here } [Then(@"He should see the respective search results")] public void ThenHeShouldSeeTheRespectiveSearchResults() { //Assert if search results matches the search string Console.WriteLine(this.searchString); } } }
Here we created an instance variable which holds empty value initially and is populated when the given step is executed. As the steps belong to the same class we can directly access the instance variable value in Then step.
Well, that's simple...
What if you need data in other step definition files? Use Context, check below
Context
We can store the data in Scenario or Feature Context and the steps can get the values from there
We can use ScnearioContext or FeatureContext depending on the requirement
We will modify our earlier step definition implementation and put the steps in their respective classes, check below updated code
namespace SpecflowTest { [Binding] public class GoogleSearchPage { string searchString = String.Empty; [Given(@"User opens google search page and search for '(.*)'")] public void GivenStep(string searchString) { this.searchString = searchString; //Code to open google and search goes here //Put the data in scneario context //Other Scenario steps can access the value ScenarioContext.Current.Add("searchString", searchString); } } [Binding] public class GoogleSearchResultPage { [Then(@"He should see the respective search results")] public void ThenHeShouldSeeTheRespectiveSearchResults() { //Get the searchString for the scenario context string searchStringValue = ScenarioContext.Current.Get<string>("searchString"); //Assert if search results matches the search string Console.WriteLine(searchStringValue); } } }
Here we added the search string value in scenario context so that other upcoming steps of the same scenario can access the value
In Then step, we got the search string value from scenario context
Similarly, if you want to share data between scenario's we can use Feature context
We will talk about some more features for Scenario and Feature context in the next lecture(Hooks),
That's all for this lecture.
Comments
Post a Comment