selenium_driver.rb 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. # This file registers the custom Zammad chrome and firefox drivers.
  3. # The options check if a REMOTE_URL ENV is given and change the
  4. # configurations accordingly.
  5. Capybara.register_driver(:zammad_chrome) do |app|
  6. build_chrome_driver(app)
  7. end
  8. Capybara.register_driver(:zammad_chrome_mobile) do |app|
  9. # User agent for Chrome Beta on Pixel 7 (Android 13).
  10. build_chrome_driver(app, user_agent: 'Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Mobile Safari/537.36')
  11. end
  12. Capybara.register_driver(:zammad_firefox) do |app|
  13. build_firefox_driver(app)
  14. end
  15. Capybara.register_driver(:zammad_firefox_mobile) do |app|
  16. # User agent for Firefox on Pixel 7 (Android 13).
  17. build_firefox_driver(app, user_agent: 'Mozilla/5.0 (Android 13; Mobile; rv:109.0) Gecko/112.0 Firefox/112.0')
  18. end
  19. private
  20. def build_chrome_driver(app, user_agent: nil)
  21. # Turn on browser logs
  22. chrome_options = Selenium::WebDriver::Chrome::Options.new(
  23. logging_prefs: {
  24. browser: 'ALL'
  25. },
  26. prefs: {
  27. 'intl.accept_languages' => 'en-US',
  28. 'profile.default_content_setting_values.notifications' => 1, # ALLOW notifications
  29. },
  30. # Disable shared memory usage as it does not really provide a performance gain but cause resource limit issues in CI.
  31. args: %w[--enable-logging --v=1 --disable-dev-shm-usage --disable-search-engine-choice-screen],
  32. # Disable the "Chrome is being controlled by automated test software." info bar.
  33. exclude_switches: ['enable-automation'],
  34. )
  35. driver_args = {
  36. browser: :chrome,
  37. options: chrome_options
  38. }
  39. if ENV['REMOTE_URL'].present?
  40. driver_args[:browser] = :remote
  41. driver_args[:url] = ENV['REMOTE_URL']
  42. driver_args[:http_client] = Selenium::WebDriver::Remote::Http::Default.new(
  43. open_timeout: 120,
  44. read_timeout: 120
  45. )
  46. end
  47. if ENV['BROWSER_HEADLESS'].present?
  48. driver_args[:options].add_argument '--headless=new' # native headless for v109+
  49. end
  50. if user_agent.present?
  51. driver_args[:options].add_argument "--user-agent=\"#{user_agent}\""
  52. end
  53. driver_args[:options].add_argument '--allow-insecure-localhost'
  54. driver_args[:options].add_argument '--ignore-certificate-errors'
  55. ENV['FAKE_SELENIUM_LOGIN_USER_ID'] = nil
  56. ENV['FAKE_SELENIUM_LOGIN_PENDING'] = nil
  57. Capybara::Selenium::Driver.new(app, **driver_args).tap do |driver|
  58. # Selenium 4 installs a default file_detector which finds wrong files/directories such as zammad/test.
  59. driver.browser.file_detector = nil if ENV['REMOTE_URL'].present?
  60. end
  61. end
  62. def build_firefox_driver(app, user_agent: nil)
  63. profile = Selenium::WebDriver::Firefox::Profile.new
  64. profile['intl.locale.matchOS'] = false
  65. profile['intl.accept_languages'] = 'en-US'
  66. profile['general.useragent.locale'] = 'en-US'
  67. profile['permissions.default.desktop-notification'] = 1 # ALLOW notifications
  68. driver_args = {
  69. browser: :firefox,
  70. options: Selenium::WebDriver::Firefox::Options.new(profile: profile),
  71. }
  72. if ENV['REMOTE_URL'].present?
  73. driver_args[:browser] = :remote
  74. driver_args[:url] = ENV['REMOTE_URL']
  75. driver_args[:http_client] = Selenium::WebDriver::Remote::Http::Default.new(
  76. open_timeout: 120,
  77. read_timeout: 120
  78. )
  79. end
  80. if ENV['BROWSER_HEADLESS'].present?
  81. driver_args[:options].add_argument '-headless'
  82. end
  83. if user_agent.present?
  84. driver_args[:options].add_preference 'general.useragent.override', user_agent
  85. end
  86. ENV['FAKE_SELENIUM_LOGIN_USER_ID'] = nil
  87. ENV['FAKE_SELENIUM_LOGIN_PENDING'] = nil
  88. Capybara::Selenium::Driver.new(app, **driver_args).tap do |driver|
  89. # Selenium 4 installs a default file_detector which finds wrong files/directories such as zammad/test.
  90. driver.browser.file_detector = nil if ENV['REMOTE_URL'].present?
  91. end
  92. end