driven_by.rb 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require_relative 'set_up'
  3. CAPYBARA_PORT = ENV['CAPYBARA_PORT'] || 3001
  4. CAPYBARA_HOSTNAME = ENV['CI'].present? ? 'build' : 'localhost'
  5. RSpec.configure do |config|
  6. # Provide SSL certificates with the help of the 'localhost' gem.
  7. localhost_autority = Localhost::Authority.fetch(CAPYBARA_HOSTNAME)
  8. Capybara.register_server :puma_wrapper do |app, port, host, **_options|
  9. # Start a silenced Puma as application server.
  10. Capybara.servers[:puma].call(app, port, host, Silent: true, Host: "ssl://0.0.0.0?key=#{localhost_autority.key_path}&cert=#{localhost_autority.certificate_path}", Threads: '0:16')
  11. end
  12. Capybara.configure do |capybara_config|
  13. capybara_config.server = :puma_wrapper
  14. capybara_config.server_port = CAPYBARA_PORT
  15. capybara_config.save_path = 'tmp/screenshots'
  16. # See https://docs.gitlab.com/runner/executors/docker.html#create-a-network-for-each-job
  17. capybara_config.app_host = "https://#{CAPYBARA_HOSTNAME}"
  18. end
  19. config.before(:each, type: :system) do |example|
  20. Setting.set('http_type', 'https')
  21. Setting.set('fqdn', "#{CAPYBARA_HOSTNAME}:#{CAPYBARA_PORT}")
  22. browser_name = ENV.fetch('BROWSER', 'firefox')
  23. # If mobile user agent was requested by the example,
  24. # use an appropriate driver (e.g. zammad_chrome_mobile).
  25. if example.metadata[:mobile_user_agent].present?
  26. browser_name += '_mobile'
  27. # End the Capybara browser session whenever the user agent is to be mocked,
  28. # in order to get a fresh session for subsequent examples.
  29. Capybara.send(:session_pool).reverse_each do |_mode, session|
  30. session.quit
  31. end
  32. end
  33. # set custom Zammad driver (e.g. zammad_chrome) for special
  34. # functionalities and CI requirements
  35. driven_by(:"zammad_#{browser_name}")
  36. screen_size = example.metadata[:screen_size] || case example.metadata[:app]
  37. when :mobile
  38. :mobile
  39. when :desktop_view
  40. :desktop_view
  41. else
  42. :desktop
  43. end
  44. case screen_size
  45. when :mobile
  46. browser_width = 390
  47. browser_height = 844
  48. when :tablet
  49. browser_width = 1020
  50. browser_height = 760
  51. else # :desktop
  52. browser_width = 1520
  53. browser_height = 1000
  54. end
  55. page.driver.browser.manage.window.resize_to(browser_width, browser_height)
  56. end
  57. capybara_examples_performed = 0
  58. config.after(:each, type: :system) do |example|
  59. capybara_examples_performed += 1
  60. # End the main capybara session only from time to time, to speed up tests and make
  61. # sure memory consumption does not rise too much.
  62. # Also end the session whenever the user agent was mocked, in order to get a fresh
  63. # session for subsequent examples.
  64. # Make sure additional sessions (from using_sessions) are always ended
  65. # after every test and not kept alive. Selenium will automatically close
  66. # idle sessions which can cause 404 errors later.
  67. # (see https://github.com/teamcapybara/capybara/issues/2237)
  68. Capybara.send(:session_pool).reverse_each do |_mode, session|
  69. if !session.eql?(Capybara.current_session) || (capybara_examples_performed % 100).zero? || example.metadata[:mobile_user_agent]
  70. session.quit
  71. end
  72. end
  73. end
  74. retry_exceptions = [
  75. Net::OpenTimeout,
  76. Net::ReadTimeout,
  77. Selenium::WebDriver::Error::InvalidArgumentError,
  78. Selenium::WebDriver::Error::SessionNotCreatedError,
  79. Selenium::WebDriver::Error::UnknownError,
  80. ].freeze
  81. config.around(:each, type: :system) do |example|
  82. use_vcr = example.metadata.fetch(:use_vcr, false)
  83. # WebMock makes it impossible to have persistent http connections to Selenium,
  84. # which may cause overhead and Net::OpenTimeout errors.
  85. WebMock.disable! if !use_vcr
  86. # rspec-retry
  87. example.run_with_retry retry: 3, exceptions_to_retry: retry_exceptions
  88. WebMock.enable! if !use_vcr
  89. end
  90. end