Olena Kolesnik
Olena Kolesnik

Reputation: 13

NUnit Selenium webdriver is not closed after running all tests

I am trying to open browser, run several tests and close browser. My problem is that browser stays open after running all tests. What is the correct way of closing browser? If I put [Setup] and [TearDown] attributes to open and close browser in each test - browser is opened and closed, but if I put [OneTimeSetup] and [OneTimeTearDown] attributes - browser stays opened.

using Atata;
using NUnit.Framework;
namespace UITests
{
[TestFixture]
public class UITestFixture
{
    [OneTimeSetUp]
    public void SetUp()
    {
        //Open Chrome
         AtataContext.Configure().
         ApplyJsonConfig("Atata.json").
         Build();
    }

    [OneTimeTearDown]
    public void TearDown()
    {
        AtataContext.Current?.CleanUp();
    }
}
}

using Atata;
using NUnit.Framework;
namespace UITests
{
class Tests : UITestFixture
{
    [Test]
    public void PositiveTest()
    {
         Go.To<OverallViewPage>().ItemToVerify1.Should.Equal("2,006.59");
    }
    [Test]
    public void NegativeTest()
    {
        Go.To<OverallViewPage>().ItemToVerify2.Should.Equal("2,006.59");
    }

}
}
using Atata;
namespace UITestFixture
{    
    using _ = OverallViewPage;
    public class OverallViewPage : Page<_>
    {
        [FindByXPath("/html/body/div[2]/app-root/pnl/div/div/pnl-total/pnl-total-grid/main-grid/div/div/div/main-grid-row/div[2]/main-grid-row/div[4]/div[3]/div/span")]
        public Link<_> ItemToVerify1 { get; private set; }

        [FindByXPath("/html/body/div[2]/app-root/pnl/div/div/pnl-total/pnl-total-grid/main-grid/div/div/div/main-grid-row/div[2]/main-grid-row/div[4]/div[2]/div/span")]
        public Link<_> ItemToVerify2 { get; private set; }
    }
}

Upvotes: 1

Views: 966

Answers (1)

Yevgeniy Shunevych
Yevgeniy Shunevych

Reputation: 1163

It is possible but not correct to use the same AtataContext in different tests. Separate AtataContext should be created per each UI test, as AtataContext also collects log during test execution. But you can reuse the same web driver instance by different tests by passing already created instance of RemoteWebDriver to each AtataContextBuilder.

Check out the sources of Fixture Reusing Driver Atata sample project.

Basically you need to enhance base test fixture class like below:

using Atata;
using NUnit.Framework;
using OpenQA.Selenium.Remote;

namespace AtataSamples.FixtureReusingDriver
{
    [TestFixture]
    public class UITestFixture
    {
        protected virtual bool ReuseDriver => false;

        protected RemoteWebDriver PreservedDriver { get; private set; }

        [OneTimeSetUp]
        public void SetUpFixture()
        {
            if (ReuseDriver)
                PreservedDriver = AtataContext.GlobalConfiguration.BuildingContext.DriverFactoryToUse.Create();
        }

        [SetUp]
        public void SetUp()
        {
            AtataContextBuilder contextBuilder = AtataContext.Configure();

            if (ReuseDriver && PreservedDriver != null)
                contextBuilder = contextBuilder.UseDriver(PreservedDriver);

            contextBuilder.Build();
        }

        [TearDown]
        public void TearDown()
        {
            AtataContext.Current?.CleanUp(!ReuseDriver);
        }

        [OneTimeTearDown]
        public void TearDownFixture()
        {
            if (PreservedDriver != null)
            {
                PreservedDriver.Dispose();
                PreservedDriver = null;
            }
        }
    }
}

Then in fixture classes where you need to use the same driver instance by all tests enable ReuseDriver property:

public class SomeTests : UITestFixture
{
    protected override bool ReuseDriver => true;

    [Test]
    public void SomeTest()
    {
        //...
    }
}

Upvotes: 0

Related Questions