Specflow Advanced
Contents
- 1 Parameterized Step Definitions
- 2 Creating a step definition with a data table
- 3 Sharing and maintaining state between step definitions
- 4 Hooks
- 5 Mapping multiple Steps to a Single Step Definition
- 6 Execute Other Steps from Within a Step Definition
- 7 Using Step Bindings from External Assemblies
- 8 Sharing Data Between Steps with Context Injection
- 9 Conversions
Parameterized Step Definitions
for similar steps like
Given there is 5 gram of toothpaste Given there is 10 gram of toothpaste
we can delete step definitions and generate them by right clicking scenario. It generates definition like
Given_there_is_p0_gram_of_toothpaste(int p0)
or we can write this line like
Given_there_is_GRAMS_gram_of_BRAND_toothpaste(int grams, string brand)
Creating a step definition with a data table
if the scenario contains data tables, step definition can be:
Given_there_is_p0_gram_of_toothpaste(Table dataTable) var firstdata = table.Rows[0][0]; var seconddata = table.Rows[0][1];
or
var firstdata = table.Rows[0]["ToolName"]; var seconddata = table.Rows[0]["ToolQuality"];
Sharing and maintaining state between step definitions
to save it
Scenario.Context.Current.Add("brand name", brand);
to Read it
Scenario.Context.Current("brand name");
or in specs we may define a higher level variable under public class someStep
Hooks
The hooks (event bindings) can be used to perform additional automation logic on specific events, like before the execution of a scenario. The hooks are global but can be restricted to run only for features or scenarios with a specific tag (see below). The execution order of hooks for the same event is undefined.
SUPPORTED HOOK ATTRIBUTES
[BeforeTestRun] [AfterTestRun]
automation logic that has to run before/after the entire test run Note: As the most of the unit test runners do not provide a hook for executing logic after the test execution has been finished, the [AfterTestRun] event is triggered by the test assembly unload event. The exact timing and thread of this execution might be different for each test runner. The method it is applied to must be static.
[BeforeFeature] [AfterFeature]
automation logic that has to run before/after executing each feature The method it is applied to must be static.
[BeforeScenario] or [Before] [AfterScenario] or [After]
automation logic that has to run before/after executing each scenario or scenario outline example The short attribute names are available from v1.8.
[BeforeScenarioBlock] [AfterScenarioBlock]
automation logic that has to run before/after executing each scenario block (e.g. between the "givens" and the "whens")
[BeforeStep] [AfterStep]
automation logic that has to run before/after executing each scenario step You can annotate a single method with multiple attributes.
TAG FILTERING
Most of the hooks support tag filtering. This means that they are executed only if the feature or the scenario has at least one of the tags specified in the tag filter (so the tags are combined with OR). The tag can be made by specifying the tag in the attribute or with Scoped Bindings (from v1.8). The following example starts Selenium for scenarios marked with @web tag.
[BeforeScenario("web")]
public static void BeforeWebScenario()
{
StartSelenium();
}
For the scenario, scenarioblock or step events, the following tags are considered:
- tag on the feature
- tag on the scenario
- tag on the scenario outline
- tag on the scenario outline example set (Examples:)
For more complex filtering the ScenarioContext class can be used. The following example starts selenium if the scenario is tagged with @web and @automated.
[BeforeScenario("web")]
public static void BeforeWebScenario()
{
if(ScenarioContext.Current.ScenarioInfo.Tags.Contains("automated"))
StartSelenium();
}
Mapping multiple Steps to a Single Step Definition
[Given(@'I have signed in as '(.*)'")]
[Given(@'I have logged in as '(.*)'")]
public void GivenIHaveSignedInAs(string p0)
{
.....
}
Execute Other Steps from Within a Step Definition
First inherit from Steps
[Binding]
public class CalculatorAdditionSteps:Steps
{
Then we can call steps like this
[When(@"I add (.*) and (.*) together")]
public void WhenIAddAndTogether(int p0, int p1)
{
When("I enter (.*) into the calculator");
but we have to place (.*) with actual parameters
When(string Format("I enter {0} into the calculator", p0));
Using Step Bindings from External Assemblies
Sharing Data Between Steps with Context Injection
Using Hooks to run Additional Automation Code
Test Run and Feature Hooks
Scenario Related Hooks
Conversions
Please Check Specflow Page