Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to skip BeforeStep, AfterStep for selected steps bindings? #952

Closed
ghost opened this issue Oct 9, 2017 · 10 comments
Closed

How to skip BeforeStep, AfterStep for selected steps bindings? #952

ghost opened this issue Oct 9, 2017 · 10 comments

Comments

@ghost
Copy link

ghost commented Oct 9, 2017

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()
{
}
@SabotageAndi
Copy link
Contributor

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.

@ghost
Copy link
Author

ghost commented Oct 9, 2017

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)
     {
     }
}

@SabotageAndi
Copy link
Contributor

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
		{
		}
	}
}

@ghost
Copy link
Author

ghost commented Oct 10, 2017

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?

@SabotageAndi
Copy link
Contributor

As far as I remember StepInfo.Text should be the text of the scenario in the feature file without the keyword at the beginning.

@ghost
Copy link
Author

ghost commented Oct 10, 2017

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

@SabotageAndi
Copy link
Contributor

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.

@SabotageAndi
Copy link
Contributor

Ah, the ScenarioStepContext is not in the DI container. Yeah, so you need to do ScenarioStepContext.Current.

@ghost
Copy link
Author

ghost commented Oct 10, 2017

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.

@ghost ghost closed this as completed Oct 10, 2017
@github-actions
Copy link

github-actions bot commented Jun 1, 2021

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 1, 2021
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant