Selenium / Capybara - 无法加载Firefox配置文件

6
当我使用Selenium运行测试时,浏览器反复弹出,指出找不到Firefox配置文件。我已经准备了一个用于Selenium的Firefox配置文件,但是我不确定如何告诉Selenium该配置文件在哪里。
我应该如何告诉Selenium使用哪个Firefox配置文件?
4个回答

4
我遇到了同样的错误。对我来说,问题是在我的测试中调用save_and_open_page导致的。我删除它们后,Firefox配置文件错误停止了。
我还没有需要为capybara/selenium创建特殊的Firefox配置文件,但是为了更彻底地回答你的问题,在解决这个问题时,我找到了以下两种指定Firefox配置文件的方法。
注意:这两种方法都没有解决我的配置文件错误问题,但我还是包含在这里,因为你问了。
方法1: (要求项目中的每个开发人员在Firefox中设置特殊配置文件。)
将以下内容添加到test_helper.rb文件中。
Capybara.register_driver :my_firefox_driver do |app|
  Capybara::Selenium::Driver.new(app, :browser => :firefox, :profile => 'name_of_existing_profile')
end

方法二:(不需要项目中的每个开发人员在Firefox中设置特殊配置文件。)

将以下内容添加到您的测试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

无论您选择上述哪种方法,请将默认驱动程序设置为新驱动程序,或通过在测试开始时放置Capybara.current_driver = :my_firefox_driver来选择性使用新的驱动程序,并确保您的test_helper.rb包括一个tearDown任务以Capybara.use_default_driver这应该是如果您按照设置说明操作的话。

谢谢!我设定 Chrome 为默认浏览器后,成功让“Show me the page”(save_and_open_page)运行。现在所有测试都在 FF 上运行,并在 Chrome 上显示保存的页面。 - vas

1
为了在Ruby中实现这个,需要进行大量的调查,但我最终成功了。
首先,使用-p标志启动Firefox以选择配置文件。创建一个新的配置文件并将其存储在项目中的某个位置。在我的情况下,是在“firefox_profile”目录中。之后,您需要给Selenium一个提示,告诉它在哪里找到这个配置文件,为此,您可以monkey patch layout_on_disk方法:
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

在这里作为Gist


0

我也遇到了这个问题,最后发现与Firefox配置文件无关。在我的情况下,是因为Ghostdriver的版本与PhantomJS不兼容以及Selenium的版本与FirefoxDriver不兼容(我尝试设置我的代码以允许两者同时使用)导致了classpath不兼容性。移除Ghostdriver依赖项并注释掉PhantomJS代码可以解决此配置文件错误。实际上,如果我更仔细地阅读它给我的错误消息,我会发现配置文件错误的根本原因是由于类不兼容而导致的缺少方法。具体错误信息类似于:

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


1
为什么要踩我?尽管根本原因不同,但我和原问题作者有相同的症状。我在搜索中发现了这个问题,并认为其他人也可能会遇到同样的问题。我可以理解不点赞,但你真的认为我的回答会有害吗?我承认仔细阅读是最终的解决方案,但我认为我不是唯一一个通过谷歌寻找弹出框问题的解决方案,而不是忽略弹出框错误并查看日志中的消息。 - KC Baltz

0
我在 Firefox 更新后遇到了这个错误。 我手动打开了 Firefox,允许其应用更新,之后测试就正常工作了。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接