【问题标题】:Running multiple nUnit tests triggers "No connection" error, but running individually works (JetBrains Rider)运行多个 nUnit 测试会触发“无连接”错误,但单独运行可以正常工作 (JetBrains Rider)
【发布时间】:2018-11-07 14:29:41
【问题描述】:

我有一个非常简单的 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:意外错误。 System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: No connection could be because the target machine主动拒绝它127.0.0.1:57535

如何解决这个问题,以便我可以连续运行所有测试?

【问题讨论】:

  • 好像你正在尝试连接到以下地址 127.0.0.1:57535 但它不可用
  • 但是为什么它在第一次测试中起作用呢?然后第二次测试失败了?

标签: c# selenium nunit jetbrains-ide rider


【解决方案1】:

当您实例化 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");
        }
    }
}

【讨论】:

  • 感谢 Shlomi,您的解释很有道理 :) 它有效。
猜你喜欢
  • 2022-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多