Header Ads

HOW TO ADD AN ADD-ON TO FIREFOX INSTANCE OF SELENIUM WEBDRIVER?

We can also run the Firefox web browser with addons/extensions using capabilities to perform automation testing.


Here is the very basic program to run Firefox 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.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.remote.DesiredCapabilities;

public class RunFirefoxWithAddons{

 public static void main(String[] args) { 

  WebDriver driver = null;

  FirefoxProfile firefoxProfile = new FirefoxProfile();
  File addonpath = new File("path of addon/extension (.xpi file)");
  firefoxProfile.addExtension(addonpath);

  DesiredCapabilities capabilities = DesiredCapabilities.firefox();
  capabilities.setCapability(FirefoxDriver.PROFILE, profile);

  driver = new FirefoxDriver(capabilities);

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

}

Detailed explanation for the above program is as follows:




Following code is the required packages 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 Firefox browser and Selenium remote capabilities.
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.remote.DesiredCapabilities;



Following code is to initialize the webdriver.
WebDriver driver = null;
Following code is to define Firefox profile.
FirefoxProfile firefoxProfile = new FirefoxProfile();
Following code is to define filepath of firefox addon/extension.
File addonpath = new File("path of addon/extension (.xpi file)");
Following code is to add extension into defined Firefox profile.
firefoxProfile.addExtension(addonpath);
Following code is to define Firefox capability.
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
Following code is to set Firefox capability.
capabilities.setCapability(FirefoxDriver.PROFILE, profile);
Following code is to initialize the firefox driver.
driver = new FirefoxDriver(capabilities);
Following code is to open the hello selenium blog in firefox 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();



No comments


What would you like to add in my list? I look forward to reading your comments below.



Powered by Blogger.