Reputation: 193
I have following BeforeScenario hook in specflow:
private readonly ScenarioContext _scenarioContext;
public Hooks(ScenarioContext scenarioContext, ITestOutputHelper testOutput)
{
_scenarioContext = scenarioContext;
_testOutput = testOutput;
}
[BeforeScenario]
public async Task TestFixtureSetUp()
{
SetAttachmentFileName();
var page = BrowserSession.Page;
_scenarioContext.ScenarioContainer.RegisterInstanceAs(page);
_scenarioContext.ScenarioContainer.RegisterInstanceAs(page.Context);
_scenarioContext.ScenarioContainer.RegisterInstanceAs(page.Context.Browser!);
_scenarioContext.ScenarioContainer.RegisterInstanceAs(page.PageLocator());
_scenarioContext.ScenarioContainer.RegisterInstanceAs(_testsConfig);
_scenarioContext.ScenarioContainer.RegisterInstanceAs(_configuration);
await BrowserSession.StartChunkTracing(_scenarioContext.ScenarioInfo.Title);
}
Now I want to create AfterFeature hook to cleanup some data after feature, but I don't know how to access created pages or scenario Context inside AfterFeature method.
Upvotes: 0
Views: 487
Reputation: 18868
The [AfterScenario]
hook does not have access to any of the scenario-related objects, but it does have access to the object container (i.e., the IOC container or DI container).
Based on your code snippet, the FeatureContext is all you need:
[AfterFeature]
public static void AfterFeature(FeatureContext feature)
{
var page = feature.FeatureContainer.Resolve<YourPageClassHere>();
// ^^^^^^^^^^^^^^^^ <-- IObjectContainer
}
Anything you need that is scenario-specific must go in an [AfterScenario]
hook:
[AfterScenario]
public void AfterScenario()
{
// Just me making a big assumption here
await BrowserSession.StopChunkTracing(_scenarioContext.ScenarioInfo.Title);
}
Upvotes: 0