Selenium WebDriver on Firefox: Working with Add-Ons

Running Selenium WebDriver on Firefox with Static Add-Ons

  1. Create a special profile for Firefox
  2. Install add-ons on that profile
  3. Start Firefox as described here

Installing Add-On when Starting Selenium WebDriver on Firefox

import java.io.File;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.FirefoxDriver;
// ...
final String addOnPath = "C:\\Temp\\youraddon.xpi";
File addOnFile = new File( addOnPath );
FirefoxProfile profile = new FirefoxProfile();
profile.addExtension( addOnFile );
WebDriver driver = new FirefoxDriver( profile );

Getting List of Installed / Active Add-Ons with Selenium WebDriver on Firefox

There’s no easy way to achieve this unfortunately. So the method below is really an ugly hack, but it get the job done:

  • Firefox is loaded on about:addons page
  • The page contains list of add-ons in JSON format, which can be parsed
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference( "browser.startup.homepage", "about:addons" ); 
WebDriver driver = new FirefoxDriver( profile );
driver.get( "about:addons" );
		
String source= driver.getPageSource();
final String addOnsSectionStart 		= "<browser id=\"discover-browser\"";
source = source.substring( source.indexOf( addOnsSectionStart ) + addOnsSectionStart.length());
source = source.substring( source.indexOf( "{%22" ) );
source = source.substring( 0, source.indexOf( "\" clickthrough" ) );
source = URLDecoder.decode( source, "UTF-8" );
		
JSONObject addonObjects = new JSONObject(source);
JSONArray jsonAddonArray = addonObjects.names();
System.out.println(
	String.format(
		"%-10s\t%-15s\t%-15s\t%-15s\t%-15s\t%s",
		"type",
		"version",
		"user disabled",
		"is compatible",
		"is blocklisted",
		"name"
	)
);
		
for(int i = 0; i < jsonAddonArray.length(); i++)
{
	JSONObject jsonAddonObject = addonObjects.getJSONObject(jsonAddonArray.getString(i));
	System.out.println(
		String.format(
			"%-10s\t%-15s\t%-15s\t%-15s\t%-15s\t%s",
			jsonAddonObject.getString("type"),
			jsonAddonObject.getString("version"),
			jsonAddonObject.getString("userDisabled"),
			jsonAddonObject.getString("isCompatible"),
			jsonAddonObject.getString("isBlocklisted"),
			jsonAddonObject.getString("name")
		)
	);
}

The output will look like this:

type      	version        	user disabled  	is compatible  	is blocklisted 	name
plugin    	7.7.1.0        	false          	true           	false          	QuickTime Plug-in 7.7.1
plugin    	6.0.260.3      	false          	true           	false          	Java(TM) Platform SE 6 U26
theme     	17.0.1         	false          	true           	false          	Default
plugin    	2.4.2432.1652  	false          	true           	false          	Google Updater
extension 	2.28.0         	false          	true           	false          	Firefox WebDriver
Advertisements
Selenium WebDriver on Firefox: Working with Add-Ons

2 thoughts on “Selenium WebDriver on Firefox: Working with Add-Ons

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s