0

I have a very simple Selenium c# structure as follows:

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

When I run each test unit, each passes. But if I run all test units (just 2 in this case), it fails with the following error:

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

Screenshot of the results when running all tests

How can I fix this so that I can run all tests at in a series?

kenlukas
  • 3,616
  • 9
  • 25
  • 36

1 Answers1

0

When your instancing a ChromeDriver, it creates a socket that is being used for the tests.

Then you are using TearDown that runs after each test, so it basically closes the connection after the TearDown and doesn't open it again for the second test.

So you either:

  • Close ChromeDriver only after all tests are done.
  • Close ChromeDriver and create new instance after each test.

Here's an example with the second solution

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 Bazel
  • 359
  • 2
  • 14