Tuesday 2 August 2016

Launch Browser with Extensions - Webdriver


Launching the selenium webdriver, by default opens the plain browser without any extensions.
But there is way, to launch the chrome browser along with the pre-installed extensions.

To load any chrome extension in webdriver, we need to package the extension to .crx file & refer the same file in the desired capabilities / chrome options to load when the browser is launched.

Follow the steps mentioned in the below link to create .crx file.
https://developer.chrome.com/extensions/packaging

Refer the *.crx file path in your code & launch the browser.



Method 1:

System.setProperty("webdriver.chrome.driver", "C:/foderpath/chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("PATH/CRXFILE.crx"));

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);

ChromeDriver driver = new ChromeDriver(capabilities);
driver.get("url");

======================= OR ==========================

Method 2:

System.setProperty("webdriver.chrome.driver", "C:/foderpath/chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("PATH/CRXFILE.crx"));

ChromeDriver driver = new ChromeDriver(options);
driver.get("url");


References:
https://sites.google.com/a/chromium.org/chromedriver/capabilities
https://developer.chrome.com/extensions/packaging

No comments:

Post a Comment