Firefox is default but still get WebDriverError: unable to connect to chromedriver 127.0.0.1:9515

Firefox is default but still get WebDriverError: unable to connect to chromedriver 127.0.0.1:9515

我是第一次设置 Capybara,它似乎默认调用 Chrome 而不是 Firefox。

起初我收到网络驱动程序错误:

Selenium::WebDriver::Error::WebDriverError: unable to connect to chromedriver 127.0.0.1:9515* with associated stacktrace
https://pastebin.com/TW5NWJgu

我可以通过添加一个 gem 'chromedriver-helper' 来清除它,现在测试用 chromium 打开。

我也尝试将此添加到 spec_helper 和 rails_helper:

Capybara.register_driver :selenium do |app|
  Capybara::Selenium::Driver.new(app, browser: :firefox)
end

我能够确认 rails 能够成功调用 firefox,因为 rails_helper 中的以下命令在我开始测试时确实成功启动了 Firefox(但没有采取任何进一步的操作) (根据评论,我后来删除了这个命令)。

RSpec.configure do |config|
    driver = Selenium::WebDriver.for :firefox
end

我在另一台机器和不同的 rails 应用程序上遇到了同样的错误(也没有在 Capybara 的第一遍中设置 geckodriver)

我的理解是Capybara应该默认调用Firefox。似乎在某处我找不到第二种配置。有谁知道我在哪里可以找到调用 chrome 的行?

从您提供的日志文件中我们可以看到您使用的是 Rails 5.1、RSpec 3.8 和 Capybara 2.18。由于日志还包括 "actionpack-5.1.6/lib/action_dispatch/system_testing/driver.rb",我们可以知道您正在编写系统 tests/specs(通过 rspec-rails)。系统测试使用的驱动程序由 driven_by 方法控制,如 RSpec system spec docs and by default uses the Rails registered :selenium driver which is configured to use Chrome. As documented in the Rails System Test docs 中所述,您可以通过指定

切换到 Firefox
driven_by :selenium, using: :firefox

此外,Capybara 2.18 在这一点上已经过时了。如果您计划使用 Firefox/Chrome.

的最新版本,您可能需要更新到最新版本(截至目前为 3.6)