From 1d344d5ec970714dc874b8994b7bfea374d5307c Mon Sep 17 00:00:00 2001 From: Martin Gruner Date: Wed, 16 Mar 2022 09:22:36 +0100 Subject: [PATCH] Maintenance: Make headless mode configurable. --- .gitlab/ci/base.yml | 2 + spec/support/capybara/selenium_driver.rb | 18 ++++- test/browser_test_helper.rb | 86 ++++++++++-------------- 3 files changed, 53 insertions(+), 53 deletions(-) diff --git a/.gitlab/ci/base.yml b/.gitlab/ci/base.yml index 84e9b57f8..87f150962 100644 --- a/.gitlab/ci/base.yml +++ b/.gitlab/ci/base.yml @@ -22,6 +22,7 @@ variables: REMOTE_URL: "http://selenium-firefox:4444/wd/hub" BROWSER: "firefox" + BROWSER_HEADLESS: "true" START_XVFB: "false" # not needed for headless mode SE_NODE_MAX_SESSIONS: "5" SE_NODE_OVERRIDE_MAX_SESSIONS: "true" @@ -30,6 +31,7 @@ variables: REMOTE_URL: "http://selenium-chrome:4444/wd/hub" BROWSER: chrome + BROWSER_HEADLESS: "true" START_XVFB: "false" # not needed for headless mode SE_NODE_MAX_SESSIONS: "5" SE_NODE_OVERRIDE_MAX_SESSIONS: "true" diff --git a/spec/support/capybara/selenium_driver.rb b/spec/support/capybara/selenium_driver.rb index 27f1f7010..28b5efc78 100644 --- a/spec/support/capybara/selenium_driver.rb +++ b/spec/support/capybara/selenium_driver.rb @@ -7,7 +7,7 @@ Capybara.register_driver(:zammad_chrome) do |app| # Turn on browser logs - options = Selenium::WebDriver::Chrome::Options.new( + chrome_options = Selenium::WebDriver::Chrome::Options.new( logging_prefs: { browser: 'ALL' }, @@ -21,12 +21,19 @@ Capybara.register_driver(:zammad_chrome) do |app| options = { browser: :chrome, - options: options + options: chrome_options } if ENV['REMOTE_URL'].present? options[:browser] = :remote options[:url] = ENV['REMOTE_URL'] + options[:http_client] = Selenium::WebDriver::Remote::Http::Default.new( + open_timeout: 120, + read_timeout: 120 + ) + end + + if ENV['BROWSER_HEADLESS'].present? options[:options].headless! end @@ -54,6 +61,13 @@ Capybara.register_driver(:zammad_firefox) do |app| if ENV['REMOTE_URL'].present? options[:browser] = :remote options[:url] = ENV['REMOTE_URL'] + options[:http_client] = Selenium::WebDriver::Remote::Http::Default.new( + open_timeout: 120, + read_timeout: 120 + ) + end + + if ENV['BROWSER_HEADLESS'].present? options[:options].headless! end diff --git a/test/browser_test_helper.rb b/test/browser_test_helper.rb index 3e14b3a66..cbc309226 100644 --- a/test/browser_test_helper.rb +++ b/test/browser_test_helper.rb @@ -41,30 +41,6 @@ class TestCase < ActiveSupport::TestCase ENV['BROWSER'] || 'firefox' end - def profile - browser_profile = nil - case browser - when 'firefox' - browser_profile = Selenium::WebDriver::Firefox::Profile.new - - browser_profile['intl.locale.matchOS'] = false - browser_profile['intl.accept_languages'] = 'en-US' - browser_profile['general.useragent.locale'] = 'en-US' - # currently console log not working for firefox - # https://github.com/SeleniumHQ/selenium/issues/1161 - # browser_profile['loggingPref'] = { browser: :all } - when 'chrome' - - # profile are only working on remote selenium - if ENV['REMOTE_URL'] - browser_profile = Selenium::WebDriver::Chrome::Profile.new - browser_profile['intl.accept_languages'] = 'en' - browser_profile['loggingPref'] = { browser: :all } - end - end - browser_profile - end - def browser_support_cookies if browser.match?(%r{(internet_explorer|ie)}i) return false @@ -85,13 +61,41 @@ class TestCase < ActiveSupport::TestCase Socket.ip_address_list.detect(&:ipv4_private?).ip_address end + def browser_options + case browser + when 'firefox' + profile = Selenium::WebDriver::Firefox::Profile.new + profile['intl.locale.matchOS'] = false + profile['intl.accept_languages'] = 'en-US' + profile['general.useragent.locale'] = 'en-US' + profile['permissions.default.desktop-notification'] = 1 # ALLOW notifications + + options = Selenium::WebDriver::Firefox::Options.new( + profile: profile + ) + when 'chrome' + options = Selenium::WebDriver::Chrome::Options.new( + logging_prefs: { + browser: 'ALL' + }, + prefs: { + 'intl.accept_languages' => 'en-US', + 'profile.default_content_setting_values.notifications' => 1, # ALLOW notifications + }, + # Disable the "Chrome is controlled by automation software" info bar. + excludeSwitches: ['enable-automation'], + ) + end + if ENV['BROWSER_HEADLESS'].present? + options.headless! + end + options + end + def browser_instance @browsers ||= {} if ENV['REMOTE_URL'].blank? - params = { - profile: profile, - } - local_browser = Selenium::WebDriver.for(browser.to_sym, params) + local_browser = Selenium::WebDriver.for(browser.to_sym, options: browser_options) @browsers[local_browser.hash] = local_browser browser_instance_preferences(local_browser) return local_browser @@ -113,36 +117,16 @@ class TestCase < ActiveSupport::TestCase end def browser_instance_remote - caps = Selenium::WebDriver::Remote::Capabilities.send(browser) - if ENV['BROWSER_OS'] - caps.platform = ENV['BROWSER_OS'] - end - if ENV['BROWSER_VERSION'] - caps.version = ENV['BROWSER_VERSION'] - end - http_client = Selenium::WebDriver::Remote::Http::Default.new( open_timeout: 120, read_timeout: 120 ) - case browser - when 'firefox' - options = Selenium::WebDriver::Firefox::Options.new - options.headless! - when 'chrome' - options = Selenium::WebDriver::Chrome::Options.new( - # Disable the "Chrome is controlled by automation software" info bar. - excludeSwitches: ['enable-automation'], - ) - options.headless! - end local_browser = Selenium::WebDriver.for( :remote, - url: ENV['REMOTE_URL'], - desired_capabilities: caps, - http_client: http_client, - options: options, + url: ENV['REMOTE_URL'], + http_client: http_client, + options: browser_options, ) @browsers[local_browser.hash] = local_browser browser_instance_preferences(local_browser)