Closing Driver after all test are runned



  • 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"));
            }
        }
    }
    


  • 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).


Log in to reply
 

Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2