0

I am receiving NullReferenceException on CleanUp() method. The below code is from SpecflowHooks.cs

Driver code in SpecflowHooks

    public static ChromeDriver driver { get; set; }
[BeforeFeature]

public static void OneTime()

{
    ChromeOptions options = new ChromeOptions();
    options.AddArgument("--ignore-ssl-errors=yes");
    options.AddArgument("--ignore-certificate-errors");
    string Path = "D:/repos/Hoogly/Drivers/"; 
    driver = new ChromeDriver(Path);
    driver.Manage().Window.Maximize(); 
    driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);

}

    private readonly IObjectContainer container;

    public SpecflowHooks(IObjectContainer container)
    {
        this.container = container;
    }        

[BeforeScenario]
    public void Setup()
    {
        container.RegisterInstanceAs(driver);
    }

[AfterScenario]
    public void CleanUp()
    {
        container.Resolve<IWebDriver>();
        driver.Close();
        driver.Dispose();

    }

While Debugging I noticed that the Container is blank when Cleanup() method is executed. But I am not clear as to why that is getting blank even after the Register Instance in Setup(). I am using Specflow and Selenium Webdriver I am not sure if this is the optimal way to manage drivers.

1 Answers1

0

There are a number of problems with the code:

  1. The call to container.RegisterInstanceAs passes the driver object, so it is registering the web driver as an instance of ChromeDriver. Change this to:

    container.RegisterInstanceAs<IWebDriver>(driver);
    //                          ^^^^^^^^^^^^
    
  2. Do not dispose of the web driver after each scenario. The BeforeFeature hook executes once per feature. The AfterScenario hook executes multiple times per feature (once for each scenario in that feature file). If you create the web driver once per feature, dispose of it once per feature.

    [AfterFeature]
    public void CleanUp()
    {
        driver.Close();
        driver.Dispose();
    }
    

The full class should look like:

[Binding]
public class SpecflowHooks
{
    public static ChromeDriver driver { get; set; }

    private readonly IObjectContainer container;

    public SpecflowHooks(IObjectContainer container)
    {
        this.container = container;
    }        

    [BeforeFeature]
    public static void OneTime()
    {
        ChromeOptions options = new ChromeOptions();
        options.AddArgument("--ignore-ssl-errors=yes");
        options.AddArgument("--ignore-certificate-errors");
        string Path = "D:/repos/Hoogly/Drivers/"; 
        driver = new ChromeDriver(Path);
        driver.Manage().Window.Maximize(); 
        driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
    }

    [BeforeScenario]
    public void Setup()
    {
        container.RegisterInstanceAs<IWebDriver>(driver);
    }

    [AfterFeature]
    public static void CleanUp()
    {
        driver.Close();
        driver.Dispose();
    }
}
Greg Burghardt
  • 17,900
  • 9
  • 49
  • 92