|
36 | 36 | Capybara::Selenium::Driver.new(app, browser: :chrome, options: options, service: service) |
37 | 37 | end |
38 | 38 |
|
| 39 | +# Capybara 3.40.0 clears storage with `origin: "*"`, which recent Chrome |
| 40 | +# rejects. Clear the app server's origin instead. The version guard turns this |
| 41 | +# off once a Capybara release with the fix is installed. |
| 42 | +# https://github.com/teamcapybara/capybara/pull/2867 |
| 43 | +# https://github.com/teamcapybara/capybara/pull/2868 |
| 44 | +if Gem::Version.new(Capybara::VERSION) <= Gem::Version.new("3.40.0") |
| 45 | + Capybara::Selenium::Driver::ChromeDriver.class_eval do |
| 46 | + # Mirrors Capybara's upstream reset!, so keep its shape rather than splitting it up. |
| 47 | + # :reek:TooManyStatements |
| 48 | + def reset! |
| 49 | + # Use instance variable directly so we avoid starting the browser just to reset the session |
| 50 | + return unless @browser |
| 51 | + |
| 52 | + switch_to_window(window_handles.first) |
| 53 | + window_handles.slice(1..).each { |win| close_window(win) } |
| 54 | + return super if chromedriver_version < 73 |
| 55 | + |
| 56 | + timer = Capybara::Helpers.timer(expire_in: 10) |
| 57 | + begin |
| 58 | + clear_storage unless uniform_storage_clear? |
| 59 | + @browser.navigate.to('about:blank') |
| 60 | + wait_for_empty_page(timer) |
| 61 | + rescue *unhandled_alert_errors |
| 62 | + accept_unhandled_reset_alert |
| 63 | + retry |
| 64 | + end |
| 65 | + |
| 66 | + # base_url includes the random port Capybara's server listens on, which |
| 67 | + # Storage.clearDataForOrigin needs to match the app's storage. |
| 68 | + origin = Capybara.current_session.server&.base_url |
| 69 | + execute_cdp('Storage.clearDataForOrigin', origin: origin, storageTypes: storage_types_to_clear) if origin |
| 70 | + end |
| 71 | + end |
| 72 | +end |
| 73 | + |
39 | 74 | class ApplicationSystemTestCase < ActionDispatch::SystemTestCase |
40 | 75 | driven_by :headless_chromium |
41 | 76 | end |
0 commit comments