I have defined [AfterScenario] binding in my stepdefinition class and have method which will be executed after each scenario, but from some reason this isn't working. My method isn't called after each scenario, so excel remains open after some tests... Also there is no log for some unsuccessful tests. Why? Do you have any idea or suggestions?
Here is my method:
[AfterScenario] public void AfterScenario() { if (TestContext.CurrentContext.Result.State != TestState.Success) ErrorLog(); excelDriver.Stop(); }
Hooks (event bindings) can be used to perform additional automation logic at specific times, such as any setup required prior to executing a scenario. In order to use hooks, you need to add the Binding attribute to your class: [Binding] public class MyClass { ... }
ScenarioContext helps you store values in a dictionary between steps. This helps you to organize your step definitions better than using private variables in step definition classes. There are some type-safe extension methods that help you to manage the contents of the dictionary in a safer way.
I was playing with the BeforeScenario and AfterScenario stuff myself just today for the first time.
I tagged my scenario with @sometag and then with the attribute on my AfterScenario() method used the same tag (without the '@' or at sign) as an argument, e.g.
Feature file:
@sometag Feature: Some feature or other Here is my feature description ... Scenario: Some scenario ...
Events file:
[AfterScenario("sometag")] public void AfterScenario() { // stuff... }
I don't know if tagging stuff like that is the key. Maybe without tags the AfterScenario() would always fire, I didn't try it.
The other thing you can try is attaching VS to the nunit-agent.exe process, setting a breakpoint inside your AfterScenario() method and seeing when it is hit.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With