Преглед на файлове

Configure remote selenium setup

Andrew Swistak преди 6 години
родител
ревизия
a810d60bdc
променени са 2 файла, в които са добавени 69 реда и са изтрити 16 реда
  1. 57 1
      spec/capybara_helper.rb
  2. 12 15
      spec/rails_helper.rb

+ 57 - 1
spec/capybara_helper.rb

@@ -4,9 +4,44 @@ require 'selenium/webdriver'
 Capybara.default_max_wait_time = 10
 Capybara.server = :puma, {Silent: true}
 
+# Check if Capybara should be running against a remote Selenium instance
+def remote_selenium?
+  (ENV['SELENIUM_FIREFOX_HOST'] || ENV['SELENIUM_CHROME_HOST']) && ENV['SELENIUM_PORT']
+end
+
+# Condtionally swap driver setup, defaults to `:headless_firefox`
+def selenium_driver
+  return @selenium_driver if @selenium_driver
+
+  selenium_driver = []
+  if remote_selenium?
+    selenium_driver.push('remote')
+  elsif ENV['CAPYBARA_GUI_DRIVER'] != 'true'
+    selenium_driver.push('headless')
+  end
+
+  case ENV['CAPYBARA_JS_DRIVER']
+  when 'chrome'
+    selenium_driver.push('chrome')
+  when 'firefox'
+    selenium_driver.push('firefox')
+  else
+    selenium_driver.push('firefox')
+  end
+
+  @selenium_driver = selenium_driver.join('_').to_sym
+end
+
+# Setting app_host and default_host here have no bearing on test runners.
+# These must be set in a `before` block for each test, i.e. in rails_helper.rb
+# Capybara.app_host = "http://#{ip}:#{app_host_port}"
+# Capybara.default_host = "http://#{ip}:#{app_host_port}"
+
+Capybara.server_port = ENV['APP_HOST_PORT'] if ENV['APP_HOST_PORT']
+Capybara.server_host = '0.0.0.0' if remote_selenium?
+
 # Headless firefox and chrome configuration. Useful for CI matrices.
 # See https://gist.github.com/bbonamin/4b01be9ed5dd1bdaf909462ff4fdca95
-
 Capybara.register_driver :firefox do |app|
   Capybara::Selenium::Driver.new(app, browser: :firefox)
 end
@@ -50,3 +85,24 @@ Capybara.register_driver :headless_chrome do |app|
 
   driver
 end
+
+Capybara.register_driver :remote_firefox do |app|
+  Capybara::Selenium::Driver.new(
+    app,
+    browser: :remote,
+    url: "http://#{ENV['SELENIUM_FIREFOX_HOST']}:#{ENV['SELENIUM_PORT']}/wd/hub",
+    desired_capabilities: Selenium::WebDriver::Remote::Capabilities.firefox,
+  )
+end
+
+Capybara.register_driver :remote_chrome do |app|
+  args = ['--no-default-browser-check', '--start-maximized']
+  caps = Selenium::WebDriver::Remote::Capabilities.chrome('chromeOptions' => {'args' => args})
+
+  Capybara::Selenium::Driver.new(
+    app,
+    browser: :remote,
+    url: "http://#{ENV['SELENIUM_CHROME_HOST']}:#{ENV['SELENIUM_PORT']}/wd/hub",
+    desired_capabilities: caps,
+  )
+end

+ 12 - 15
spec/rails_helper.rb

@@ -2,14 +2,13 @@
 
 # This file is copied to spec/ when you run 'rails generate rspec:install'
 require 'spec_helper'
+require 'capybara_helper'
 ENV['RAILS_ENV'] ||= 'test'
 require File.expand_path('../config/environment', __dir__)
 # Prevent database truncation if the environment is production
 abort('The Rails environment is running in production mode!') if Rails.env.production?
 require 'rspec/rails'
 # Add additional requires below this line. Rails is not loaded until this point!
-require 'spec_helper'
-require 'capybara_helper'
 
 # Requires supporting ruby files with custom matchers and macros, etc, in
 # spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
@@ -66,20 +65,18 @@ RSpec.configure do |config|
 
   config.include FactoryBot::Syntax::Methods
 
-  # Condtionally swap driver setup, defaults to `:headless_firefox`
-  driver = []
-  driver.push('headless') unless ENV['CAPYBARA_GUI_DRIVER'] == 'true'
+  config.before(:each, type: :system) do
+    # Enforce app host location by setting on every spec. Yes, it is strange
+    # that re-configuring on every system test is a necessity.
+    if (app_host_port = ENV['APP_HOST_PORT'])
+      Capybara.configure do |capybara_config|
+        host_ip = Socket.getaddrinfo(Socket.gethostname, 'echo', Socket::AF_INET)[0][3]
 
-  if ENV['CAPYBARA_JS_DRIVER'] == 'chrome'
-    driver.push('chrome')
-  elsif ENV['CAPYBARA_JS_DRIVER'] == 'firefox'
-    driver.push('firefox')
-  else
-    driver.push('firefox')
-  end
-  driver = driver.join('_').to_sym
+        capybara_config.app_host = "http://#{host_ip}:#{app_host_port}"
+        capybara_config.default_host = "http://#{host_ip}:#{app_host_port}"
+      end
+    end
 
-  config.before(:each, type: :system) do
-    driven_by driver
+    driven_by selenium_driver
   end
 end