Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Capybara standalone Selenium Chrome config
web:
build: .
volumes:
- .:/opt/myapp
ports:
- '3000:3000'
links:
- db
- redis
- selenium
...
selenium:
image: selenium/standalone-chrome
container_name: selenium
RSpec.configure do |config|
...
config.before(:each) do
if ENV['SELENIUM_PORT']
server = Capybara.current_session.server
Capybara.app_host = server ?
"http://#{server.host}:#{server.port}" :
"http://localhost:9887"
end
end
config.after(:each) do
Capybara.reset_sessions!
Capybara.use_default_driver
Capybara.app_host = nil
end
...
Capybara.configure do |config|
...
if ENV['SELENIUM_PORT']
if RUBY_PLATFORM.match(/linux/)
config.server_host = `/sbin/ip route|awk '/scope/ { print $9 }'`.chomp
else
config.server_host = '127.0.0.1'
end
end
end
Capybara.register_driver :chrome do |app|
if ENV['SELENIUM_PORT']
uri = URI(ENV['SELENIUM_PORT'])
Capybara::Selenium::Driver.new(app, browser: :remote,
url: "http://#{uri.host}:#{uri.port}/wd/hub", desired_capabilities: :chrome)
else
Capybara::Selenium::Driver.new(app, browser: :chrome)
end
end
Capybara.javascript_driver = :chrome
@aliibrahim

This comment has been minimized.

Copy link

@aliibrahim aliibrahim commented Nov 18, 2016

Thanks for this!

@mpestov

This comment has been minimized.

Copy link

@mpestov mpestov commented Oct 3, 2017

Cool gist! It works for me.

@ecbrodie

This comment has been minimized.

Copy link

@ecbrodie ecbrodie commented Jan 1, 2018

@varyonic What supplies the value for the environment variable 'SELENIUM_PORT'? Is it a magic variable from Docker-Compose? Passed in through the CLI via the rspec command?

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