C# Selenium driver - close all web pages after finish

Viewed 164
        [SetUp]
        public void Setup()
        {
           //open web page
        }
        [Test]
        public void Test()
        {
           //test stuff
        }

C# selenium driver has two methods, you can set up for example opening browser and going to specific web page, and then testing specific elements, so you don't have to write opening part in each test method.

But then all these web pages will stay open until you close them manually. Is there an equivalent for Setup that runs after test has been completed, so you can close browser automatically?

1 Answers

Here is a way you can close each browser after the test finishes (assuming you are using Nunit and not MSTest, which it appears you are). You would place this at the bottom, after all your tests:

[TearDown]
public void TearDown()
{
    if (driver != null)
    {
        driver.Dispose();
    }
}
Related