Selenium C# Tutorial: Chrome Options concepts to simplifying Web Testing

Updated: Jan 18

The Chromeoptions Class is one of the main concepts in Selenium WebDriver for handling and manipulating various properties of the Chrome driver. The chrome options class is mainly used in conjunction with Desired Capabilities for customizing Chrome driver sessions such as open chrome in maximized mode, Disable pop-ups, etc.

The class is inherited from "OpenQA.Selenium.Chrome.ChromeOptions" and is available for WebDriver (in WebDriver.dll) Version: 3.1.0 and higher. Also, it is Used with ChromeDriver.exe v17.0.963.0 and higher. Please check its documentation for more information about this class and its capabilities.


This article documents the main ChromeDriver supported capabilities and how to use them:


Example #1: Open Chrome browser in maximized mode

[SetUp]
public void Setup()
{
//Create an instance of the ChromeOptions class 
ChromeOptions chromeOptions = new ChromeOptions();
            
//Set the argument 
chromeOptions.AddArguments("--start-maximized");

//Pass the ChromeOptions object into the ChromeDriver constructor
Driver = new ChromeDriver(chromeOptions);            
}

Example #2: Opens Chrome in incognito mode

[SetUp]
public void Setup()
{
//Create an instance of the ChromeOptions class 
ChromeOptions chromeOptions = new ChromeOptions();
            
//Set Chrome to work with incognito mode
chromeOptions.AddArguments("incognito");

//Pass the ChromeOptions object into the ChromeDriver constructor
Driver = new ChromeDriver(chromeOptions);   

Driver.Navigate().GoToUrl("http://agilequalitymadeeasy.com/");         
} 

Output:


Example #3: Open Chrome browser Headless mode

Headless mode is a functionality that allows the execution of a full version of the latest Chrome browser while controlling it programmatically.

[SetUp]
public void Setup()
{
//Create an instance of the ChromeOptions class 
ChromeOptions chromeOptions = new ChromeOptions();
            
//Set Chrome to work with headless mode
chromeOptions.AddArguments("headless");

//Pass the ChromeOptions object into the ChromeDriver constructor
Driver = new ChromeDriver(chromeOptions);   

Driver.Navigate().GoToUrl("http://agilequalitymadeeasy.com/");         
} 


Example #4: Open Chrome browser without extensions

in some scenarios, you will need to run your code on a chrome browser with no "Enabled" extensions. Here is how we are doing it:


With Argument Disabled:

[SetUp]
public void Setup()
{
//Create an instance of the ChromeOptions class 
ChromeOptions chromeOptions = new ChromeOptions();

//Opens Chrome without enabled extensions
//chromeOptions.AddArguments("disable-extensions");

//Pass the ChromeOptions object into the ChromeDriver constructor
Driver = new ChromeDriver(chromeOptions);
            
Driver.Navigate().GoToUrl("http://agilequalitymadeeasy.com/");
}

With Argument Enabled:

[SetUp]
public void Setup()
{
//Create an instance of the ChromeOptions class 
ChromeOptions chromeOptions = new ChromeOptions();

//Opens Chrome without enabled extensions
chromeOptions.AddArguments("disable-extensions");

//Pass the ChromeOptions object into the ChromeDriver constructor
Driver = new ChromeDriver(chromeOptions);
            
Driver.Navigate().GoToUrl("http://agilequalitymadeeasy.com/");
}


Example #5: Open Chrome browser using custom profile (Aka: user data directory)

When we open a new ChromeDriver session, it is (By Default) creating a new temporary profile for each session which is not recommended. It creates a conflict between selenium test execution and the machine chrome browser as they try to access the same profile. You may want to use specific options or preferences for different automation tasks. To do it, we can use the 'chrome.prefs' capability to specify preferences that will be applied after Chrome starts.


Chrome keeps the default profile locally in a different location in each system.

In Windows:

C:\Users\username\AppData\Local\Google\Chrome\User Data\default

Example #6: Ignore Certificate errors

SSL (Secure Sockets Layer) is a standard security protocol for establishing a secure connection between the server and the client, which is a browser. The below code will help to accept all the SSL certificates in chrome, and the user will not receive any SSL certificate-related error using this code.

[SetUp]
public void Setup()
{
//Create an instance of the ChromeOptions class 
ChromeOptions chromeOptions = new ChromeOptions();

//Opens Chrome and accept all the SSL certificates        
chromeOptions.AddArguments("ignore-certificate-errors");

//Pass the ChromeOptions object into the ChromeDriver constructor
Driver = new ChromeDriver(chromeOptions);
            
Driver.Navigate().GoToUrl("http://agilequalitymadeeasy.com/");
}


Example #7: Disables pop-ups displayed on Chrome browser

[SetUp]
public void Setup()
{
//Create an instance of the ChromeOptions class 
ChromeOptions chromeOptions = new ChromeOptions();

//Opens Chrome and disabling popups       
chromeOptions.AddArguments("disable-popup-blocking");

//Pass the ChromeOptions object into the ChromeDriver constructor
Driver = new ChromeDriver(chromeOptions);
            
Driver.Navigate().GoToUrl("http://agilequalitymadeeasy.com/");
}

Example #8: Set an HTTP proxy Chrome

[SetUp]
public void Setup()
 {      
 // Set a HTTP proxy Chrome
ChromeOptions options = new ChromeOptions();

//Create a new proxy object
var proxy = new Proxy();
            
proxy.IsAutoDetect = false;  
            
proxy.HttpProxy = "127.0.0.1:8888"; // Or SSL: localhost:8888

//Set the proxy to the Chrome options
options.Proxy = proxy;

//Pass the ChromeOptions object into the ChromeDriver constructor
IWebDriver driver = new ChromeDriver(options);

Driver.Navigate().GoToUrl("http://agilequalitymadeeasy.com/");
}



Example #9: Execute Selenium Chrome WebDriver in silent mode

[SetUp]
public void Setup()
{
//Create an instance of the ChromeOptions class 
ChromeOptions chromeOptions = new ChromeOptions();

//Suppresses ChromeDriver's diagnostics outputs
ChromeDriverService service = ChromeDriverService.CreateDefaultService();
service.SuppressInitialDiagnosticInformation = true;
         
//Pass the ChromeOptions object into the ChromeDriver constrctor
IWebDriver Driver = new ChromeDriver(service,chromeOptions);

Driver.Navigate().GoToUrl("http://agilequalitymadeeasy.com/");
}

Or

[SetUp]
public void Setup()
{
//Create an instance of the ChromeOptions class 
ChromeOptions chromeOptions = new ChromeOptions();

chromeOptions.AddArgument("--log-level=3");

//Pass the ChromeOptions object into the ChromeDriver constrctor
IWebDriver Driver = new ChromeDriver(chromeOptions);

Driver.Navigate().GoToUrl("http://agilequalitymadeeasy.com/");
}

Or

[SetUp]
public void Setup()
{
//Create an instance of the ChromeOptions class 
ChromeOptions chromeOptions = new ChromeOptions();

chromeOptions.AddArgument("--silent");

//Pass the ChromeOptions object into the ChromeDriver constrctor
IWebDriver Driver = new ChromeDriver(chromeOptions);

Driver.Navigate().GoToUrl("http://agilequalitymadeeasy.com/");
}


3,252 views0 comments