Options Classes in Selenium Java

In this section, we are Going to Learn to Execute Selenium Script with Chrome and Firefox Option classes.

Options Classes in Selenium Web Driver?

Options Classes are Used for Maniplulaing the Properties of Respective Web Driver, IN our case we are using Chrome and Firefox Web Driver To Demonstrate the use case of Option classes.

Prerequisites

• Download and Install Java JDK
• Download and Install Eclipse IDE
• Download Selenium Jar and Web Driver

Once the download is done you can follow some simple steps to install this on your machine or follow the tutorial to Set Up Environment For Selenium.

In This Example, let’s Open Chrome Capture the Browser Name and Version Also disable the info bar.

Test Scenario:
  • Open the browser and Disable the info bar.
  • Load the URL “www.google.com”.
  • and Capture Title and Browser Name and Version.

Create Selenium Script in Eclipse IDE
• Open Eclipse IDE and click on file >> new >> class
• Enter Class Name “Option_class” and Click Finish

Code:-
package project1;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.chromium.ChromiumDriver;

public class Option_class {
    public static void main(String[]args) 
    
{
System.setProperty("webdriver.chrome.driver", "E:/CodeBun/project1/chromedriver.exe");//initialize Chrome Web Driver
ChromeOptions options = new ChromeOptions();// Create Instance for Chrome Option class
options.setExperimentalOption("excludeSwitches", new String[]{"enable-automation"});// it is option method to disable the infobar when chrome browser opens
WebDriver driverChrome = new ChromeDriver(options);// Create Instance for Chrome Web Driver  
driverChrome.get("https://www.google.com");//Url Request Form Chrome Driver
String title = driverChrome.getTitle();//it capture the title and store it into title variable
System.out.println("Page Title: " +title);//it prints the title varible
System.out.println("Browser Name is : "+((ChromiumDriver) driverChrome).getCapabilities().getBrowserName());//it is option method to Print the Browser Name
System.out.println("Browser Version is : "+((ChromiumDriver)
driverChrome).getCapabilities().getBrowserVersion());//it is an option method to Print the Browser version
}
}

Code Explanation

System.setProperty("webdriver.chrome.driver","E:/CodeBun/project1/chromedriver.exe");

In the Above Example, System.setproperty()the method is Used to Initialize with Chrome Web driver.

ChromeOptions options = new ChromeOptions();

ChromeOptions options = new ChromeOptions()is Used to Create an instance for the Chrome Option class.

options.setExperimentalOption("excludeSwitches", new String[]{"enable-automation"});

options.setExperimentalOption("excludeSwitches", new String[]{"enable-automation"})is Used for Disabling the Info bar which stated that “Chrome is being Controlled by automated test Software”

WebDriver driverChrome = new ChromeDriver(options);

WebDriver driverChrome = new ChromeDriver(options)is Used To Create an instance of Chrome Web Driver and Options passed as an argument in the example.

driverChrome.get("https://www.google.com");

driverChrome.get("https://www.google.com")is used for Requesting Specific URL, in the Above Statement, it is Requested by Chrome Driver.

String title = driverChrome.getTitle();

System.out.println("Page Title: " +title);

it will capture the title from the web application store into a string, which then will be printed.

System.out.println("Browser Name is : "+((ChromiumDriver) driverChrome).getCapabilities().getBrowserName());

DriverChrome.getCapabilities().getBrowserName()is Used For Capturing Browser Name and System.out.println()

is used for Printing Browser Name.

System.out.println("Browser Version is :"+((ChromiumDriver)driverChrome).getCapabilities().getBrowserVersion());

DriverChrome.getCapabilities().getBrowserVersion()is Used For Capturing Browser Version and System.out.println()is used for Printing Browser Version.

Output

Options class in Selenium using Firefox

In This Example, let’s Open Firefox in Headless mode and Capture the Browser Name and Version.

Test Scenario:
  • Open the browser in Headless Mode.
  • Load the URL “www.google.com”.
  • and Capture Title and Browser Name and Version.
Code:-
package project1;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;

public class Option_class {
    public static void main(String[]args)// Main Java Class
    
{
System.setProperty("webdriver.gecko.driver", "E:/CodeBun/Project3/geckodriver.exe");//initialize Firefox Web Driver
FirefoxOptions Fireop = new FirefoxOptions();// Create Instance for Firefox Option class
Fireop.addArguments("--headless");//it is option method to run Broswer in Headless mode
WebDriver driverFirefox = new FirefoxDriver(Fireop);// Create Instance for Firefox Web Driver
driverFirefox.get("https://www.google.com");//Url Request Form Firefox Driver
String title = driverFirefox.getTitle();//it capture the title and store it into title variable
System.out.println("Page Title: " +title);//it prints the title varible
System.out.println("Browser Name is : "+(((FirefoxDriver) driverFirefox).getCapabilities().getBrowserName()));//it is option method to Print the Browser Name
System.out.println("Browser Version is : "+(((FirefoxDriver) driverFirefox).getCapabilities().getBrowserVersion()));//it is an option method to Print the Browser version
driverFirefox.quit();//it terminate the browser instance
}
}

Code Explanation:-

System.setProperty("webdriver.gecko.driver","E:/CodeBun/Project3/geckodriver.exe");

In the Above Example, System.setproperty()the method is Used to Initialize with Firefox Web driver.

FirefoxOptions Fireop = new FirefoxOptions();

FirefoxOptions Fireop = new FirefoxOptions()is Used to Create an instance for the Firefox Option class.

Fireop.addArguments("--headless");

Fireop.addArguments("--headless")is an Option class method used to run Firefox in headless mode.

Headless mode:-

it Operates your Typical Web Browser but without the General User interface.

WebDriver driverFirefox = new FirefoxDriver(Fireop);

WebDriver driverFirefox = new FirefoxDriver(Fireop)is Used To Create an instance of Firefox Web Driver and Fireop is passed as an argument in the example.

driverFirefox.get("https://www.google.com");

driverFirefox.get("https://www.google.com")is used for Requesting Specific URL, in the Above Statement, it is Requested by Firefox Driver.

String title = driverFirefox.getTitle();

System.out.println("Page Title: " +title);

it will capture the title from the web application store into a string, which then will be printed.

System.out.println("Browser Name is : "+(((FirefoxDriver) driverFirefox).getCapabilities().getBrowserName()));

DriverFirefox.getCapabilities().getBrowserName()is Used For Capturing Browser Name and System.out.println() is used for Printing Browser Name.

System.out.println("Browser Versionis:"+(((FirefoxDriver)driverFirefox).getCapabilities().getBrowserVersion()));

DriverChrome.getCapabilities().getBrowserVersion()is Used For Capturing Browser Version and System.out.println()is used for Printing Browser Version.

driverFirefox.quit();

driverFirefox.quit()this would close all windows Open by Firefox Web Driver Also driver Will be Closed background.

Output:-

We Have Successfully Demonstrate the use option class in selenium for Chrome and Firefox Driver.