1

I'm using Nunit + .NetCore 2.2 in my project and I need to close the browser only after running all the tests and not after each test class. Currently the tests are closing the browser after each test class. I have many test classes with [Test], but I'm not using [TestFixture].

My class with [OneTimeSetup] is:

    public class TestBase
{
    [SetUp]
    public void Setup()
    {

    }


    [TearDown]
    public void TearDown()
    {

    }

    [OneTimeSetUp]
    public void OneTimeSetUp()
    {
        DriverFactory.CreateInstance();
        DriverFactory.INSTANCE.Navigate().GoToUrl("https://www.google.com.br");
    }

    [OneTimeTearDown]
    public void OneTimeTearDown()
    {
        DriverFactory.QuitInstace();
    }
}

And a test class

namespace Tests
{
    public class Tests : TestBase
    {
        [Test]
        public void SearchTests()
        {
            SearchPageObjects searchPageObjects = new SearchPageObjects();

            searchPageObjects.PreencherBuscar("Tempo em BH");
            Assert.IsTrue(searchPageObjects.VerificaNomeCidade("Belo Horizonte, MG"));
        }
    }
}
  • This Question is similar to [Specflow - Close browser after all tests are done ](stackoverflow.com/questions/47946175/…) try the solution provided in it. – Himanil Gupta Jul 1 at 15:31
  • Have you read the documenation, you need the SetUpFixutre attribute on the top of your class. link and link.TestFixture attribute marks a class that contains tests - link. – Nikolay Barakov Jul 2 at 11:24
  • @HimanilGupta The provided answer is for Specflow Hooks, the OP is asking about NUnit. – Nikolay Barakov Jul 2 at 11:31
1

I would create a globally accessible driver in your base class and set the driver once in your OneTimeSetup method. Make sure you're only closing or quitting the driver in the OneTimeTearDown method. In each PageObject class, just pass the driver you created earlier and use driver.Url = "{url}" or driver.Navigate().GoToUrl("{Url}") before any other actions.

If you find that OneTimeSetup is running more than once for some mysterious reason, I would try adding a bool variable that checks if it has been run, and if it has then don't do anything. (like the top answer here - https://stackoverflow.com/questions/12087959/junit-run-set-up-method-once).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.