使用 Homebrew cask 安装时,Selenium WebDriver 找不到 Firefox

Selenium WebDriver can't find Firefox when installed with Homebrew cask

Homebrew cask 在 Mac OS X 上做了一些应用程序链接魔术。显然它不够好,因为寻找 Firefox 可执行文件的 Selenium WebDriver 找不到它。以下是 RSpec 测试 运行 使用 Capybara 驱动 Selenium 的错误消息。

提示 Firefox 可执行文件位置的好方法是什么?

Failure/Error: Unable to find matching line from backtrace
 Selenium::WebDriver::Error::WebDriverError:
   Could not find Firefox binary (os=macosx). Make sure Firefox is installed or set the path manually with Selenium::WebDriver::Firefox::Binary.path=
 # /Users/mende/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/selenium-webdriver-2.43.0/lib/selenium/webdriver/firefox/binary.rb:127:in `path'
 # /Users/mende/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/selenium-webdriver-2.43.0/lib/selenium/webdriver/firefox/binary.rb:60:in `execute'
 # /Users/mende/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/selenium-webdriver-2.43.0/lib/selenium/webdriver/firefox/binary.rb:34:in `start_with'
 # /Users/mende/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/selenium-webdriver-2.43.0/lib/selenium/webdriver/firefox/launcher.rb:70:in `start_silent_and_wait'
 # /Users/mende/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/selenium-webdriver-2.43.0/lib/selenium/webdriver/firefox/launcher.rb:35:in `block in launch'
 # /Users/mende/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/selenium-webdriver-2.43.0/lib/selenium/webdriver/firefox/socket_lock.rb:20:in `locked'
 # /Users/mende/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/selenium-webdriver-2.43.0/lib/selenium/webdriver/firefox/launcher.rb:32:in `launch'
 # /Users/mende/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/selenium-webdriver-2.43.0/lib/selenium/webdriver/firefox/bridge.rb:24:in `initialize'
 # /Users/mende/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/selenium-webdriver-2.43.0/lib/selenium/webdriver/common/driver.rb:31:in `new'
 # /Users/mende/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/selenium-webdriver-2.43.0/lib/selenium/webdriver/common/driver.rb:31:in `for'
 # /Users/mende/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/selenium-webdriver-2.43.0/lib/selenium/webdriver.rb:67:in `for'
 # /Users/mende/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/capybara-2.4.4/lib/capybara/selenium/driver.rb:13:in `browser'

如错误消息所述:您必须为自定义应用程序路径指定 Selenium::WebDriver::Firefox::Binary.path

# in spec_helper.rb
require 'selenium/webdriver'

Selenium::WebDriver::Firefox::Binary.path =
  "/opt/homebrew-cask/Caskroom/Firefox/latest/Firefox.app/Contents/MacOS/firefox"

如果你不想接触测试框架,你可以安装 firefox 到 /Applications(selenium 搜索的默认路径)以使其在不设置 Binary.path.[=15 的情况下工作=]

brew cask install --appdir="/Applications" firefox

我刚刚添加了 Selenium 的代码来专门查找这个目录,而无需设置它或更改 appdir。它将在 selenium-webdriver gem 2.49+

中可用