HOW TO ADD AN EXTENSION TO GOOGLE CHROME INSTANCE OF SELENIUM WEBDRIVER?

We can also run the Google Chrome web browser with addons/extensions using capabilities to perform automation testing.
To run the Selenium Webdriver programs in Google Chrome web browser we need to download Chrome Driver.


Here is the very basic program to run Google Chrome web browser with addons/extensions using capabilities.




Java Source code:

package com.helloselenium.selenium.test;
 
import java.io.File;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;

public class RunGoogleChromeWithExtension{

 public static void main(String[] args) { 

  WebDriver driver = null;

  File chromeDriver = new File("path of the chromedriver32.exe file");
  System.setProperty("webdriver.chrome.driver", chromeDriver.getAbsolutePath() );    
  
  ChromeOptions chromeOptions = new ChromeOptions();
  chromeOptions.addExtensions(new File("path of addon/extension (.crx file)"));
 
  DesiredCapabilities capabilities = DesiredCapabilities.chrome();
  capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
  driver = new ChromeDriver(capabilities);

  driver.get("http://www.helloselenium.com");
  
  driver.quit();
 }

}

Detailed explanation for the above program is as follows:




Following code is the required package for Java File.
import java.io.File;
Following code is the required packages for Selenium Webdriver.
import org.openqa.selenium.WebDriver;
Following code is the required packages for Chrome driver and Selenium remote capabilities.
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
Following code is to initialize the webdriver.
WebDriver driver = null;
Following code is to locate the chrome driver executable file.
File chromeDriver = new File("path of the chrome driver (.exe file)");
Following code is to set the chrome driver with webdriver property.
System.setProperty("webdriver.chrome.driver", chromeDriver.getAbsolutePath() ); 
Following code is to define Google Chrome options.
ChromeOptions chromeOptions = new ChromeOptions();
Following code is to add addon/extension to Google Chrome options.
chromeOptions.addExtensions(new File("path of addon/extension (.crx file)"));
Following code is to define Google Chrome capability.
DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
Following code is to set Google Chrome capability.
capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);



Following code is to initialize the chrome driver.
driver = new ChromeDriver(capabilities);
Following code is to open the hello selenium blog in chrome browser.
driver.get("http://www.helloselenium.com");
You can also use the following code is to open the hello selenium blog in browser.
driver.navigate().to("http://www.helloselenium.com");
Following code is to quit the driver instance.
driver.quit();
You can also use the following code is to close the driver instance.
driver.close();


Post a Comment

0 Comments