Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium2 firefox: use the default profile

Selenium2, by default, starts firefox with a fresh profile. I like that for a default, but for some good reasons (access to my bookmarks, saved passwords, use my add-ons, etc.) I want to start with my default profile.

There is supposed to be a property controlling this but I think the docs are out of sync with the source, because as far as I can tell webdriver.firefox.bin is the only one that works. E.g. starting selenium with:

java -jar selenium-server-standalone-2.5.0.jar -Dwebdriver.firefox.bin=not-there

works (i.e. it complains). But this has no effect:

java -jar selenium-server-standalone-2.5.0.jar -Dwebdriver.firefox.profile=default

("default" is the name in profiles.ini, but I've also tried with "Profile0" which is the name of the section in profiles.ini).

I'm using PHPWebdriver (which uses JsonWireProtocol) to access:

$webdriver = new WebDriver("localhost", "4444");

$webdriver->connect("firefox");

I tried doing it from the PHP side:

$webdriver->connect("firefox","",array('profile'=>'default') );

or:

$webdriver->connect("firefox","",array('profile'=>'Profile0') );

with no success (firefox starts, but not using my profile).

I also tried the hacker's approach of creating a batch file:

#!/bin/bash
/usr/bin/firefox -P default

And then starting Selenium with: java -jar selenium-server-standalone-2.5.0.jar -Dwebdriver.firefox.bin="/usr/local/src/selenium/myfirefox"

Firefox starts, but not using by default profile and, worse, everything hangs: selenium does not seem able to communicate with firefox when started this way.

P.S. I saw Selenium - Custom Firefox profile I tried this:

java -jar selenium-server-standalone-2.5.0.jar -firefoxProfileTemplate "not-there"

And it refuses to run! Excited, thinking I might be on to something, I tried:

java -jar selenium-server-standalone-2.5.0.jar -firefoxProfileTemplate /path/to/0abczyxw.default/

This does nothing. I.e. it still starts with a new profile :-(

like image 773
Darren Cook Avatar asked Sep 07 '11 03:09

Darren Cook


2 Answers

Simon Stewart answered this on the mailing list for me.

To summarize his reply: you take your firefox profile, zip it up (zip, not tgz), base64-encode it, then send the whole thing as a /session json request (put the base64 string in the firefox_profile key of the Capabilities object).

An example way to do this on Linux:

cd /your/profile
zip -r profile *
base64 profile.zip > profile.zip.b64

And then if you're using PHPWebDriver when connecting do:

$webdriver->connect("firefox", "", array("firefox_profile" => file_get_contents("/your/profile/profile.zip.b64")))

NOTE: It still won't be my real profile, rather a copy of it. So bookmarks won't be remembered, the cache won't be filled, etc.

like image 73
4 revs, 4 users 46% Avatar answered Oct 12 '22 12:10

4 revs, 4 users 46%


Here is the Java equivalent. I am sure there is something similar available in php.

ProfilesIni profile = new ProfilesIni();
FirefoxProfile ffprofile = profile.getProfile("default");
WebDriver driver = new FirefoxDriver(ffprofile);

If you want to additonal extensions you can do something like this as well.

ProfilesIni profile = new ProfilesIni();
FirefoxProfile ffprofile = profile.getProfile("default");
ffprofile.addExtension(new File("path/to/my/firebug.xpi"));
WebDriver driver = new FirefoxDriver(ffprofile);
like image 26
nilesh Avatar answered Oct 12 '22 13:10

nilesh