将多个对象注入对象容器
Injecting Multiple Objects into the object container
我试图将 ExtentReports 引入到我的测试套件中,但我在上下文注入方面遇到了一些问题,我试图将报告 class 与我的 IWebDriver 一起注入到我的对象容器中,但我收到了错误消息。 “ScenarioContext.Current 静态访问器不能用于多线程执行。
这让我很烦,谁能看出我哪里出错了?
这里是钩子的片段 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);
}
我没有包含关闭语句,因为我很确定这就是问题所在...感谢所有帮助
编辑以包含堆栈跟踪
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.
发现响应是由于当我尝试在它自己的 ScenarioContext 容器上使用它时 specflow 有点小问题。自从将对象容器移出并返回场景上下文后,所有这些都开始工作了。
我试图将 ExtentReports 引入到我的测试套件中,但我在上下文注入方面遇到了一些问题,我试图将报告 class 与我的 IWebDriver 一起注入到我的对象容器中,但我收到了错误消息。 “ScenarioContext.Current 静态访问器不能用于多线程执行。 这让我很烦,谁能看出我哪里出错了?
这里是钩子的片段 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);
}
我没有包含关闭语句,因为我很确定这就是问题所在...感谢所有帮助
编辑以包含堆栈跟踪
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.
发现响应是由于当我尝试在它自己的 ScenarioContext 容器上使用它时 specflow 有点小问题。自从将对象容器移出并返回场景上下文后,所有这些都开始工作了。