Skip to content

Commit

Permalink
Cukes via Docker - selenium (Chrome in a container)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Sutton committed Dec 22, 2020
1 parent 99e6067 commit ddef339
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
15 changes: 15 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,24 @@ services:
- .:/app
ports:
- "3000:3000"
- "4000:4000"
- "35729:35729"
environment:
- HOSTNAME=localhost:3000
- DOCKERIZED=true
depends_on:
- postgresql

selenium_chrome:
image: selenium/standalone-chrome-debug
logging:
driver: none
ports:
- 5900:5900

selenium_firefox:
image: selenium/standalone-firefox-debug
logging:
driver: none
ports:
- 5900:5900
44 changes: 44 additions & 0 deletions features/support/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,56 @@
)
end

Capybara.register_driver :selenium_chrome_in_container do |app|
Capybara::Selenium::Driver.new app, browser: :remote, url: 'http://selenium_chrome:4444/wd/hub', desired_capabilities: :chrome
end

Capybara.register_driver :headless_selenium_chrome_in_container do |app|
Capybara::Selenium::Driver.new(
app,
browser: :remote,
url: 'http://selenium_chrome:4444/wd/hub',
desired_capabilities: Selenium::WebDriver::Remote::Capabilities.chrome(chromeOptions: { args: %w[headless disable-gpu] })
)
end


Capybara.register_driver :selenium_firefox_in_container do |app|
Capybara::Selenium::Driver.new(app,
browser: :remote,
url: 'http://selenium_firefox:4444/wd/hub',
desired_capabilities: :firefox
)
end

Capybara.register_driver :headless_selenium_firefox_in_container do |app|
Capybara::Selenium::Driver.new(
app,
browser: :remote,
url: 'http://selenium_firefox:4444/wd/hub',
desired_capabilities: Selenium::WebDriver::Remote::Capabilities.firefox(
opts: { args: ['-headless'] }
)
)
end

Capybara.javascript_driver = if ENV['CAPYBARA_JAVASCRIPT_DRIVER']
ENV['CAPYBARA_JAVASCRIPT_DRIVER'].to_sym
else
:selenium
# :headless_selenium_chrome_in_container
# :headless_selenium_firefox_in_container
# :selenium_firefox_in_container
end

Capybara.server_host = '0.0.0.0'

Capybara.server_port = 4000

Capybara.app_host = 'http://app:4000'

Capybara.always_include_port = true

if ENV['BROWSER']
DatabaseCleaner.strategy = :truncation
Capybara.default_driver = ENV['BROWSER'].to_sym
Expand Down
15 changes: 15 additions & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
require 'paper_trail/frameworks/rspec'
require 'factory_bot'
require 'faker'
require_relative './support/capybara.rb'

# Requires supporting ruby files with custom matchers and macros, etc, in
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
Expand Down Expand Up @@ -48,6 +49,20 @@
# arbitrary gems may also be filtered via:
# config.filter_gems_from_backtrace("gem name")
config.include FactoryBot::Syntax::Methods

config.before(:each, type: :system, js: true) do
driven_by :selenium_chrome_in_container

Capybara.server_host = '0.0.0.0'

Capybara.server_port = 4000

Capybara.app_host = 'http://web:4000'
end

config.before(:each, type: :system) do
driven_by :rack_test
end
end

Shoulda::Matchers.configure do |config|
Expand Down
17 changes: 17 additions & 0 deletions spec/support/capybara.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'selenium/webdriver'

Capybara.register_driver :selenium_chrome_in_container do |app|
Capybara::Selenium::Driver.new app,
browser: :remote,
url: 'http://selenium_chrome:4444/wd/hub',
desired_capabilities: :chrome
end

Capybara.register_driver :headless_selenium_chrome_in_container do |app|
Capybara::Selenium::Driver.new app,
browser: :remote,
url: 'http://selenium_chrome:4444/wd/hub',
desired_capabilities: Selenium::WebDriver::Remote::Capabilities.chrome(
chromeOptions: { args: %w[headless disable-gpu] }
)
end

0 comments on commit ddef339

Please sign in to comment.