From e3bc613cd2ab830b059bcc26ebf9f0d03fa9e312 Mon Sep 17 00:00:00 2001 From: Mark Sutton Date: Fri, 18 Dec 2020 13:37:10 +0000 Subject: [PATCH] Cukes via Docker - selenium (Chrome in a container) --- docker-compose.yml | 15 ++++++++++++ features/support/env.rb | 52 ++++++++++++++++++++++++++++++++++++---- spec/rails_helper.rb | 15 ++++++++++++ spec/support/capybara.rb | 17 +++++++++++++ 4 files changed, 94 insertions(+), 5 deletions(-) create mode 100644 spec/support/capybara.rb diff --git a/docker-compose.yml b/docker-compose.yml index bd05cd9b..74067bf5 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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 diff --git a/features/support/env.rb b/features/support/env.rb index 68dd53e9..b3f0d0b0 100644 --- a/features/support/env.rb +++ b/features/support/env.rb @@ -87,11 +87,53 @@ ) end -Capybara.javascript_driver = if ENV['CAPYBARA_JAVASCRIPT_DRIVER'] - ENV['CAPYBARA_JAVASCRIPT_DRIVER'].to_sym - else - :selenium - 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 + +if ENV['CAPYBARA_JAVASCRIPT_DRIVER'] + Capybara.javascript_driver = ENV['CAPYBARA_JAVASCRIPT_DRIVER'].to_sym +else + Capybara.javascript_driver = :selenium + # Capybara.javascript_driver = :headless_selenium_chrome_in_container + # Capybara.javascript_driver = :headless_selenium_firefox_in_container + # Capybara.javascript_driver = :selenium_firefox_in_container + + Capybara.server_host = '0.0.0.0' + + Capybara.server_port = 4000 + + Capybara.app_host = 'http://app:4000' + + Capybara.always_include_port = true +end if ENV['BROWSER'] DatabaseCleaner.strategy = :truncation diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index f7aeef54..48f07015 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -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 @@ -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://app:4000' + end + + config.before(:each, type: :system) do + driven_by :rack_test + end end Shoulda::Matchers.configure do |config| diff --git a/spec/support/capybara.rb b/spec/support/capybara.rb new file mode 100644 index 00000000..3b4ab265 --- /dev/null +++ b/spec/support/capybara.rb @@ -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