selenium_driver.rb 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # Copyright (C) 2012-2022 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. # Turn on browser logs
  7. chrome_options = Selenium::WebDriver::Chrome::Options.new(
  8. logging_prefs: {
  9. browser: 'ALL'
  10. },
  11. prefs: {
  12. 'intl.accept_languages' => 'en-US',
  13. 'profile.default_content_setting_values.notifications' => 1, # ALLOW notifications
  14. },
  15. args: %w[--enable-logging --v=1],
  16. # Disable the "Chrome is controlled by automation software" info bar.
  17. excludeSwitches: ['enable-automation'],
  18. )
  19. driver_args = {
  20. browser: :chrome,
  21. capabilities: chrome_options
  22. }
  23. if ENV['REMOTE_URL'].present?
  24. driver_args[:browser] = :remote
  25. driver_args[:url] = ENV['REMOTE_URL']
  26. driver_args[:http_client] = Selenium::WebDriver::Remote::Http::Default.new(
  27. open_timeout: 120,
  28. read_timeout: 120
  29. )
  30. end
  31. if ENV['BROWSER_HEADLESS'].present?
  32. driver_args[:capabilities].headless!
  33. end
  34. ENV['FAKE_SELENIUM_LOGIN_USER_ID'] = nil
  35. Capybara::Selenium::Driver.new(app, **driver_args).tap do |driver|
  36. # Selenium 4 installs a default file_detector which finds wrong files/directories such as zammad/test.
  37. driver.browser.file_detector = nil if ENV['REMOTE_URL'].present?
  38. end
  39. end
  40. Capybara.register_driver(:zammad_firefox) do |app|
  41. profile = Selenium::WebDriver::Firefox::Profile.new
  42. profile['intl.locale.matchOS'] = false
  43. profile['intl.accept_languages'] = 'en-US'
  44. profile['general.useragent.locale'] = 'en-US'
  45. profile['permissions.default.desktop-notification'] = 1 # ALLOW notifications
  46. driver_args = {
  47. browser: :firefox,
  48. capabilities: Selenium::WebDriver::Firefox::Options.new(profile: profile),
  49. }
  50. if ENV['REMOTE_URL'].present?
  51. driver_args[:browser] = :remote
  52. driver_args[:url] = ENV['REMOTE_URL']
  53. driver_args[:http_client] = Selenium::WebDriver::Remote::Http::Default.new(
  54. open_timeout: 120,
  55. read_timeout: 120
  56. )
  57. end
  58. if ENV['BROWSER_HEADLESS'].present?
  59. driver_args[:capabilities].headless!
  60. end
  61. ENV['FAKE_SELENIUM_LOGIN_USER_ID'] = nil
  62. Capybara::Selenium::Driver.new(app, **driver_args).tap do |driver|
  63. # Selenium 4 installs a default file_detector which finds wrong files/directories such as zammad/test.
  64. driver.browser.file_detector = nil if ENV['REMOTE_URL'].present?
  65. end
  66. end