Thursday, May 19, 2011

Injecting Firebug into a remote FIrefox instance on Selenium 2

There are ample instructions about how to instantiate Firefox with Firebug on a localhost, but what if you're trying to do so with a reverse SSH tunnel (see http://hustoknow.blogspot.com/2010/10/using-selenium-tests-with-djangopython.html) and don't intend to be running things on your localhost? Well, the Selenium 2 developers provide a way, albeit not very well documented. Apparently there is an API command to base-64 encode a .zip file that can be transferred over the network to the remote browser.

1. First, create the profile on your local machine:

firefox -ProfileManager --no-remote

2. Install the Firebug add-on, enabling the Net/Console panels after the plug-in is installed (so these settings get saved inside your prefs.js file))

3. Tar up the profile directory (stored inside ~/.mozilla/firefox with some special hash prefix i.e. ~/.mozilla/firefox/60f709x.selenium) and transfer to the machine that will invoke

(At the time of this writing, you also must add a user.js file to the profile directory, since the Selenium Python bindings expect to find this file even though this file does not normally get created by default -- see patch submitted at http://code.google.com/p/selenium/issues/detail?id=1692 and http://kb.mozillazine.org/User.js_file)

4. Unzip the file into a directory.

Then the profile can be added as follows:

from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
profile = FirefoxProfile(profile_directory="/home/myuserid/.mozilla")
profile.update_preferences() # increases the num of simultaneous connections among other things

from selenium.webdriver.common.desired_capabilities import DesiredCapabilities 
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
profile = FirefoxProfile(profile_directory="/home/myuserid/.mozilla")

selenium_browser = webdriver.Remote(desired_capabilities=DesiredCapabilities.FIREFOX,
                                    command_executor=http://anotherhost.com:4444/wd/hub',
                                    browser_profile=profile)


The Python bindings for Selenium 2 will take the directory, zip it up, base-64 encode, and transfer it over the network to the Selenium RC server. Note that the old Selenium 1.0 way of using the java -jar selenium-server.jar -firefoxProfileTemplate “<selenium Profile Directory>" (as described in http://girliemangalo.wordpress.com/2009/02/05/creating-firefox-profile-for-your-selenium-rc-tests/) seems to be ignored for WebDriver-based Selenium 2 tests, so if you're planning on using only Selenium 2, the approach described approach seems to be the way things work.

2 comments:

  1. Thanks. Using the above, I was also able to get profile management working for standalone Firefox webdriver, as in webdriver.Firefox(browser_profile=profile)
    Before, I never figured out the correct way to instantiate FirefoxProfile.

    ReplyDelete
  2. This is realy a Nice blog post read on of my blogs It is really helpful article please read it too my blog Firefox not responding you can visits our websites or toll free no +1-866-558-4555. solve your problem fastly.

    ReplyDelete