Skip to content

Commit

Permalink
Fixes #36978 - Add possibility to use remote webdriver
Browse files Browse the repository at this point in the history
  • Loading branch information
dosas committed Dec 21, 2023
1 parent b793575 commit 40b0f2a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
40 changes: 32 additions & 8 deletions test/integration_test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,42 @@
end

Selenium::WebDriver::Chrome::Service.driver_path = ENV['TESTDRIVER_PATH'] || Foreman::Util.which('chromedriver', Rails.root.join('node_modules', '.bin'))
Capybara.register_driver :selenium_chrome do |app|

ShowMeTheCookies.register_adapter(:selenium_chrome_remote, ShowMeTheCookies::SeleniumChrome)

Capybara.register_driver :selenium_chrome_remote do |app|
options = Selenium::WebDriver::Chrome::Options.new
options.args << '--disable-gpu'
options.args << '--no-sandbox'
options.args << '--window-size=1024,768'
options.args << '--headless' unless ENV['DEBUG_JS_TEST'] == '1'
Capybara::Selenium::Driver.new(app, browser: :chrome, options: options)
additional_options = ENV.fetch('ADDITIONAL_CHROME_OPTIONS', '')
additional_options.split(";").each do |add_opt|
options.args << add_opt
end
selenium_remote_host = ENV.fetch('SELENIUM_REMOTE_HOST', nil)
selenium_remote_port = ENV.fetch('SELENIUM_REMOTE_PORT', 4444)
Capybara::Selenium::Driver.new(
app,
browser: :remote,
url: "http://#{selenium_remote_host}:#{selenium_remote_port}/wd/hub",
options: options)
end

Capybara.configure do |config|
config.javascript_driver = ENV["JS_TEST_DRIVER"]&.to_sym || :selenium_chrome
config.default_max_wait_time = 20
config.javascript_driver = ENV.fetch("JS_TEST_DRIVER") { ENV['DEBUG_JS_TEST'] ? :selenium_chrome : :selenium_chrome_headless }.to_sym
config.default_max_wait_time = 20
config.enable_aria_label = true
if ENV.fetch("JS_TEST_DRIVER", nil) == :selenium_chrome_remote.to_s
app_host = ENV.fetch('APP_SERVER_HOST') do
Socket.ip_address_list
.find(&:ipv4_private?)
.ip_address
end
app_port = ENV.fetch('APP_SERVER_PORT', "8080")
config.server_port = app_port
# application server
config.server_host = "0.0.0.0"
# address used by selenium host to connect to application server
config.app_host = "http://#{app_host}:#{app_port}"
end
end

class ActionDispatch::IntegrationTest
Expand Down Expand Up @@ -299,7 +323,7 @@ def database_cleaner_strategy
end

def login_admin
visit('/users/login') if Capybara.current_driver == :selenium_chrome
visit('/users/login')
SSO.register_method(TestSSO)
set_request_user(:admin)
end
Expand Down
6 changes: 5 additions & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@
FactoryBot.use_parent_strategy = false

# Do not allow network connections and external processes
WebMock.disable_net_connect!(allow_localhost: true)
if ENV.fetch('SELENIUM_REMOTE_HOST', nil)
WebMock.disable_net_connect!(allow_localhost: true, allow: ENV['SELENIUM_REMOTE_HOST'])
else
WebMock.disable_net_connect!(allow_localhost: true)
end

# Configure shoulda
Shoulda::Matchers.configure do |config|
Expand Down

0 comments on commit 40b0f2a

Please sign in to comment.