Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

selenium / capybara - Firefox profile cannot be loaded

When I run tests using selenium the browsers repeatedly pops up stating that the firefox profile cannot be found. I have a prepared a Firefox profile for use with selenium I'm just not sure how to tell selenium where that profile located.

How do I tell Selenium which firefox profile to use?

like image 254
recursive_acronym Avatar asked Jul 03 '12 22:07

recursive_acronym


2 Answers

I was getting this same error. For me it turned out that it was calls to save_and_open_page within my test that were causing the problem. I removed those and the Firefox profile errors stopped.

I haven't had any need (yet) for a special Firefox profile just for capybara/selenium, but, to answer your question more thoroughly, in trying to solve this problem I came across the following two methods to specify a profile for Firefox.

Note: Neither of these actually solved my problem with the profile errors but I'm including them here anyway, since you asked.

Method 1: (Requires each developer on project to setup special profile in Firefox.)

Add the following to your test_helper.rb

Capybara.register_driver :my_firefox_driver do |app|
  Capybara::Selenium::Driver.new(app, :browser => :firefox, :profile => 'name_of_existing_profile')
end

Method 2: (Does not require each developer on project to setup special profile in Firefox.)

Add the following to your test helper.rb

require 'selenium-webdriver'

...

  Capybara.register_driver :my_firefox_driver do |app|
    profile = Selenium::WebDriver::Firefox::Profile.new
    Capybara::Selenium::Driver.new(app, :browser => :firefox, :profile => profile)
  end

Then, regardless of which above method you choose, set your default driver to the new driver, or selectively use the new driver by putting Capybara.current_driver = :my_firefox_driver at the beginning of your tests and ensuring that your test_helper.rb includes a teardown task to Capybara.use_default_driver which it should if you followed the setup instructions.

like image 182
Jon Garvin Avatar answered Sep 20 '22 09:09

Jon Garvin


To do this in Ruby required a lot of investigation but I got it to work.

First, start Firefox with the -p flag to select a profile. Create a new profile and store it in a place in your project. In my case in the "firefox_profile" directory. After that you need to give Selenium a hint on where to find this profile and to do that you can monkey patch the layout_on_disk method:

module Selenium
  module WebDriver
    module Firefox
      class Profile
        def layout_on_disk
          firefox_profile = File.expand_path(File.join(File.dirname(__FILE__),'firefox_profile'))
          profile_dir = create_tmp_copy(firefox_profile)
          FileReaper << profile_dir

          install_extensions(profile_dir)
          delete_lock_files(profile_dir)
          delete_extensions_cache(profile_dir)
          update_user_prefs_in(profile_dir)

          puts "Using temporary Firefox profile in: #{profile_dir} from #{firefox_profile}"
          profile_dir
        end
      end
    end
  end
end

As Gist here

like image 1
Martin K Avatar answered Sep 22 '22 09:09

Martin K