运行所有测试后,NUnit Selenium Webdriver未关闭

问题描述

我正在尝试打开浏览器,运行多个测试并关闭浏览器。我的问题是,运行所有测试后,浏览器保持打开状态。关闭浏览器的正确方法是什么?如果我在每个测试中都使用[设置]和[TearDown]属性来打开和关闭浏览器,则打开和关闭浏览器,但是如果我使用[OneTimeSetup]和[OneTimeTearDown]属性,则浏览器保持打开状态。

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; }
    }
}

解决方法

在不同的测试中可能但不正确地使用相同的AtataContext。每个UI测试都应创建单独的AtataContext,因为AtataContext还会在测试执行期间收集日志。但是您可以通过将已经创建的RemoteWebDriver实例传递给每个AtataContextBuilder来通过不同的测试来重用同一Web驱动程序实例。

查看Fixture Reusing Driver Atata示例项目的来源。

基本上,您需要增强基本测试夹具类,如下所示:

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;
            }
        }
    }
}

然后在需要通过所有测试使用同一驱动程序实例的灯具类中,启用ReuseDriver属性:

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

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