Adding add-on in Firefox and Chrome using WebDriver

Adding add-on in Firefox and Chrome using WebDriver

Title sounds interesting. But this is not so easy for all the new selenium learners to launch chrome browser or Firefox browser with certain plugin, So in this post we will learn to add add-on in firefox and chrome using Selenium WebDriver.

So we are going to start with Firefox, I assume you are excited to learn this skill, So lets’ begin with simple step by step feeding.
Steps:
1-  For this we need to create one profile
FirefoxProfile firefoxprofile = new FirefoxProfile();

2- Now go to add-on’s binary download page and download the add-on with .xpi extension and save it in Default Download location.Suppose taking path of .xpi file is c:/add-on

oh still you are thinking what we need to do then


3- Read the file location by using File class that we normally use for creation of files and directories, file searching, file deletion etc.

File addonpath = new File("path of .xpi file");

4-  Now time has came to call the addExtension() of FirefoxProfile class.This method will install add-on in new profile created with new Instance of Firefox.

firefoxprofile.addExtension(addonpath);
Now pass this profile in to new instance of FirefoxDriver

As a whole this code will look like this in Eclipse to install add-on in Firefox using WebDriver

FirefoxProfile firefoxprofile = new FirefoxProfile();
File addonpath = new File("path of .xpi file");
firefoxprofile.addExtension(addonpath);

System.setProperty("webdriver.gecko.driver", "path of gecko driver");
WebDriver driver = new WebDriver(firefoxprofile)



So now task is complete for Firefox but still we need to do the same for Chrome.

But don’t you think, we might be following the same process here in chrome as well in firefox we were talking about firefoxprofile but here we would be taking chromeoption.

So lets’ begin, In Chrome we need to create instance of ChromeOption class . This class has methods like addExtension()- which is used to install add-on in new instance of Chrome,  and along with this we have setBinary() method, which is used to sets the path of Chrome executable,

setArguments()- Adds additional command line arguments to be used when starting Chrome.

Steps for Chrome:
1-
Download the add-on in default location and read it using File Class again like above
File addonpath = new File("path of .crx file");
2- Now create instance of ChromeOptions
ChromeOptions chrome = new ChromeOptions();
chrome.addExtensions(addonpath);

System.setProperty(“webdriver.chrome.driver”, “path of chromedriver”);
WebDriver driver = new ChromeDriver(options);

So here we have seen code to add plugin/add-on to firefox and chrome browser but still you feel difficulty to launch chrome browser and you don’t understand for what reason we are using System.setProperty then read Launch Chrome Browser Using WebDriver.