SUBSCRIBE HERE TO GET POST UPDATES VIA EMAIL :

Delivered by FeedBurner

Selenium : Loading Google Chrome Driver With Extensions

By default, Chrome driver browser instance opens with fresh profile when webdriver launch it. So your installed extensions in google chrome browser will not display in google chrome driver browser instance when you run test in chrome driver. Now supposing there is an extension(any xyz extension) which i wants to load with google chrome driver when run your test using selenium webdriver. Is is possible? How to load google chrome driver browser instance with extension? Yes we can do it.. You need to follow bellow given steps to load chrome driver with extension.

For example, i wants to load Page Ruler extension with google chrome driver instance which is already installed in my google chrome default browser. Way to load any extension with google chrome driver is same. You can load any other extension too as per your requirement. Earlier we learnt how to load firefox driver with add-on in THIS POST.

To load any chrome extension with webdriver chrome driver instance, You need .crx file. If you have .crx file, you can load it very easily in selenium webdriver chrome instance. Bellow given steps will create .crx file of Page Ruler google chrome extension.

Get Extension ID
First of all you need to get ID of extension.
For that,
  • Open google chrome browser.
  • Open URL : chrome://extensions/ in chrome browser. It will show you list of installed extensions.
  • Tick developer mode check box which is display at top right corner of page.
  • It will show you ID of each extension as shown in bellow image. Note down the ID of Page Ruler extension which you wants to load with google chrome driver instance as shown bellow.

Locate Chrome Extension Folder
First of all you need to locate folder where your extension files are stored. Generally you will get extension folder path using bellow given syntax.
  • Open Win Run dialog using keyboard Win + R keys.
  • Paste bellow given path in Run dialog and press enter.
%USERPROFILE%\Local Settings\Application Data\Google\Chrome\User Data\Default\Extensions

It will open folder where your all google chrome extensions are stored as shown in bellow image. Locate your extension folder and open it. You can locate it using its ID, which is provided on google chrome's extension page as shown in bellow image.



  • Open that folder as shown in above image.
  • There will be another folder with extension version number. Current version of page ruler extension is 2.0.9 so folder name is display like 2.0.9_0. It will be different for other extensions.
  • Double click on extension's version folder to open it. 
  • Copy full path of extension's version folder and paste it in notepad. We will use it in next step.



So now you have full path of your extension's version folder and now you can pack extension very easily to create .crx file as described in bellow step.

Create .crx  file by packing extension
To pack extension

  • Navigate to google chrome extensions page.
  • Tick developer mode check box.
  • Click on Pack extension button. It will open Pack extension dialog as bellow.
  • Set Extension's version folder path(which is copy during previous step) in "Extension root directory" text box of Pack extension dialog.
  • Click on Pack extension button.
  • It will create .crx file in your extension folder. .crx file path will display on Pack extension dialog as shown bellow. .crx file name can be different as per your extension version.


  • Go to folder where .crx file is located.
  • Copy 2.0.9_0.crx file in our example and paste it in D: drive.
Now we have 2.0.9_0.crx file in D: drive which we can use in our selenium webdriver test to load extension with chrome driver instance.

Create WebDriver test to load chrome driver with extension
Now we are ready to launch chrome driver browser instance with extension. Create bellow given test in eclipse and run it.

chromeExtension.java
package SeleniumExcercise;

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 chromeExtension {

 WebDriver driver;

 public static void main(String args[]) {

  // Create object of ChromeOptions to load chrome driver options.
  ChromeOptions options = new ChromeOptions();

  // Load extension file from D: drive.
  options.addExtensions(new File("D://2.0.9_0.crx"));

  // Set chromedriver.exe path.
  System.setProperty("webdriver.chrome.driver", "D://chromedriver.exe");

  // Set browser capability to load options with driver.
  DesiredCapabilities capabilities = new DesiredCapabilities();
  capabilities.setCapability(ChromeOptions.CAPABILITY, options);

  // Load chrome driver with extension.
  ChromeDriver driver = new ChromeDriver(capabilities);
  driver.get("http://www.google.com");
 }
}

Above test will load chrome driver with page ruler extension as shown in bellow image.


This way you can load any extension with chrome driver using .crx file.

2 comments:

  1. After installing the extension, how do you open it through a click? How do you interact with the extension through Selenium?

    ReplyDelete
  2. After installing the extension, how do you open it through a click? How do you interact with the extension through Selenium?

    ReplyDelete