Selenium Grid C# Webdriver Examples

C# .NET Webdriver bindings are supported by Gridlastic, run your tests in the cloud with real browsers.

Examples for Selenium version 3.14.0+ (Desired Capabilities is deprecated in Selenium webdriver 3.14.0 for C#)

Selenium C# Chrome and Internet Explorer Example

// Download the C# webdriver at https://www.nuget.org/packages/Selenium.WebDriver

using NUnit.Framework;
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.IE;
using System.Threading;
using System.Drawing;

namespace Test
{
    public class Tests
    {
   
        [Test]
        public void Test1()
        {
            RemoteWebDriver driver;
	  
	  
      //CHROME and IE            
      ChromeOptions Options = new ChromeOptions();
      //InternetExplorerOptions Options = new InternetExplorerOptions();
      Options.PlatformName = "windows";
      Options.AddAdditionalCapability("platform", "WIN10", true); // Supported values: "VISTA" (Windows 7), "WIN8" (Windows 8), "WIN8_1" (windows 8.1), "WIN10" (Windows 10), "LINUX" (Linux)
      Options.AddAdditionalCapability("version", "latest", true); // you can specify version=latest or the version number like version="90". For IE you must always specify the version number.
      Options.AddAdditionalCapability("gridlasticUser", USERNAME, true);
      Options.AddAdditionalCapability("gridlasticKey", ACCESS_KEY, true);
      Options.AddAdditionalCapability("video", "True", true);
			
			
      driver = new RemoteWebDriver(
        new Uri("https://HUB_SUBDOMAIN.gridlastic.com/wd/hub/"), Options.ToCapabilities(), TimeSpan.FromSeconds(600));// NOTE: connection timeout of 600 seconds or more required for time to launch grid nodes if non are available.
      try
            {
                driver.Manage().Window.Maximize(); // WINDOWS, DO NOT WORK FOR LINUX/firefox. If Linux/firefox set window size, max 1920x1080, like driver.Manage().Window.Size = new Size(1920, 1080);
             // driver.Manage().Window.Size = new Size(1920, 1080); // LINUX/firefox			 
				driver.Navigate().GoToUrl("https://www.google.com/ncr");
				IWebElement query = driver.FindElement(By.Name("q"));
				query.SendKeys("webdriver");
				query.Submit();
			}
            finally
            {
			Console.WriteLine("Video: " + VIDEO_URL + driver.SessionId);
			driver.Quit();
			}
    }
  }
}

Selenium C# Firefox Example

// Download the C# webdriver at https://www.nuget.org/packages/Selenium.WebDriver

using NUnit.Framework;
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.IE;
using System.Threading;
using System.Drawing;

namespace Test
{
    public class Tests
    {
   
        [Test]
        public void Test1()
        {
            RemoteWebDriver driver;
	  
	  
      //FIREFOX example testing with version 88 on Linux         
      FirefoxOptions Options = new FirefoxOptions();
      // Options.BrowserExecutableLocation = "C:\\Program Files (x86)\\Mozilla Firefox\\firefox88\\firefox.exe"; // WINDOWS
      Options.BrowserExecutableLocation = "/home/ubuntu/Downloads/firefox88/firefox"; // LINUX
      // Options.PlatformName = "windows"; // exclude for linux
      Options.AddAdditionalCapability("platform", "LINUX", true); // Supported values: "VISTA" (Windows 7), "WIN8" (Windows 8), "WIN8_1" (windows 8.1), "WIN10" (Windows 10), "LINUX" (Linux)
      Options.AddAdditionalCapability("version", "88", true); // you can specify version=latest or the version number like version="88". For IE you must always specify the version number.
      Options.AddAdditionalCapability("gridlasticUser", USERNAME, true);
      Options.AddAdditionalCapability("gridlasticKey", ACCESS_KEY, true);
      Options.AddAdditionalCapability("video", "True", true);
			
			
      driver = new RemoteWebDriver(
        new Uri("https://HUB_SUBDOMAIN.gridlastic.com/wd/hub/"), Options.ToCapabilities(), TimeSpan.FromSeconds(600));// NOTE: connection timeout of 600 seconds or more required for time to launch grid nodes if non are available.
      try
            {
             // driver.Manage().Window.Maximize(); // WINDOWS, DO NOT WORK FOR LINUX/firefox. If Linux/firefox set window size, max 1920x1080, like driver.Manage().Window.Size = new Size(1920, 1080);
                driver.Manage().Window.Size = new Size(1920, 1080); // LINUX/firefox			 
				driver.Navigate().GoToUrl("https://www.google.com/ncr");
				IWebElement query = driver.FindElement(By.Name("q"));
				query.SendKeys("webdriver");
				query.Submit();
			}
            finally
            {
			Console.WriteLine("Video: " + VIDEO_URL + driver.SessionId);
			driver.Quit();
			}
    }
  }
}


Example for Selenium version 3.13.0 and below

// Download the C# webdriver at https://www.nuget.org/packages/Selenium.WebDriver

using NUnit.Framework;
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.IE;
using System.Threading;
using System.Drawing;

namespace Test
{
    public class Tests
    {
   
        [Test]
        public void Test1()
        {
            RemoteWebDriver driver;
	  
	  
      DesiredCapabilities capability = DesiredCapabilities.Chrome();
      capability.SetCapability("platform", "WIN10"); // Supported values: "VISTA" (Windows 7), "WIN8" (Windows 8), "WIN8_1" (windows 8.1), "WIN10" (Windows 10), "LINUX" (Linux)
      //capability.SetCapability("platformName", "windows"); // (selenium version 3.5.3+)
      capability.SetCapability("version", "latest"); // you can specify version=latest or the version number like version="88". For IE you must always specify the version number.
      capability.SetCapability("gridlasticUser", USERNAME);
      capability.SetCapability("gridlasticKey", ACCESS_KEY);
      capability.SetCapability("video", "True");
      //capability.SetCapability("firefox_binary", @"C:\Program Files (x86)\Mozilla Firefox\firefox70\firefox.exe"); // For firefox testing specify binary location (selenium version 3.5.3+)
      
	  
	  driver = new RemoteWebDriver(
        new Uri("https://HUB_SUBDOMAIN.gridlastic.com/wd/hub/"), capability, TimeSpan.FromSeconds(600));// NOTE: connection timeout of 600 seconds or more required for time to launch grid nodes if non are available.
      try
            {
                driver.Manage().Window.Maximize(); // WINDOWS, DO NOT WORK FOR LINUX/firefox. If Linux/firefox set window size, max 1920x1080, like driver.Manage().Window.Size = new Size(1920, 1080);
             // driver.Manage().Window.Size = new Size(1920, 1080); // LINUX/firefox			 
				driver.Navigate().GoToUrl("https://www.google.com/ncr");
				IWebElement query = driver.FindElement(By.Name("q"));
				query.SendKeys("webdriver");
				query.Submit();
			}
            finally
            {
			Console.WriteLine("Video: " + VIDEO_URL + driver.SessionId);
			driver.Quit();
			}
    }
  }
}


The Gridlastic credentials and the video url used in this code example is displayed after launching your Gridlastic selenium grid. See documention Selenium Grid Configuration Parameters where to find these credentials and replace with your own.

Get a free account and launch your Gridlastic selenium grid, then run this code locally.

NOTE: Gridlastic auto scaling requires all 3 test environment parameters platform, browser and browser version to be specified in each request in order to launch test nodes to fulfill test demand. Video recording is optional. See test environments for capabilities options.
It is important to ensure that "driver.Quit()" is always called for proper test execution and creation of video recordings of failed tests.