Maintenance: Make headless mode configurable.

This commit is contained in:
Martin Gruner 2022-03-16 09:22:36 +01:00
parent be44c36a02
commit 1d344d5ec9
3 changed files with 53 additions and 53 deletions

View file

@ -22,6 +22,7 @@
variables: variables:
REMOTE_URL: "http://selenium-firefox:4444/wd/hub" REMOTE_URL: "http://selenium-firefox:4444/wd/hub"
BROWSER: "firefox" BROWSER: "firefox"
BROWSER_HEADLESS: "true"
START_XVFB: "false" # not needed for headless mode START_XVFB: "false" # not needed for headless mode
SE_NODE_MAX_SESSIONS: "5" SE_NODE_MAX_SESSIONS: "5"
SE_NODE_OVERRIDE_MAX_SESSIONS: "true" SE_NODE_OVERRIDE_MAX_SESSIONS: "true"
@ -30,6 +31,7 @@
variables: variables:
REMOTE_URL: "http://selenium-chrome:4444/wd/hub" REMOTE_URL: "http://selenium-chrome:4444/wd/hub"
BROWSER: chrome BROWSER: chrome
BROWSER_HEADLESS: "true"
START_XVFB: "false" # not needed for headless mode START_XVFB: "false" # not needed for headless mode
SE_NODE_MAX_SESSIONS: "5" SE_NODE_MAX_SESSIONS: "5"
SE_NODE_OVERRIDE_MAX_SESSIONS: "true" SE_NODE_OVERRIDE_MAX_SESSIONS: "true"

View file

@ -7,7 +7,7 @@
Capybara.register_driver(:zammad_chrome) do |app| Capybara.register_driver(:zammad_chrome) do |app|
# Turn on browser logs # Turn on browser logs
options = Selenium::WebDriver::Chrome::Options.new( chrome_options = Selenium::WebDriver::Chrome::Options.new(
logging_prefs: { logging_prefs: {
browser: 'ALL' browser: 'ALL'
}, },
@ -21,12 +21,19 @@ Capybara.register_driver(:zammad_chrome) do |app|
options = { options = {
browser: :chrome, browser: :chrome,
options: options options: chrome_options
} }
if ENV['REMOTE_URL'].present? if ENV['REMOTE_URL'].present?
options[:browser] = :remote options[:browser] = :remote
options[:url] = ENV['REMOTE_URL'] 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! options[:options].headless!
end end
@ -54,6 +61,13 @@ Capybara.register_driver(:zammad_firefox) do |app|
if ENV['REMOTE_URL'].present? if ENV['REMOTE_URL'].present?
options[:browser] = :remote options[:browser] = :remote
options[:url] = ENV['REMOTE_URL'] 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! options[:options].headless!
end end

View file

@ -41,30 +41,6 @@ class TestCase < ActiveSupport::TestCase
ENV['BROWSER'] || 'firefox' ENV['BROWSER'] || 'firefox'
end 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 def browser_support_cookies
if browser.match?(%r{(internet_explorer|ie)}i) if browser.match?(%r{(internet_explorer|ie)}i)
return false return false
@ -85,13 +61,41 @@ class TestCase < ActiveSupport::TestCase
Socket.ip_address_list.detect(&:ipv4_private?).ip_address Socket.ip_address_list.detect(&:ipv4_private?).ip_address
end 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 def browser_instance
@browsers ||= {} @browsers ||= {}
if ENV['REMOTE_URL'].blank? if ENV['REMOTE_URL'].blank?
params = { local_browser = Selenium::WebDriver.for(browser.to_sym, options: browser_options)
profile: profile,
}
local_browser = Selenium::WebDriver.for(browser.to_sym, params)
@browsers[local_browser.hash] = local_browser @browsers[local_browser.hash] = local_browser
browser_instance_preferences(local_browser) browser_instance_preferences(local_browser)
return local_browser return local_browser
@ -113,36 +117,16 @@ class TestCase < ActiveSupport::TestCase
end end
def browser_instance_remote 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( http_client = Selenium::WebDriver::Remote::Http::Default.new(
open_timeout: 120, open_timeout: 120,
read_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( local_browser = Selenium::WebDriver.for(
:remote, :remote,
url: ENV['REMOTE_URL'], url: ENV['REMOTE_URL'],
desired_capabilities: caps,
http_client: http_client, http_client: http_client,
options: options, options: browser_options,
) )
@browsers[local_browser.hash] = local_browser @browsers[local_browser.hash] = local_browser
browser_instance_preferences(local_browser) browser_instance_preferences(local_browser)