Hi,
I have a global method with [BeforeStep] and [AfterStep] attribute. It is calling before and after all steps definitions in my tests dll.
Is it possible to skip execute it before and after selected steps. Something like that:
[Then(@"I should...."), @DO_NOT_RUN_BEFORE_STEP]
public void TESTSTEP()
{
}
You can use scoping (http://specflow.org/documentation/Scoped-Bindings/) or get the name of the scenario from the ScenarioContext and handle this in your step code.
I am not sure using scope is the correct way, when I am trying this:
[Then(@"I should..."), Scope(Tag='')]
public void TESTSTEP()
{
}
this step is not recognize in feature file.
I also don't want to skip beforestep and afterstep in all scenario steps but in one selected.
Scenario: TestScenario
Given TESTSTEP1 #call global [BeforeStep][AfterStep] method
And TESTSTEP2 #do not call global [BeforeStep][AfterStep] method
When TESTSTEP3 #call global [BeforeStep][AfterStep] method
[BeforeStep]
[AfterStep]
public static void GlobalBEFOREAFTER()
{
try
{
CHECK_IF RUN CODE
}
catch (Exception ex)
{
}
}
Sorry, I misread step with scenario. Scopes don't work here.
You have to do this in code like this:
[Binding]
public class BindingClass
{
private ScenarioStepContext _scenarioStepContext;
public BindingClass(ScenarioStepContext scenarioStepContext)
{
_scenarioStepContext = scenarioStepContext;
}
[BeforeStep()]
[AfterStep()]
public BeforeAfterHook()
{
if (_scenarioStepContext.StepInfo.Text == "") //check for step
{
}
else
{
}
}
}
Thx for help :)
What is under "_scenarioStepContext.StepInfo.Text" name of the step method or step Then/Given/And text attribute?
What when I have 10 steps method, do I need to add 10 ifs in BeforeAfterHook method? Can I mark somehow these steps methods to do not use BeforeAfterHook step method?
As far as I remember StepInfo.Text should be the text of the scenario in the feature file without the keyword at the beginning.
Yeah. but this is not the best approach :(
The better would be to add attribute to steps method to not use BeforeAfterHook method.
PS. I got nullReferenceException on ScenarioStepContext
Edit: You missed Current :). ScenarioStepContext.Current.StepInfo.Text
Sorry, we don't have anything else to achieve your requirements. It would need to extend Scoping for a step level. Feel free to submit an PR for that.
Ah, the ScenarioStepContext is not in the DI container. Yeah, so you need to do ScenarioStepContext.Current.
I solved the problem :)
ScenarioStepContext.Current.StepInfo.BindingMatch.StepBinding.Method.Type.Name return the name of class for current step. I just need to put all steps I don want to use BeforeAfterHook method for in one class, then put it name in if loop.
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Most helpful comment
I solved the problem :)
ScenarioStepContext.Current.StepInfo.BindingMatch.StepBinding.Method.Type.Namereturn the name of class for current step. I just need to put all steps I don want to use BeforeAfterHook method for in one class, then put it name in if loop.