Selenium Grid C# Examples

These C# examples are using selenium version 4 on a Gridlastic selenium grid.

Selenium C# Chrome Example

using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Chrome;


namespace test_Nunit
{
    public class Tests
    {


        [Test]
        public void Test1()
        {

            ChromeOptions chromeOptions = new ChromeOptions();
            chromeOptions.PlatformName = "win10"; // linux or win10
            chromeOptions.BrowserVersion = "latest";


            // Enable video recording
            var gridlasticOptions = new Dictionary();
            gridlasticOptions.Add("video", "true");
            gridlasticOptions.Add("gridlasticUser", USERNAME);
            gridlasticOptions.Add("gridlasticKey", ACCESS_KEY);
            chromeOptions.AddAdditionalOption("gridlastic:options", gridlasticOptions);

            RemoteWebDriver driver = new RemoteWebDriver(new Uri("https://YOUR_HUB_SUBDOMAIN.gridlastic.com/wd/hub/"), chromeOptions.ToCapabilities(), TimeSpan.FromSeconds(600));

            try
            {
                driver.Manage().Window.Maximize();
                driver.Navigate().GoToUrl("https://www.gridlastic.com/?demo");
            }
            finally
            {
                Console.WriteLine("Video: " + VIDEO_URL + driver.SessionId);
                driver.Quit();
            }
        }
    }
}


Selenium C# Chrome DevTools Example (supported starting 4.16.1)

using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.DevTools;
using OpenQA.Selenium.DevTools.V119.Emulation;


namespace selenium_4_CDP_example
{
    public class Tests
    {


        [Test]
        public async Task Test1Async()
        {

            ChromeOptions chromeOptions = new ChromeOptions();
            chromeOptions.PlatformName = "win10"; // linux or win10
            chromeOptions.BrowserVersion = "latest";

          //  chromeOptions.UseWebSocketUrl = true; // enable BiDi

    
             // Enable video recording
            var gridlasticOptions = new Dictionary();
            gridlasticOptions.Add("video", "true");
            gridlasticOptions.Add("gridlasticUser", USERNAME);
            gridlasticOptions.Add("gridlasticKey", ACCESS_KEY);
            chromeOptions.AddAdditionalOption("gridlastic:options", gridlasticOptions);

            RemoteWebDriver driver = new RemoteWebDriver(new Uri("https://YOUR_HUB_SUBDOMAIN.gridlastic.com/wd/hub/"), chromeOptions.ToCapabilities(), TimeSpan.FromSeconds(600));

             try
            {

                DevToolsSession devToolsSession = driver.GetDevToolsSession();

                if (devToolsSession != null)
                {

                     var geoLocationOverrideCommandSettings = new SetGeolocationOverrideCommandSettings();

                    // change to London
                    geoLocationOverrideCommandSettings.Latitude = 51.507351;
                    geoLocationOverrideCommandSettings.Longitude = -0.127758;
                    geoLocationOverrideCommandSettings.Accuracy = 1;

                    await devToolsSession
                      .GetVersionSpecificDomains<OpenQA.Selenium.DevTools.V119.DevToolsSessionDomains>()
                      .Emulation
                      .SetGeolocationOverride(geoLocationOverrideCommandSettings);


                    driver.Url = "https://my-location.org/";
                    await Task.Delay(5000);

                }

            }
            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.