selenium_driver.rb 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. ENV['FAKE_SELENIUM_LOGIN_PENDING'] = nil
  36. Capybara::Selenium::Driver.new(app, **driver_args).tap do |driver|
  37. # Selenium 4 installs a default file_detector which finds wrong files/directories such as zammad/test.
  38. driver.browser.file_detector = nil if ENV['REMOTE_URL'].present?
  39. end
  40. end
  41. Capybara.register_driver(:zammad_firefox) do |app|
  42. profile = Selenium::WebDriver::Firefox::Profile.new
  43. profile['intl.locale.matchOS'] = false
  44. profile['intl.accept_languages'] = 'en-US'
  45. profile['general.useragent.locale'] = 'en-US'
  46. profile['permissions.default.desktop-notification'] = 1 # ALLOW notifications
  47. driver_args = {
  48. browser: :firefox,
  49. capabilities: Selenium::WebDriver::Firefox::Options.new(profile: profile),
  50. }
  51. if ENV['REMOTE_URL'].present?
  52. driver_args[:browser] = :remote
  53. driver_args[:url] = ENV['REMOTE_URL']
  54. driver_args[:http_client] = Selenium::WebDriver::Remote::Http::Default.new(
  55. open_timeout: 120,
  56. read_timeout: 120
  57. )
  58. end
  59. if ENV['BROWSER_HEADLESS'].present?
  60. driver_args[:capabilities].headless!
  61. end
  62. ENV['FAKE_SELENIUM_LOGIN_USER_ID'] = nil
  63. ENV['FAKE_SELENIUM_LOGIN_PENDING'] = nil
  64. Capybara::Selenium::Driver.new(app, **driver_args).tap do |driver|
  65. # Selenium 4 installs a default file_detector which finds wrong files/directories such as zammad/test.
  66. driver.browser.file_detector = nil if ENV['REMOTE_URL'].present?
  67. end
  68. end