黄瓜;Selenium WebDriver - 如何使用 Google Chrome 作为测试浏览器而不是 Firefox?

20

在我的Rails 3.1应用程序中,为了Cucumber场景测试,我使用了@javascript标签,因此激活了Selenium。我遇到了以下错误:

Could not find Firefox binary (os=macosx). Make sure Firefox is installed or set the path manually with Selenium::WebDriver::Firefox::Binary.path= (Selenium::WebDriver::Error::WebDriverError)
如果可能的话,我想使用Google Chrome作为浏览器 - 而不是Firefox(我没有安装它)。这是否可行?有什么可以做的吗?
实际上,Cucumber/Selenium难道不应该检测浏览器并使用它吗?
====编辑====
添加后
Capybara.register_driver :selenium do |app|
  Capybara::Selenium::Driver.new(app, :browser => :chrome)
end

将...添加到features/support/env.rb之后,我现在得到了这个错误:

Unable to find the chromedriver executable. Please download the server from http://code.google.com/p/chromium/downloads/list and place it somewhere on your PATH. More info at http://code.google.com/p/selenium/wiki/ChromeDriver. (Selenium::WebDriver::Error::WebDriverError)
  ./features/step_definitions/web_steps.rb:45:in `/^(?:|I )am on (.+)$/'
  features/update_memories.feature:11:in `Given I am on the home page'

我从这里下载了文件。我试着将可执行的chromedriver放在/usr/bin路径下,但仍然遇到上述错误。

====编辑 2====

按照下面的建议更进一步操作并运行 "sudo chmod +x /usr/bin/chromedriver" 后,运行cucumber时我现在收到一个新的错误:

@javascript
  Scenario: navigate to memory update page from home page              # features/update_memories.feature:11
    Given I am on the home page                                        # features/step_definitions/web_steps.rb:44
      unable to connect to chromedriver http://127.0.0.1:57870 (Selenium::WebDriver::Error::WebDriverError)
      ./features/step_definitions/web_steps.rb:45:in `/^(?:|I )am on (.+)$/'
      features/update_memories.feature:12:in `Given I am on the home page'
    When I activate the edit memory switch for the memory "I played"   # features/step_definitions/memories/memory_steps.rb:5
    Then I should be on the edit memory page for the memory "I played" # features/step_definitions/web_steps.rb:187
      PGError: server closed the connection unexpectedly
        This probably means the server terminated abnormally
        before or while processing the request.
      : ROLLBACK (ActiveRecord::StatementInvalid)

需要帮助!越来越接近了...

6个回答

32

对于 capybara,请将以下内容添加到 env.rb 文件中:

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

下载Chrome驱动程序并将其复制到您的路径中,例如/usr/bin/,然后使其可执行。

$ sudo chmod +x /usr/bin/chromedriver

1
好的,我用你指定的chmod命令进展了一步(谢谢!),但现在我遇到一个新错误。我正在更新我的问题。 - dmonopoly
1
顺便说一句,我能够使用Homebrew安装chromedriver,但需要在我的Textmate PATH中添加/usr/local/bin到首选项->高级->Shell变量。 - orbiteleven
Chrome驱动程序的链接已更改为http://chromedriver.storage.googleapis.com/index.html。 - equivalent8
我终于成功了,使用 sudo mv chromedriver /usr/local/bin/chromedriver - Jeff Tian
非常感谢你。附言:在Rails中使用RSpec,我只需要将这个放在spec_helper.rb中。 - Ali
显示剩余4条评论

3
今天获取chromedriver可执行文件最简单的方法似乎是安装chromedriver-helper gem。有关详细信息,请参见https://github.com/flavorjones/chromedriver-helper
除了安装gem之外,您还需要按照其他答案中已经提到的方式配置环境:
Capybara.register_driver :selenium do |app| Capybara::Selenium::Driver.new(app, :browser => :chrome) end

1
如果您正在使用Capybara,请尝试以下操作:
Capybara.register_driver :selenium do |app| Capybara::Selenium::Driver.new(app, :browser => :chrome) end
有关详细信息,请参见capybara文档(特别是查看配置和添加驱动程序部分)。

我按照你建议的做了,但是我遇到了一个新的错误(请参见更新的问题)... 有没有可能你能帮忙并更新你的答案? - dmonopoly

1
Capybara.default_driver = :chrome
Selenium::WebDriver::Chrome::Service.executable_path = '/usr/local/bin/chromedriver' # specify the path of chromedriver
Capybara.register_driver :chrome do |app|
  Capybara::Selenium::Driver.new(app, :browser => :chrome)
end

0

如果你在使用带有Bash的Linux系统,请将chrome驱动器路径放入PATH变量中。

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

下载Chrome驱动程序可执行文件并将其复制到您的路径中,例如 /usr/bin/,并使其可执行。

$ sudo chmod +x /usr/bin/chromedriver

这对我有用。


0
尝试在setUp函数的类中将设置“*chrome”更改为“*googlechrome”。

不。Selenium本身有令人困惑的*chrome vs *googlechrome选项,但在Capybara中,选项则是较为清晰的:firefox vs chrome。 - Anders Kindberg

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