Home > Software engineering >  Selenium test issue with browser not closing after testing
Selenium test issue with browser not closing after testing

Time:06-30

I'm writing tests in C# using Selenium and Nunit. They're written for Chrome and sample test is starting fine, opening browser, navigating to page and testing properly. After passing(or failing) the test it should (the browser )close but it doesn't and i do not know why. Here's the code and Im hoping someone will point me to the right way.

  public void Test1()
    {
        IWebDriver driver = new ChromeDriver();
        ChromeOptions options = new ChromeOptions();
        options.AddArguments("start-maximized");
        options.AddArguments("--disable-extensions");
        driver = new ChromeDriver(options);
        driver.Navigate().GoToUrl("https://name-of-the-site.com/");
        string check = driver.Title;
        Console.WriteLine("Site title");
        Console.WriteLine(check);
        String expectedTitle = "name of the site";
        StringAssert.AreEqualIgnoringCase(expectedTitle, check);
        if (check == expectedTitle)
        {
            Console.WriteLine("Title Confirmed");
        }
        else
        {
            Console.WriteLine("Title Not Confirmed");
        }
        Assert.Pass();
        driver.Close();
        driver.Quit();   }

Thanks in advance

CodePudding user response:

You need to add an TearDown with the code to close and quit to run after, like this:

[TearDown]
   public void QuitBrowserAndCloseConnection() { 
    driver.Close();
    driver.Quit();
} 

This is necessary to force to run this code if the test pass or fail.

  • Related