Created
November 17, 2012 11:47
-
-
Save saturday06/4095192 to your computer and use it in GitHub Desktop.
Rails3 configuration example for using selenium grid
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
require 'spork' | |
require 'socket' | |
Spork.prefork do | |
require 'cucumber/rails' | |
# read environment variables | |
desired_capabilities = { | |
browserName: (ENV['SELENIUM_DESIRED_BROWSER_NAME'] or "firefox"), | |
version: ENV['SELENIUM_DESIRED_VERSION'], | |
platform: ENV['SELENIUM_DESIRED_PLATFORM'], | |
} | |
if ENV['SELENIUM_HUB_URL'] | |
selenium_hub_url = ENV['SELENIUM_HUB_URL'] | |
elsif ENV['JENKINS_URL'] | |
uri = URI(ENV['JENKINS_URL']) | |
selenium_hub_url = "#{uri.scheme}://#{uri.host}:4444/wd/hub" | |
else | |
selenium_hub_url = "http://127.0.0.1:4444/wd/hub" | |
end | |
# guess ip address that external www browsers can connect | |
# see http://coderrr.wordpress.com/2008/05/28/get-your-local-ip-address/ | |
do_not_reverse_lookup = Socket.do_not_reverse_lookup | |
begin | |
Socket.do_not_reverse_lookup = true | |
server_host = UDPSocket.open do |socket| | |
socket.connect(URI(selenium_hub_url).host, 4444) | |
socket.addr.last | |
end | |
ensure | |
Socket.do_not_reverse_lookup = do_not_reverse_lookup | |
end | |
# intercept WebMock | |
if defined?(WebMock) && !WebMock.net_connect_allowed?(selenium_hub_url) | |
# currently we can't help choosing dirty solution | |
if WebMock::Config.instance.allow | |
raise RuntimeError.new "WebMock::Config.instance.allow is present: #{WebMock::Config.instance.allow}" | |
end | |
options = { | |
allow_localhost: WebMock::Config.instance.allow_localhost, | |
allow: selenium_hub_url, | |
net_http_connect_on_start: WebMock::Config.instance.net_http_connect_on_start, | |
} | |
WebMock.disable_net_connect! options | |
end | |
# configure capybara | |
Capybara.default_selector = :css | |
Capybara.default_driver = :selenium | |
Capybara.register_driver :selenium do |app| | |
Capybara::Selenium::Driver.new(app, | |
:browser => :remote, | |
:url => selenium_hub_url, | |
:desired_capabilities => desired_capabilities, | |
) | |
end | |
server_port = Capybara.current_session.driver.rack_server.port | |
Capybara.app_host = "http://#{server_host}:#{server_port}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment