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 fill 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.

6 comments

  • Your blog is superbbbbbbbbb…I feel like posting many comments.
    Moreover i have sent some questions to your mail id about..

    1.Automating Flash images on web pages like in flipkart, ebay,myntra etc etc
    2.Automating adding bookmarks in FF and chrome.
    3.Suppose if i want to execute 1000 tests in all three FF,IE,chrome .Having one machines setup for running tests.Is it like all 1000 in IE run first then FF then Chrome.

    My mail intension is running 1k tests in all three..

    Should i go for grid having three machines with different browsers installed?suggest me…

  • Waiting for reply

  • Hi Dwarika Dhish Mishra,
    I have tried by above process which you have mentioned for Firefox. Browser opening properly but I cant able to install “selenium-ide-2.5.0.xpi” for add on. Could you please support on this.
    Thanks in advance.

  • Damian Tarkowski

    You can create instance of File class. Whats more ChromeOptions class have method “AddExtension”, not “addExtension”.

  • hi, thanks for the blog.

    I am trying to create extension for bypassing authentication.
    I was able to follow the chrome extension part but facing error on firefox while installing .xpi file on firefox. Please help. Mentioning the steps below

    Created a manifest.json file

    {
    “name”: “Webrequest API”,
    “version”: “1.0”,
    “description”: “Extension to handle Authentication window”,
    “permissions”: [
    “webRequest”,
    “webRequestBlocking”,
    “”
    ],
    “background”: {
    “scripts”: [“webrequest.js”]
    },
    “manifest_version”: 2
    }

    created a webrequest.js file, code for which is mentioned below.

    var target = “https://sso.viacomcloud.com/”;

    var myCredentials = {
    username: “getestone”,
    password: “[email protected]
    }

    var pendingRequests = [];

    // A request has completed.
    // We can stop worrying about it.
    function completed(requestDetails) {
    console.log(“completed: ” + requestDetails.requestId);
    var index = pendingRequests.indexOf(requestDetails.requestId);
    if (index > -1) {
    pendingRequests.splice(index, 1);
    }
    }

    function provideCredentialsSync(requestDetails) {
    // If we have seen this request before, then
    // assume our credentials were bad, and give up.
    if (pendingRequests.indexOf(requestDetails.requestId) != -1) {
    console.log(“bad credentials for: ” + requestDetails.requestId);
    return {cancel:true};
    }
    pendingRequests.push(requestDetails.requestId);
    console.log(“providing credentials for: ” + requestDetails.requestId);
    return {authCredentials: myCredentials};
    }

    browser.webRequest.onAuthRequired.addListener(
    provideCredentialsSync,
    {urls: [target]},
    [“blocking”]
    );

    browser.webRequest.onCompleted.addListener(
    completed,
    {urls: [target]}
    );

    Created a zip file using 7-zip file manager and renamed the zip file as Ext.xpi
    Opened about:config on firefox (verson 70) and changed xpiextensionsignrequired to ‘False’
    Opened about:addons on firefox. Selected ‘install extension from a file’.

    I get the following error: ” this add on could not be installed because it appears to be corrupt.”

    Please advise why am I getting the error.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.