Selenium and non standard Firefox profiles
by Dima

Custom Fox

One day you will need different browser profiles for testing. For example, you may want to run tests with JavaScript switched off or test a web site with a certain browser plug-in turned on.

User scripts developers (userscripts.org, Greasemonkey) could be interested in running Selenium tests with their script loaded into browser. If you did not work with user scripts – have a look. They can improve your experience with some sites or with browser in general. For example you can add a button which allows downloading YouTube video in different formats.

Another situation is when your application uses CAPTCHA for login. For example such famous Russian sites as dirty.ru and habrahabr.ru use CAPTCHA on login.
What can be done to pass CAPTCHA automatically? On some sites there is an option ‘Remember me’ which allows us to avoid CAPTCHA but Selenium, by default, creates an absolutely new profile in Firefox without any cookies.
We can solve this problem by creating a special ‘logged on’ profile in Firefox which will be used by Selenium.

When Selenium starts it creates a copy of folder containing browser profile and works with this copy. This is why if logon information is stored only in cookies your tests can log off and test your application login page. In this case cookies will be changed but only in folder Selenium created when it was launched. On next launch Selenium will create another copy and tests will end up logged on again avoiding CAPTCHA.

Now we will show you how to create and use such profile with Selenium.

Let’s prepare browser profile first. Start Firefox from command line with this command:

firefox.exe -ProfileManager -no-remote

You will see browser UI to control profiles and we hit button “Create Profile …”.

FF profiles window

Now we can specify profile name and a path where profile will be saved.

Name it as you like but remember path as it is important. Most likely you want a profile to be located in your tests project which will use it. In this case you do not need to worry about moving profiles when you will move your project in future. Another option is to put folder with profiles under your version control system. In this case version control system will keep all profiles you need and you can get them on any system you like to run Selenium tests. You will only need to move browser profiles to systems running Selenium RC in case you, like us, are running distributed Selenium Grid.

After we specified a name and a path we hit “Finish” and are back at Firefox profiles UI. Please note – if you will uncheck “Don’t ask at startup” this UI will pop up every time we start a browser (arrow #1). If you will keep it checked – browser will use previous profile silently. Firefox does not remember profile it was launched by Selenium. This is why creation and use of a new profile will not mess up your usual work with this browser.

Now we select our new profile (see picture below arrow #2) and start Firefox. Note that Firefox creates files in a folder we specified with first run.

After Firefox started we can set it up to match our requirements. For example, we can login to a site with CAPTCHA with option “Remember me”, enable or disable some plug-ins, change any other settings we want. Do not forget to uncheck “Always perform default browser check when starting Firefox” if it will pop up.

After settings were changed simply close Firefox – profile will be saved by a browser. If you will need to modify this profile – simply start Firefox with it, change it and close a browser.
Profile is ready. We need to explain Selenium how to use it. It is too easy.

All you need to do is to start Selenium with option -firefoxProfileTemplate. For example, if you selected folder “C:\projects\sel_tests\ff_profile” when you created profile you will start Selenium with following command:

java -jar C:\selenium\selenium-server-1.0-SNAPSHOT.jar -firefoxProfileTemplate C:\projects\sel_tests\ff_profile

Done!

Print this post | Home

One comment

  1. Intv says:

    Спасибо, очень помогло.