Webdriver C# test scripts execution using Grid2

Nunit framework is not support parallel execution of driver test scripts using Grid. If we use MbUuit framework then we can accomplish parallel execution of test scripts against Grid2. For set up MbUnit you can see “MbUnit Framework

Test scripts setup :
  1. Add reference Gallio and  MbUnit ddl file to your Webdriver project from installed Gallio folder.
  2. Add below code in project’s  “AssemblyInfo.cs” file.
using MbUnit.Framework;

[assembly:DegreeOfParallelism(2)]
[assembly:Parallelizable(TestScope.All)]
       3. We need to add below code for MbUnit framework in test scripts.
using MbUnit.Framework;

add [Parallelizable] annotation on test case lavel ass I added in belowtest scripts.
       4. I have created two test one “GridTest1.cs” file for internet explorer and “GridTest2.cs” file for firefox  browser.
         GrridTest1.cs
using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using MbUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.Remote;


namespace SmapleProject
{
    [Parallelizable]
    [TestFixture]
    public class GridTest1
    {
        private IWebDriver driver;
        private StringBuilder verificationErrors;
        private string baseURL;
     
       
        [SetUp]
        public void SetupTest()
        {
            DesiredCapabilities capabilities = new DesiredCapabilities();
            capabilities = DesiredCapabilities.InternetExplorer();        
            capabilities.SetCapability(CapabilityType.BrowserName, "iexplore");
            capabilities.SetCapability(CapabilityType.Platform, new Platform(PlatformType.Windows));
            capabilities.SetCapability(CapabilityType.Version, "9.0");
            
            driver = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"), capabilities);
            baseURL = "https://www.google.co.in/";
            verificationErrors = new StringBuilder();
        }
       
        [TearDown]
        public void TeardownTest()
        {          
            driver.Quit();          
        }
       
        [Test]
        public void GoogleTest()
        {
            driver.Navigate().GoToUrl(baseURL + "/");
            driver.FindElement(By.Id("gbqfq")).Clear();
            driver.FindElement(By.Id("gbqfq")).SendKeys("Testing");
            driver.FindElement(By.Id("gbqfb")).Click();
        }     
    }
}
               
               GridTest2.cs
using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using MbUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.Remote;


namespace SmapleProject
{
    [Parallelizable]
    [TestFixture]
    public class GridTest2
    {
        private IWebDriver driver;
        private StringBuilder verificationErrors;
        private string baseURL;
     
       
        [SetUp]
        public void SetupTest()
        {
            DesiredCapabilities capabilities = new DesiredCapabilities();
            capabilities = DesiredCapabilities.Firefox();
            capabilities.SetCapability(CapabilityType.BrowserName, "firefox");
            capabilities.SetCapability(CapabilityType.Platform, new Platform(PlatformType.Windows));
            capabilities.SetCapability(CapabilityType.Version, "15.0");

            driver = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"), capabilities);
            //driver = new FirefoxDriver();
            baseURL = "https://www.google.co.in/";
            verificationErrors = new StringBuilder();
        }
       
        [TearDown]
        public void TeardownTest()
        {          
            driver.Quit();          
        }
       
        [Test]
        public void GoogleTest()
        {
            driver.Navigate().GoToUrl(baseURL + "/");
            driver.FindElement(By.Id("gbqfq")).Clear();
            driver.FindElement(By.Id("gbqfq")).SendKeys("Testing");
            driver.FindElement(By.Id("gbqfb")).Click();
        }     
    }
}

Launch Grid
       1.  Run below command in on your console to launch Grid hub console:

java -jar selenium-server-standalone-2.21.0.jar -host localhost -port 4444 -role hub

       2. Open two console and run below command to launch and register node with Grid hub.

java -jar selenium-server-standalone-2.21.0.jar -host localhost -port 5555 -role webdriver -hub http://localhost:4444/grid/register -browser browserName=firefox,platform=WINDOWS

java -jar selenium-server-standalone-2.21.0.jar -host localhost -port 5556 -role webdriver -hub http://192.168.1.22:4444/grid/register -browser browserName=iexplore,platform=WINDOWS


Test Execution:
      1. Build webdriver project.
      2. Open Gallio Icarus console add created Webdriver project dll file.

      3. Select both test script and click on run button. You should see both test case executed parallels.

6 comments:

  1. This is a great post for C# users with Selenium. I have searched the web for hours and could not find such a great post.

    I liked the steps given in your blog

    ReplyDelete
    Replies
    1. I am not able to add the attributes [Parallelizable] [assembly:DegreeOfParallelism(2)]
      [assembly:Parallelizable(TestScope.All)]. After addition of these lines it is underlined with red. What is that I am missing. i have added all the references and using statements as per the direction.

      Delete
  2. I executed the steps as given above. Everything is working fine, the only issue is that the tests are executed sequentially and not parallaly. Could you please let me know how can i resolve this..

    ReplyDelete
  3. I am seeing error when i try to run this code locally.
    driver = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"), capabilities);
    This line errors and driver shows up as null

    ReplyDelete
  4. Followed the procedure which you have provided, while executing the scenarios I'm facing some error message like: "Error forwarding the new session cannot find: Capabilities [{browserName=iexplore, version=11.0, platform=WINDOWS}]"
    can you please give me a reply, why exactly it is failing??

    ReplyDelete

Leave your comments, queries, suggestion I will try to provide solution