Selenium test issue with browser not closing after testing

Issue

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

Solution

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.

Answered By – Matheus Leão

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published