在C#和SpecFlow

时间:2017-04-29 13:36:43

标签: c# selenium specflow selenium-grid

我正在试图在我的selenium C#项目中使用RemoteWebDriver实例撞墙。每当我运行测试时,我都会得到“对象引用未设置为对象的实例”

我用以下内容开始了我的中心......

java -jar "C:\Selenium Grid\selenium-server-standalone-3.4.0.jar" -role hub

使用以下内容启动我的节点...

java -Dwebdriver.chrome.driver=C:\WebDrivers\chromedriver.exe -jar "C:\Selenium Grid\selenium-server-standalone-3.4.0.jar" -role webdriver -hub http://10.0.75.1:4444/grid/register -browser browserName=chrome,platform=WINDOWS -port 5557

Hub主页....

enter image description here

然后我在单元测试文件中有以下代码....

[Binding]
public sealed class Hooks {

    private readonly IObjectContainer _objectContainer;
    private IWebDriver _driver;
    private PersonDetails _personDetails;

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

    [BeforeScenario]
    public void BeforeScenario()
    {
        //_driver = new ChromeDriver();
        var capabilities = new DesiredCapabilities();
        capabilities.SetCapability(CapabilityType.BrowserName, "chrome");
        capabilities.SetCapability(CapabilityType.Version, "53.0");
        capabilities.SetCapability(CapabilityType.Platform, "Windows 10");
        _driver = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"),capabilities, TimeSpan.FromSeconds(600));

        _objectContainer.RegisterInstanceAs<IWebDriver>(_driver);
        _personDetails = new PersonDetails {
            Name = "Barry",
            Age = 25
        };
        _objectContainer.RegisterInstanceAs<PersonDetails>(_personDetails);
    }

    [AfterScenario]
    public void AfterScenario()
    {
        _driver.Quit();
    }
}

它一直在这条线上徘徊

 _driver = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"),capabilities, TimeSpan.FromSeconds(600));

出现以下错误......

Result StackTrace:  
at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
   at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
   at OpenQA.Selenium.Remote.RemoteWebDriver.StartSession(ICapabilities desiredCapabilities)
   at OpenQA.Selenium.Remote.RemoteWebDriver..ctor(ICommandExecutor commandExecutor, ICapabilities desiredCapabilities)
   at OpenQA.Selenium.Remote.RemoteWebDriver..ctor(Uri remoteAddress, ICapabilities desiredCapabilities, TimeSpan commandTimeout)
   at SeleniumLoadable.Step_Definitions.Hooks.BeforeScenario() in C:\Users\matty\OneDrive\Visual Studio Projects\SeleniumLoadable\SeleniumLoadable\Step Definitions\Hooks.cs:line 33
   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 SeleniumLoadable.Features.ParallelFeature.ScenarioSetup(ScenarioInfo scenarioInfo)
   at SeleniumLoadable.Features.ParallelFeature.AddMoreNumbers() in C:\Users\matty\OneDrive\Visual Studio Projects\SeleniumLoadable\SeleniumLoadable\Features\Parallel.feature:line 7
--TearDown
   at SeleniumLoadable.Step_Definitions.Hooks.AfterScenario() in C:\Users\matty\OneDrive\Visual Studio Projects\SeleniumLoadable\SeleniumLoadable\Step Definitions\Hooks.cs:line 46
   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 SeleniumLoadable.Features.ParallelFeature.ScenarioTearDown()
Result Message: 
System.InvalidOperationException : 
TearDown : System.NullReferenceException : Object reference not set to an instance of an object.

有谁知道我做错了什么?

1 个答案:

答案 0 :(得分:1)

好的,这是更新:

Selenium 3.4.0-Java: https://github.com/SeleniumHQ/selenium/issues/3946

Selenium 3.4.0-C#: https://github.com/SeleniumHQ/selenium/issues/3939

c#nuget版本3.4.0中的远程WebDriver实例 https://github.com/SeleniumHQ/selenium/issues/3929

希望此信息可以帮助您:)