0

我正在努力将一些 Selenium 测试转换为使用 Selenium 网格并且遇到了问题。

测试在本地机器上使用浏览器运行良好。

我创建了一个这样的新驱动程序(在 spec_helper.rb 中):

Capybara.register_driver :selenium_remote do |app|
  caps = Selenium::WebDriver::Remote::Capabilities.chrome
  Selenium::WebDriver.for(:remote, 
    :url => 'http://gridhub:4444/grid/driver', 
    :desired_capabilities => caps)
end

当我使用新驱动程序运行测试时,会联系集线器并且节点加载 Chrome 驱动程序,网格侧的一切看起来都正常。

问题是在启动测试的机器上我得到以下异常:

Failure/Error: Unable to find matching line from backtrace
     SystemStackError:
       stack level too deep
# /home/.rvm/gems/ruby-1.9.3-p194/gems/capybara-2.0.1/lib/capybara.rb:298

我创建了这个最基本的测试,但运行它时仍然看到相同的异常:

Capybara.default_driver = :selenium_remote

describe 'I can' do
  it 'visit Google without throwing an error' do        
    visit 'http://www.google.com'
  end
end

我正在使用 Selenium 服务器独立 2.26.0、Capybara 2.0.1 和 RSpec 2.9.0。

任何想法是什么原因造成的?

4

1 回答 1

1

好吧,在这篇博文(http://www.softr.li/blog/2012/11/08/capybara-selenium-rspec-vagrant-remotely)的帮助下,我得到了它的工作。

上述博文基于另一篇博文(http://www.without-brains.net/blog/2012/08/01/capybara-and-selenium-with-vagrant)。

看起来我错误地注册了 Capybara Selenium Web 驱动程序。

以下是博客文章中正确注册驱动程序的代码。只需按原样使用它即可解决我的问题,然后我对其进行了修改以供自己使用。

该代码也在要点中(https://gist.github.com/4038197#file-capybara_remote-rb)。

创建一个新的 Ruby 文件,添加此代码,并将其包含在您的 spec_helper.rb 文件中。

# SELENIUM_SERVER is the IP address or hostname of the system running Selenium
# Server, this is used to determine where to connect to when using one of the
# selenium_remote_* drivers
SELENIUM_SERVER = "10.10.11.1"

# SELENIUM_APP_HOST is the IP address or hostname of this system (where the
# tests run against) as reachable for the SELENIUM_SERVER. This is used to set
# the Capybara.app_host when using one of the selenium_remote_* drivers
SELENIUM_APP_HOST = "10.10.11.2"

# CAPYBARA_DRIVER is the Capybara driver to use, this defaults to Selenium with
# Firefox
CAPYBARA_DRIVER = "selenium_remote_firefox"

# At this point, Capybara.default_driver is :rack_test, and
# Capybara.javascript_driver is :selenium. We can't run :selenium in the Vagrant box,
# so we set the javascript driver to :selenium_remote_firefox which we're going to
# configure.
Capybara.javascript_driver = :selenium_remote_firefox

RSpec.configure do |config|

  config.before(:each) do
    if selenium_remote?
      Capybara.app_host = "http://#{SELENIUM_APP_HOST}:#{page.driver.rack_server.port}"
    end
  end

  config.after(:each) do
    Capybara.reset_sessions!
    Capybara.use_default_driver
    Capybara.app_host = nil
  end

  # Determines if a selenium_remote_* driver is being used
  def selenium_remote?
    !(Capybara.current_driver.to_s =~ /\Aselenium_remote/).nil?
  end
end

# CapybaraDriverRegistrar is a helper class that enables you to easily register
# Capybara drivers
class CapybaraDriverRegistrar

  # register a Selenium driver for the given browser to run on the localhost
  def self.register_selenium_local_driver(browser)
    Capybara.register_driver "selenium_#{browser}".to_sym do |app|
      Capybara::Selenium::Driver.new(app, browser: browser)
    end
  end

  # register a Selenium driver for the given browser to run with a Selenium
  # Server on another host
  def self.register_selenium_remote_driver(browser)
    Capybara.register_driver "selenium_remote_#{browser}".to_sym do |app|
      Capybara::Selenium::Driver.new(app, browser: :remote, url: "http://#{SELENIUM_SERVER}:4444/wd/hub", desired_capabilities: browser)
    end
  end
end

# Register various Selenium drivers
CapybaraDriverRegistrar.register_selenium_remote_driver(:firefox)

现在,任何带有 :js => true 元数据的 RSpec 示例都将使用此驱动程序。

于 2013-01-08T18:19:55.823 回答