Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Capybara configuration connecting to selenium server for testing with a remote browser (pretty useful in Vagrant)
# 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)
@ikk0v

This comment has been minimized.

Copy link

@ikk0v ikk0v commented Jan 23, 2017

Is there a way to pass a profile to remote FIreFox driver?
I want it to ignore SSL errors and dont know how...

@charlieperson

This comment has been minimized.

Copy link

@charlieperson charlieperson commented Aug 16, 2017

I keep getting:

NoMethodError:
undefined method `rack_server' for #Capybara::Selenium::Driver:0x007f9468b118c8

Any idea why that might be?

@phuongdh

This comment has been minimized.

Copy link

@phuongdh phuongdh commented Sep 15, 2017

I'm running into this problem even though my Selenium Grid is up and running with no error in the log.

     Failure/Error: visit '/playlists'
     
     Selenium::WebDriver::Error::ServerError:
       status code 500
     # /home/app/gems/ruby/2.2.0/gems/selenium-webdriver-3.5.2/lib/selenium/webdriver/remote/response.rb:74:in `assert_ok'
     # /home/app/gems/ruby/2.2.0/gems/selenium-webdriver-3.5.2/lib/selenium/webdriver/remote/response.rb:34:in `initialize'
     # /home/app/gems/ruby/2.2.0/gems/selenium-webdriver-3.5.2/lib/selenium/webdriver/remote/http/common.rb:83:in `new'
     # /home/app/gems/ruby/2.2.0/gems/selenium-webdriver-3.5.2/lib/selenium/webdriver/remote/http/common.rb:83:in `create_response'
     # /home/app/gems/ruby/2.2.0/gems/selenium-webdriver-3.5.2/lib/selenium/webdriver/remote/http/default.rb:107:in `request'
     # /home/app/gems/ruby/2.2.0/gems/selenium-webdriver-3.5.2/lib/selenium/webdriver/remote/http/common.rb:61:in `call'
     # /home/app/gems/ruby/2.2.0/gems/selenium-webdriver-3.5.2/lib/selenium/webdriver/remote/bridge.rb:170:in `execute'
     # /home/app/gems/ruby/2.2.0/gems/selenium-webdriver-3.5.2/lib/selenium/webdriver/remote/bridge.rb:103:in `create_session'
     # /home/app/gems/ruby/2.2.0/gems/selenium-webdriver-3.5.2/lib/selenium/webdriver/remote/bridge.rb:54:in `handshake'
     # /home/app/gems/ruby/2.2.0/gems/selenium-webdriver-3.5.2/lib/selenium/webdriver/remote/driver.rb:39:in `initialize'
     # /home/app/gems/ruby/2.2.0/gems/selenium-webdriver-3.5.2/lib/selenium/webdriver/common/driver.rb:58:in `new'
     # /home/app/gems/ruby/2.2.0/gems/selenium-webdriver-3.5.2/lib/selenium/webdriver/common/driver.rb:58:in `for'
     # /home/app/gems/ruby/2.2.0/gems/selenium-webdriver-3.5.2/lib/selenium/webdriver.rb:86:in `for'
     # /home/app/gems/ruby/2.2.0/gems/capybara-2.11.0/lib/capybara/selenium/driver.rb:22:in `browser'
     # /home/app/gems/ruby/2.2.0/gems/capybara-2.11.0/lib/capybara/selenium/driver.rb:55:in `visit'
     # /home/app/gems/ruby/2.2.0/gems/capybara-2.11.0/lib/capybara/session.rb:246:in `visit'
     # /home/app/gems/ruby/2.2.0/gems/capybara-2.11.0/lib/capybara/dsl.rb:52:in `block (2 levels) in <module:DSL>'
     # ./spec/features/capybara_playlist_spec.rb:31:in `block (2 levels) in <top (required)>'
     # /home/app/gems/ruby/2.2.0/gems/rspec-retry-0.5.4/lib/rspec/retry.rb:112:in `block in run'
     # /home/app/gems/ruby/2.2.0/gems/rspec-retry-0.5.4/lib/rspec/retry.rb:101:in `loop'
     # /home/app/gems/ruby/2.2.0/gems/rspec-retry-0.5.4/lib/rspec/retry.rb:101:in `run'
     # /home/app/gems/ruby/2.2.0/gems/rspec-retry-0.5.4/lib/rspec_ext/rspec_ext.rb:12:in `run_with_retry'
     # /home/app/gems/ruby/2.2.0/gems/rspec-retry-0.5.4/lib/rspec/retry.rb:30:in `block (2 levels) in setup'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment