运行 多个 nUnit 测试触发 "No connection" 错误,但 运行 单独工作 (JetBrains Rider)

Running multiple nUnit tests triggers "No connection" error, but running individually works (JetBrains Rider)

我有一个非常简单的 Selenium c# 结构如下:

using System;
using System.Timers;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

namespace ConsoleApplication2
{
    internal class Program
    {
        IWebDriver driver = new ChromeDriver();
        
        public static void Main(string[] args)
        {
            
        }

        [SetUp]
        public void Initialize()
        {
            driver.Navigate().GoToUrl("https://www.google.pt/");
            Console.WriteLine("INITIALIZE complete");
        }
        
        [Test]
        public void TestGoogleSearch()
        {
            IWebElement element = driver.FindElement(By.Name("q"));
            
            element.SendKeys("ivo cunha");
            Console.WriteLine("IVO complete");
        }
        
        [Test]
        public void TestGoogleSearch2()
        {
            IWebElement element = driver.FindElement(By.Name("q"));
            
            element.SendKeys("adam o'brien");
            Console.WriteLine("ADAM complete");
        }
        
        [TearDown]
        public void CleanUp()
        {
            System.Threading.Thread.Sleep(2500);
            driver.Close();
            driver.Quit();
            driver.Dispose();
            Console.WriteLine("CLEANUP complete");
        }
    }
}

当我运行每个测试单元时,每个单元都通过了。但是如果我 运行 所有测试单元(在这种情况下只有 2 个),它会失败并出现以下错误:

OpenQA.Selenium.WebDriverException : Unexpected error. System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 127.0.0.1:57535

我该如何解决这个问题,以便我可以 运行 在一个系列中进行所有测试?

当您实例化 ChromeDriver 时,它会创建一个用于测试的套接字。

然后您使用的是在每次测试后运行的 TearDown,因此它基本上会在 TearDown 之后关闭连接,并且不会在第二次测试时再次打开它。

所以你要么:

  • 仅在所有测试完成后关闭ChromeDriver
  • 关闭 ChromeDriver 并在每次测试后创建新实例。

这是第二种解决方案的示例

using System;
using System.Timers;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

namespace ConsoleApplication2
{
    internal class Program
    {
        IWebDriver driver = null;

        public static void Main(string[] args)
        {

        }

        [SetUp]
        public void Initialize()
        {
            driver = new ChromeDriver();
            driver.Navigate().GoToUrl("https://www.google.pt/");
            Console.WriteLine("INITIALIZE complete");
        }

        [Test]
        public void TestGoogleSearch()
        {
            IWebElement element = driver.FindElement(By.Name("q"));

            element.SendKeys("ivo cunha");
            Console.WriteLine("IVO complete");
        }

        [Test]
        public void TestGoogleSearch2()
        {
            IWebElement element = driver.FindElement(By.Name("q"));

            element.SendKeys("adam o'brien");
            Console.WriteLine("ADAM complete");
        }

        [TearDown]
        public void CleanUp()
        {
            System.Threading.Thread.Sleep(2500);
            driver.Close();
            driver.Quit();
            driver.Dispose();
            Console.WriteLine("CLEANUP complete");
        }
    }
}