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
Selenium WebDriver on Firefox: Working with Add-Ons

Quick Notes on HTML 5: Document Skeleton and Basic Rules

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<!-- saved from url=(0014)about:internet -->
<title>Titles goes here</title>
<link href="style.css" rel="stylesheet">
<script src="script.js></script>
</head>
<body>
<p>Contents are here</p>
</body>
</html>

1. Use of <html>, <head> and <body> tags is optional. If they are not present, browser renders them automatically. Usage is still recommended for cleanness purposes.

2. <!DOCTYPE html> is required to prevent browsers from displaying document in quirks mode

3. <meta charset=…> definition is highly recommended right after <!DOCTYPE… or at the top of <head> section. UTF-8 is the most widely used encoding (most compact and fastest for pretty much all browsers).

4. Natural language specification, e.g. lang=”en” is optional, but recommended, as it allows search engines and screen readers to identify section correctly. Specifying language for html tag defines it for entire page, however it also can be specified for elements, such as div.

5. Stylesheet definition does not require type=”text/css” specification, since “rel” (relationship) defines link as stylesheet, and css is the only style sheet language.

6. Similarly, no need to specify language=”JavaScript” for script tag: JavaScript is assumed by default.

7. Closing </script> tag is required, even when referring an external JavaScript file.

8. Comment <!– saved from url=(0014)about:internet –> is only required to enable debugging of local page in IE; if not specified, when you load local page, IE prevents running any JavaScript on the page, unless you explicitely click “Allow blocked content” (this comment is ignored by any other browser)

9. Tags can be lower and/or upper case.

10. Attributes can be specified without values (e.g. <input type=”checkbox” checked>). This is not commonly used, however, since inconsistent with other attributes. XHTML standard required value for each attribute (e.g. <input type=”checkbox” checked=”checked” />).

11. Closing tags are not required for void elements (elements without nested contents) such as <br>, <img> and <hr>. Again, XHTML required <br />, <img … /> and <hr />.

12. Quotation marks around attribute values are only required if attribute values contain >, = or space. E.g. “<img alt=”This is an image” src=image.gif> is valid for HTML5, but not XHTML.

Loose rules 9-12 are likely to fail on validators. It also may be considered a bad “not-so-clean” style. From the browser perspective, however, those inconsistencies do not add any overhead, and thus following XHTML rules is not necessary from performance or compatibility perspective.

Quick Notes on HTML 5: Document Skeleton and Basic Rules

Notable Firefox Extensions

  1. Download: FlashGot extension with Free Download Manager (though add-on supports many other download managers as well), and optionally Video DownloadHelper for downloading videos from sites like youtube. And Web2PDF Converter to save web pages as PDF.
  2. Bookmarks: Delicious (requires account on delicious.com)
  3. Anti-ad: I use the combination of Adblock Plus (advertisement and banner blocker) and Beef Taco (a cookie that blocks targeted advertisement from many companies, like Google, Microsoft or Yahoo).
  4. Development and Testing: Firebug and YSlow described on this page and Web Developer – a toolbar, which gives easy access to dozens of useful tools, from enabling and disabling different components, to CSS preview, to validation…
  5. Feeds, Blogs: I am not big fan of reading feeds, I prefer to go to the original site. However I still need to be notified when a new post appears on one of the sites I read regularly. Also I often have a bunch of “things I want to read later”  (e.g. when I have time or when commuting). So I use Sage, which is a nice feed reader/notifier and Read It Later, which does what its name says.
  6. Boosts: I usually open many tabs, which bloats Firefox memory and CPU, I also like to install SaveMemory.
  7. Multi-functional: GreaseMonkey is a champion, with lots of existing scripts and ability to create new ones.

Installation links on Mozilla site: FlashGot / Video DownloadHelper / Web2PDF Converter / Delicious / AdBlock Plus / Beef Taco / Firebug / YSlow / Web Developer / Sage / Read It Later / SaveMemory / GreaseMonkey

Notable Firefox Extensions

Firefox About Protocol

The complete list is available on Mozillazine page

The ones I use all the time:

Firefox About Protocol

Firefox Profiles

In essence Firefox profile encapsulates user information: bookmarks, cookies, history, private information, e.g. passwords and so on. Which makes them very useful in many occasions, e.g.

  • Different people sharing the same computer and not willing to create different users on operating system level. Especially with blooming social networks, blogs and other sites where logging in is required, it’s much easier to keep different profiles, than log-out and log-in each time, to each site.
  • Even for the same person who is using the same machine for different purposes, this feature can allow to separate the roles of the machine, from web browsing perspective, e.g. “home”, “work”, etc.
  • Sometimes creating separate profile is also good when working on a specific project which requires a large amount of bookmarks, which can be deleted once the project is complete.
  • Finally profiles are very useful in testing, as they allow to simulate different users without the pain of creating different sessions on OS level

Without further configuration Firefox usese a default profile (in early versions of Firefox you could see it every time you opened a browser). In Firefox 2 or 3, however, you need to start Profile Manager purposely to see the profile you are using and to create/delete additional profiles.

Summary of the commands:

  1. Start Profile Manager
     firefox.exe -ProfileManager

    The switch is not case-sensitive, thus you can type -profilemanager. And in addition you can use -p or -P switch: their meaning is slightly different, according to the command line reference, but they open the same Profile Manager anyways. Usually there’s no need to specify the full path to Firefox (e.g. C:\Program Files\Mozilla Firefox\). With -ProfileManager switch alone, Profile Manager will only start if you don’t have another instance of Firefox running. Thus

  2. Start Profile Manager, while another instance of Firefox is already running, or open Firefox with multiple profiles simultaneously (works with Firefox 2 or higher):
        firefox.exe -P -no-remote

  3. Start Firefox with a specified profile:
        firefox.exe -P "profile name" [-no-remote]

    A -no-remote switch in this case again is only required if another instance of Firefox with different profile is already running.

  4. Create a new profile:
        firefox.exe -CreateProfile "profile name" [-no-remote]

    Once again, -CreateProfile switch is not case-sensitive, and -no-remote switch is only needed if another instance of Firefox is already running.

  5. Create a new profile in the non-default location:
        firefox.exe -CreateProfile "profilename profile_path" [-no-remote]

    Here profile name and path must be quoted together.

  6. Finally, you can start Firefox with a profile, that is not defined through the Profile Manager (useful when you need to test something, with a profile that you recieved, say, from a customer):
        firefox.exe -profile "X:\myprofile" [-no-remote]

  7. And this is how to recreate a default profile

Profiles are defined in the file called profiles.ini, on Windows located in %APPDATA%\Mozilla\Firefox folder. An explanation of the file structure and the meaning of the values is provided on Profiles.ini_file Mozillazine page.

I couldn’t find an official reference to confirm this number, but from what I’ve seen, Firefox allows up to 20 profiles per Windows user.

Firefox Profiles