Search code examples
c#seleniumselenium-webdrivernunitselenium-grid

Selenium Grid in C# Parallel execution with Nunit


i just made the setup for selenium grid 2. The hub and the nodes are up and running. I'm using Selenium webdriver + C# + Nunit to run my tests, i want now to do the following:

1) Distribute the test cases among different nodes(Parallelism), for example node 1 running test x and node 2 running test y

2) I need to be able to configure the browser and the platform type for each node, what i'm able to do now is that i make one setup function and it uses the same platform and browser for all the nodes, so how can i achieve that by assigning each node's desired capabilities.

The problem is when i run the coming code i find that the tests run simultaneously not parallel.

Snapshot of my code:

using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Safari;
using OpenQA.Selenium.Edge;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.Remote;
using NUnit;
using NUnit.Framework;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;


namespace GridTest
{
    [TestFixture]
    [Parallelizable]
    public class Test_Grid
    {
        IWebDriver driver;

        [SetUp]
        public void Setup()
        {

            /////IE Setup/////////
            var capabilities = new DesiredCapabilities("internet explorer", string.Empty, new Platform(PlatformType.Any));
            capabilities.SetCapability("ignoreProtectedModeSettings", true);

            driver = new RemoteWebDriver(new Uri("http://localhost:4445/wd/hub"),capabilities);

         }

        //Search google test 
        [Test]
        [Parallelizable]
        public void GoogleSearch()
        {
            string homepage = "http://www.google.com";
            //Navigate to the site
            driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));

            driver.Navigate().GoToUrl(homepage);

        }

        [Test]
        [Parallelizable]
        public void BingSearch()
        {
            //var driver = new FirefoxDriver();
            driver.Navigate().GoToUrl("http://www.bing.com");

        }


      [TearDown]
       public void Teardown()
        {

           driver.Quit();
        }
    }
}

Solution

  • Easy one... but you won't like the answer. :-(

    NUnit does not yet support running the test methods in a fixture in parallel. It only runs individual fixtures in parallel.

    So you need to model those things you want run in parallel at the fixture level, using OneTimeSetUp to select the correct browser.

    UPDATE - 27 Sep 2019

    I wrote the above answer over three years ago, when it was true! People, pleaseread the dates on these questions and answers, since they stay here forever unless removed.

    A few months after the answer was posted, NUnit added the ability to run individual test cases (methods) in parallel. Best source of current info about supported features is the NUnit docs, not SO! That's true for most tools.