Browser Options Class in Selenium webdriver

ChromeOptions class extends MutableCapabilities Class and MutableCapabilities extends Object Class, ChromeOptions also implements Serializable, Capabilities.

We can use ChromeOptions class to manage options specific to ChromeDriver.

Similar to ChromeOptions, Firefox also provides FirefoxOptions class where you can set all the options specific to the execution of the session.

Remaining browsers also provides this feature.

Using Browser Options class we can set options like make the browser headless, load extensions, set binaries and set proxy so on.

Methods present in the Browser Options class

All the methods present in the ChromeOptions/ FirefoxOptions class are non-static methods, below are the methods present in the ChromeOptions/ FirefoxOptions.

1. setBinary
2. setAcceptInsecureCerts
3. setProxy
4. addExtensions
5. setHeadless
6. addArguments
7. setUnhandledPromptBehaviour
8. addEncodedExtensions
9. getPlatform
10. setExperimentalOption
11. getCapability
12. setPageLoadStrategy
13. asMap
14. merge

setBinary in ChromeOptions/FirefoxOptions

We cannot expect systems to have the browser exe file in right path, sometimes due to permission issues, People will install the browsers in different path.

In these case the Selenium searches for the browser exe in default path as the browser is not present in the default path, selenium throws an exception.

This happens not only for chrome and firefox but also happens to other browsers.

To overcome this selenium provides a method called setBinary in ChromeOptions/FirefoxOptions class, using this method we can set the browser exe path.


									public static void main(String[] args) {
										System.setProperty("webdriver.chrome.driver", "D:\\PATH\\chromedriver.exe");
										ChromeOptions co = new ChromeOptions();
										// set the binary path
										co.setBinary("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");
										//open the browser
										WebDriver driver = new ChromeDriver(co);
										driver.get("https://google.com");
									}			
									

setBinary method is overloaded method, in above example we have passed path as String but we can also send file as parameter.

Below example shows how to set the exe binary path using FirefoxOptions.


										public static void main(String[] args) {
											System.setProperty("webdriver.gecko.driver", "D:\\PATH\\geckodriver.exe");
											FirefoxOptions fo = new FirefoxOptions();
											fo.setBinary(new FirefoxBinary(new File("C:\\Program Files\\Mozilla Firefox\\firefox.exe")));
											//open the browser
											WebDriver driver = new FirefoxDriver();
											driver.get("https://bing.com");
										}			
									

This method is used when ever tester want to perform the version testing in selenium, In version testing we may need to have more than one version of the a browser. In such cases we will install the additional browser in custom path.

setAcceptInsecureCerts in selenium

SSL (Secure Socket Layer) Certificate is nothing but encrypted way of transmitting data from the website to database, database to website.

If a site enables its SSL certificate we can see https protocol instead of http protocol. Few companies will have their own certificates and few companies will buy from the SSL providing companies.

For example Google, Fb, Amazon have their own certificate but in case of this(chercher.tech) website has received SSL certificate from my hosting company (it would be fun, so try to open this site like https://chercher.tech)

Sometimes some sites will open only when you open them in https mode, there are few more site whatever you open they will open only with https protocol.

Some browser will not allow you to open the https version of the website, so in this case we have to bypass the authentication certificate or SSL certificate to test the website.

Not useful Tip : We can set these kind of settings in a file called .htaccess, because that is how I removed the www from my site name, this site opens without www even you forcibly try open this site with www.

setAcceptInsecureCerts method present in the ChromeOptions/FirefoxOptions/InternetExplorerOptions class will help us to bypass the SSL certificate.

setAcceptInsecureCerts method accepts boolean parameter, if true driver accepts the certificate and proceeds further, if false is passed and site has SSL then driver will stop the execution as it cannot procees further due to SSL.


									public static void main(String[] args) {
										System.setProperty("webdriver.gecko.driver", "D:\\PATH\\geckodriver.exe");
										FirefoxOptions fo = new FirefoxOptions();
										// accept the certificate
										fo.setAcceptInsecureCerts(true);
										//open the browser
										WebDriver driver = new FirefoxDriver();
										driver.get("https://www.cacert.org/");
									}			
									

The sites having https may or may not throw the certificate warning, if a site throws a certificate warning(below image) then only this method serves its purpose, otherwise this method doesnot have any effect. sslcertificate-selenium-browseroptions

Handle PROXY in Selenium Webdriver

Before starting this section i want to inform you guys that all the proxies used in this are my imagination, obviously these will not help you to bypass any server but it will help you to learn how actually proxy works in selenium.

A proxy is nothing but a mask over your computer address, every company will have proxy atleast for installing softwares.

Basically proxy is nothing but showing someone else's computer as your computer through masking.

By connecting through one of these proxy servers, your computer sends your requests to the proxy server which then processes your request and returns what you were wanting.

Proxies are used for a number of reasons such as to filter web content, to go around restrictions such as parental blocks, to screen downloads and uploads and to provide anonymity when surfing the internet.

FYI : I use proxy only to use facebook.., nah I am too old for facebook

Steps to Handle Proxy in selenium : If you ever visited Proxy section of the Browser you might have noticed that there are two types of proxy settings.
1. Automatic configuration.
2. Manual Configuration
You might have read using Capabilities class to set the Proxy but with updated versions of selenium will support Browser Options class.

proxy-settings-selenium

Automatic proxy in selenium :

We can set the automatic proxy using ChromeOptions/FirefoxOptions/InternetExplorerOptions class, for setting proxy we have to configure Proxy in selenium webdriver.

setProxyAutoconfigUrl is the method we have to call from the Object of Proxy class to set the automatic proxy.

Donot close the browser in your code as we want to ensure whether our code worked or not. Donot forget to pass the Browser options object to Browser Driver.


										public static void main(String[] args) {
											System.setProperty("webdriver.gecko.driver", "D:\\PATH\\geckodriver.exe");
											FirefoxOptions fo = new FirefoxOptions();
											// Create object Proxy class
											Proxy prox = new Proxy();
											prox.setProxyAutoconfigUrl("https://proxy.chercher.tech");
											// register the proxy with options class
											fo.setProxy(prox);
											// create object to firefx driver
											WebDriver driver = new FirefoxDriver(fo);
										}			
									

Open the proxy setting in the browser that you opened with automation. automatic-proxy-config-selenium-browser-options

Manual Proxy Configuration in Selenium :

In below image i have highlighted what are the required for the manual proxy, manual-proxy-selenium-webdriver-browser-options
We have to set :
1. Http Proxy
2. SSL Proxy
3. FTP Proxy
4. Socks Proxy

Important note : To Socks Proxy, we must include the Socks version, there are two versions available 1. SOCKs v4, 2. SOCKS v5. We can pass integer number 4 or 5 to mention v4 and v5.

If we didnot set the version then selenium throws below exception, as well as you need let the system know that You are setting manual Proxy.

Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: InvalidArgumentError: Expected [object Undefined] undefined to be an integer

Let see the program, how to set manual proxy in selenium


										public static void main(String[] args) {
											System.setProperty("webdriver.gecko.driver", "D:\\PATH\\geckodriver.exe");
											FirefoxOptions fo = new FirefoxOptions();
											String proxyAddress = "dummyhost:8080";
											// Create object Proxy class
											Proxy prox = new Proxy();
											// setting manual proxy
											prox.setProxyType(ProxyType.MANUAL);
											prox.setHttpProxy(proxyAddress);
											prox.setFtpProxy(proxyAddress);
											prox.setSslProxy(proxyAddress);
											// set the SOCKS version
											prox.setSocksVersion(4);
											prox.setSocksProxy(proxyAddress);
											// register the proxy with options class
											fo.setProxy(prox);
											// create object to firefx driver, register options class object
											WebDriver driver = new FirefoxDriver(fo);
										}			
									

Open the proxy setting in the browser that you opened with automation. manual-proxy-configuaration-selenium

No Proxy Configuration in Selenium :

We can also set No Proxy option in the browser using selenium, to set No proxy we have to use ProxyType as Direct. Normally we use No Proxy to bypass all the proxies set.


									public static void main(String[] args) {
										System.setProperty("webdriver.gecko.driver", "D:\\PATH\\geckodriver.exe");
										FirefoxOptions fo = new FirefoxOptions();
										// Create object Proxy class
										Proxy prox = new Proxy();
										prox.setProxyType(ProxyType.DIRECT);
										// register the proxy with options class
										fo.setProxy(prox);
										// create object to firefx driver, register options class
										WebDriver driver = new FirefoxDriver(fo);
									}			
									

Output of Direct Proxy program with selenium noproxy-direct-proxy-selenium

Auto Detect Proxy Configuration in Selenium :

By using ProxyType Enum we can set the Auto detect proxy in browser


									public static void main(String[] args) {
										System.setProperty("webdriver.gecko.driver", "D:\\PATH\\geckodriver.exe");
										FirefoxOptions fo = new FirefoxOptions();
										// Create object Proxy class
										Proxy prox = new Proxy();
										prox.setProxyType(ProxyType.AUTODETECT);
										// register the proxy with options class
										fo.setProxy(prox);
										// create object to firefx driver, register options class
										WebDriver driver = new FirefoxDriver(fo);
									}			
									

Output of Auto proxy program auto-proxy-browser-options-selenium

System Proxy Configuration in Selenium :

System proxy is also similar to Auto Proxy, setting system proxy reads the proxy value from the system.


									public static void main(String[] args) {
										System.setProperty("webdriver.gecko.driver", "D:\\PATH\\geckodriver.exe");
										FirefoxOptions fo = new FirefoxOptions();
										// Create object Proxy class
										Proxy prox = new Proxy();
										prox.setProxyType(ProxyType.SYSTEM);
										// register the proxy with options class
										fo.setProxy(prox);
										// create object to firefx driver, register options class
										WebDriver driver = new FirefoxDriver(fo);
									}			
									

Getter methods in Proxy :

There are few methods present in the proxy class which will retrieve the details about the proxy, which come handy when you want to instruct the browser to perform some tasks based on the details.

1. getFtpProxy : : This method retrieves the ftp proxy of the browser

2. getHttpProxy : fetches the http proxy

3. getNoProxy : gets the values of no proxy

4. getProxyAutoconfigUrl : this methods retrieves the url of the automatic proxy

5. getProxyType : fetches the proxy type

6. getSocksPassword : fetches the password of the SOCKS proxy

7. getSocksProxy : fetches the proxy of the SOCKS

8. getSocksUsername : fetches the username of the SOCKS proxy

9. getSocksVersion : fetches Socks version, the value could be 4 or 5

10. getSslProxy : fetches the SSL proxy

11. isAutodetect : This methods return true or false, true if the proxy is Auto Detect/ Direct

I hope you clear about how to handle the proxy using ChromeOptions/FirefoxOptions/InternetExplorerOptions. Now let's get back to methods in Browser Options class

Make browser Headless

setHeadless method in ChromeOptions/FirefoxOptions helps the user to make the browser as headless, We have dedicated full chapter for Headless browser


											public static void main(String[] args) {
												System.setProperty("webdriver.gecko.driver", "D:\\PATH\\geckodriver.exe");
												FirefoxOptions fo = new FirefoxOptions();
												// Create object Proxy class
												fo.setHeadless(true);
												// create object to firefx driver, register options class
												WebDriver driver = new FirefoxDriver(fo);
											}			
										

output of headless program headless-argument-selenium

How to handle two Browser Windows in Selenium

addArguments in selenium

Using addArguments method we can achieve result of the all the methods in ChromeOptions/FirefoxOptions/InternetExplorerOptions class.

For Example, we can make the browser as headless, we can add proxy, we can add extensions, so on..

chrome-disable-infobar-selenium
In below example shows how to remove the information bar while executing the automation in chrome.


										public static void main(String[] args) {
											System.setProperty("webdriver.chrome.driver", "D:\\PATH\\chromedriver.exe");
											ChromeOptions fo = new ChromeOptions();
											// disable the info bar
											fo.addArguments("--disable-infobars");
											// create object to firefx driver, register options class
											WebDriver driver = new ChromeDriver(fo);
											driver.get("https://bing.com");
										}			
										

Output of the add arguments disabled-infobar-selenium

Read & Write Barcode using Selenium Webdriver

Handle Browser notification

Lot of websites have started sending notifications these days. You get these notifications on desktop or device even when the concerned web page is not open in your browser.

These are also called as Web push notifications. These notifications looks like below in chrome browser: We have to use addArguments method to handle these kind of browser notifications,


										public static void main(String[] args) {
											System.setProperty("webdriver.chrome.driver", "D:\\PATH\\chromedriver.exe");
											ChromeOptions fo = new ChromeOptions();
											fo.addArguments("--disable-notifications");
											WebDriver driver = new ChromeDriver(fo);
											driver.get("https://drupal-stage-web.weather.com/en-IN");
										}			
									

setUnhandledPromptBehaviour in Selenium

setUnhandledPromptBehaviour methods helps the user to handle the un handled alert. Whenever there is an alert this method perform operation on that based on your input to this method.

Note: With few version this method may not work.


										public static void main(String[] args) {
											System.setProperty("webdriver.chrome.driver", "D:\\PATH\\chromedriver.exe");
											ChromeOptions fo = new ChromeOptions();
											// set the behaviuor of the alert
											fo.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.ACCEPT);
											// create object to firefox driver, register options class
											WebDriver driver = new ChromeDriver(fo);
											driver.get("https://chercher.tech/java/practice-pop-ups-selenium-webdriver");
											driver.findElement(By.name("alert")).click();
											driver.findElement(By.name("prompt")).click();
										}			
									

Hidden Locators in Selenium Webdriver

Page Load Strategy

When Page Loading takes too much time and you need to stop downloading additional subresources (images, css, js etc) you can change the pageLoadStrategy through the webdriver.

We want to reduce the number of calls to various resources like Images, JS, so on.

Page Load Strategy helps the user to load the webpage faster, and pageLoadStrategy support following values.

1. normal This strategy causes Selenium to wait for the full page loading (html content and subresources downloaded and parsed).

2. eager This strategy causes Selenium to wait for the DOMContentLoaded event (html content downloaded and parsed only).

3. none his strategy causes Selenium to return immediately after the initial page content is fully received (html content downloaded).

By default, when Selenium loads a page with normal pageLoadStrategy.


									public static void main(String[] args) {
										System.setProperty("webdriver.chrome.driver", "D:\\PATH\\chromedriver.exe");
										ChromeOptions fo = new ChromeOptions();
										// set the page load strategy
										fo.setPageLoadStrategy(PageLoadStrategy.EAGER);
										// create object to chrome driver, register options class
										WebDriver driver = new ChromeDriver(fo);
										driver.get("https://chercher.tech");
									}			
									

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions
  • Reply
  • Recent Addition

    new tutorial Registrations for Selenium Online Training is Over.

    Below are the training details:
    Meeting link : https://zoom.us/j/737840591
    Starting Time : 9:00PM 18th DEC 2018
     
    Join My Facebook Group
    Join Group