selenium / capybara - Firefox profile cannot be loaded

大城市里の小女人 提交于 2019-12-21 16:58:56

问题


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?


回答1:


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.




回答2:


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




回答3:


I also hit this problem and it turned out to be unrelated to the Firefox profile. In my case, it was a classpath incompatibility between the version of Ghostdriver I was using for PhantomJS and the version of Selenium I was using for the FirefoxDriver (I was trying to setup my code to allow for both). Removing the Ghostdriver dependency and commenting out the PhantomJS code made this profile error go away. Really, if I had read the error messages it was giving me more closely, I would have seen that the root cause of the profile error was a missing method due to the class incompatibilities. The specific error was something like:

NoSuchMethodError: org.openqa.selenium.os.CommandLine.waitFor(J)V




回答4:


I encountered this error after a Firefox update.

I opened Firefox manually, to allow it to apply the updates, then the tests worked afterward.



来源:https://stackoverflow.com/questions/11319945/selenium-capybara-firefox-profile-cannot-be-loaded

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!