|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.configure do |config| |
| 4 | + config.before(:suite) do |
| 5 | + # Adjust the save path for screenshots. These are also uploaded as artifacts in GitHub actions. |
| 6 | + Capybara.save_path = Rails.root.join('tmp/screenshots') |
| 7 | + |
| 8 | + # Clean up the save path with old assets before running the test suite. |
| 9 | + FileUtils.rm_rf(Dir.glob("#{Capybara.save_path}/*")) |
| 10 | + end |
| 11 | + |
| 12 | + config.before(:each, type: :system) do |
| 13 | + # rack_test by default, for performance |
| 14 | + driven_by :rack_test |
| 15 | + end |
| 16 | + |
| 17 | + config.before(:each, type: :system, js: true) do |
| 18 | + # Selenium when we need JavaScript |
| 19 | + driven_by :selenium, using: :"#{display_mode}#{browser}", &send(:"#{browser}_options") |
| 20 | + end |
| 21 | + |
| 22 | + private |
| 23 | + |
| 24 | + def mac_os? |
| 25 | + Gem::Platform.local.os == 'darwin' |
| 26 | + end |
| 27 | + |
| 28 | + def browser |
| 29 | + return @browser if defined?(@browser) |
| 30 | + # Vagrant currently supports Firefox only. |
| 31 | + return @browser = 'firefox' if ENV.fetch('USER', nil) == 'vagrant' |
| 32 | + |
| 33 | + case ENV.fetch('BROWSER', 'chrome') |
| 34 | + when /^chrom(e|ium)$/i |
| 35 | + @browser = 'chrome' |
| 36 | + when /^(firefox|iceweasel|gecko)$/i |
| 37 | + @browser = 'firefox' |
| 38 | + end |
| 39 | + end |
| 40 | + |
| 41 | + def display_mode |
| 42 | + return @display_mode if defined?(@display_mode) |
| 43 | + |
| 44 | + if enabled?('CI') || enabled?('HEADLESS') || ENV.fetch('USER', nil) == 'vagrant' |
| 45 | + @display_mode = 'headless_' |
| 46 | + else |
| 47 | + @display_mode = '' |
| 48 | + end |
| 49 | + end |
| 50 | + |
| 51 | + def enabled?(env_key, default_value = '0') |
| 52 | + %w[0 n no off false f].exclude?(ENV.fetch(env_key, default_value).downcase) |
| 53 | + end |
| 54 | + |
| 55 | + def chrome_options |
| 56 | + lambda {|driver_options| |
| 57 | + # Since Chrome 127, the browser will prompt the user to choose a search engine. |
| 58 | + driver_options.add_argument('disable-search-engine-choice-screen') |
| 59 | + # We need to sync the browser's locale with the one used by the testing framework. |
| 60 | + driver_options.add_argument("accept-lang=#{I18n.locale}") |
| 61 | + } |
| 62 | + end |
| 63 | + |
| 64 | + def firefox_options |
| 65 | + lambda {|driver_options| |
| 66 | + # We need to sync the browser's locale with the one used by the testing framework. |
| 67 | + driver_options.add_preference('intl.accept_languages', I18n.locale) |
| 68 | + } |
| 69 | + end |
| 70 | +end |
0 commit comments