将多个对象注入对象容器

时间:2017-02-06 12:51:50

标签: c# selenium reporting specflow extent

我试图将ExtentReports引入我的测试套件并且我在上下文注入方面存在一些问题,我试图将报告类与我的IWebDriver一起注入我的对象容器但是我得到了错误消息。 " ScenarioContext.Current静态访问器不能用于多线程执行。 这可能会让我感到困扰任何人都可以看到我哪里出错了?

这是一个钩子类的片段

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

我还没有收到密切的陈述,因为我很确定这就是问题所在......所有人都非常感谢

编辑以包含堆栈跟踪

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.

1 个答案:

答案 0 :(得分:0)

发现当我试图在自己的ScenarioContext容器上使用specflow时,响应是由于Specflow有点打ic。由于将对象容器移出并返回到场景上下文,所有这些都开始工作。