Search code examples
c#seleniumreportingspecflowextent

Injecting Multiple Objects into the object container


Im trying to introduce ExtentReports into my test suite and im having some issues with context injection, im trying to inject the reporting class into my object container along with my IWebDriver but i get the error message. "The ScenarioContext.Current static accessor cannot be used in multi-Threaded execution. This is bugging the heck out of me can anyone see where im going wrong ?

heres a snippet of the hooks class

   class Hooks : ReportingStepDefinitions
{

    private readonly IObjectContainer _objectContainer;
    private TestReports _report; 
    private IWebDriver _driver;



    public Hooks (IObjectContainer objectContainer)
    {
        _objectContainer = objectContainer;      
    }   

    [BeforeScenario]
    public void initialise()
    {  
        _report = new TestReports();
        _report.startTest();
        _driver = new ChromeDriver(@"C:\\TestData\Dependencies")
        _objectContainer.RegisterInstanceAs<IWebDriver>(_driver);
        _objectContainer.RegisterInstanceAs<TestReports>(_report);
}

i haven't included the close statements as im pretty sure this is where the issue lies ... all help appreciated

Editing to Include Stack Trace

Result StackTrace:  
Server stack trace: 
   at SpecResults.ReportingAspect.ReportingMessageSink.<>c__DisplayClass1.<SyncProcessMessage>b__0()

Exception rethrown at [0]: 
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
   at OCCSpecFlow.Hooks.initialise()
   at lambda_method(Closure , IContextManager )
   at TechTalk.SpecFlow.Bindings.BindingInvoker.InvokeBinding(IBinding binding, IContextManager contextManager, Object[] arguments, ITestTracer testTracer, TimeSpan& duration)
   at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.FireEvents(HookType bindingEvent)
   at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.OnScenarioStart(ScenarioInfo scenarioInfo)
   at TechTalk.SpecFlow.TestRunner.OnScenarioStart(ScenarioInfo scenarioInfo)
   at SpecFlowAutomation.FeatureFiles.HomepageFeature.ScenarioSetup(ScenarioInfo scenarioInfo)
   at SpecFlowAutomation.FeatureFiles.HomepageFeature.ViewingTheHomepageJumps() in C:\Source\dev\AutomationFramework\SpecFlowAutomation\FeatureFiles\HomePage.feature:line 10
--TearDown

Server stack trace: 
   at SpecResults.ReportingAspect.ReportingMessageSink.<>c__DisplayClass1.<SyncProcessMessage>b__0()

Exception rethrown at [0]: 
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
   at OCCSpecFlow.Hooks.cleanup()
   at lambda_method(Closure , IContextManager )
   at TechTalk.SpecFlow.Bindings.BindingInvoker.InvokeBinding(IBinding binding, IContextManager contextManager, Object[] arguments, ITestTracer testTracer, TimeSpan& duration)
   at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.FireEvents(HookType bindingEvent)
   at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.OnScenarioEnd()
   at TechTalk.SpecFlow.TestRunner.OnScenarioEnd()
   at SpecFlowAutomation.FeatureFiles.HomepageFeature.ScenarioTearDown()
Result Message: 
TechTalk.SpecFlow.SpecFlowException : The ScenarioContext.Current static accessor cannot be used in multi-threaded execution. Try injecting the scenario context to the binding class. See http://go.specflow.org/doc-multithreaded for details.
TearDown : System.NullReferenceException : Object reference not set to an instance of an object.

Solution

  • Found that the Response was due to specflow having a bit of a hiccup when i tried to use this over its own ScenarioContext container. since moving the object container out and returning to the scenario context all started working.