Last active
May 18, 2021 13:18
-
-
Save varyonic/aba0f4c9bcc4e51e7ceb220e5f7d45b4 to your computer and use it in GitHub Desktop.
Capybara standalone Selenium Chrome config
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
web: | |
build: . | |
volumes: | |
- .:/opt/myapp | |
ports: | |
- '3000:3000' | |
links: | |
- db | |
- redis | |
- selenium | |
... | |
selenium: | |
image: selenium/standalone-chrome | |
container_name: selenium |
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
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 |
Cool gist! It works for me.
@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
Thanks for this!