From cbc45e334d6fe042a3bbbb6704f986218f7ff9e1 Mon Sep 17 00:00:00 2001 From: Tim Fischbach Date: Tue, 24 Nov 2026 14:41:19 +0100 Subject: [PATCH] Work around Chrome 158 incompatibilities in specs Chrome 158 rejects the wildcard origin Capybara uses to clear storage between examples, making every js spec error in its after hook. Fall back to clearing cookies and the current origin's local storage until teamcapybara/capybara#2868 is released. Chrome 158 also logs a permissions policy violation when jQuery 1.x registers its unload handler inside iframes. The handler only matters for old IE, so ignore the message instead of failing specs. --- .../pageflow/support/config/capybara.rb | 7 +++- .../config/capybara/chrome_reset_patch.rb | 38 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 spec/support/pageflow/support/config/capybara/chrome_reset_patch.rb diff --git a/spec/support/pageflow/support/config/capybara.rb b/spec/support/pageflow/support/config/capybara.rb index f820f1e933..555a50882d 100644 --- a/spec/support/pageflow/support/config/capybara.rb +++ b/spec/support/pageflow/support/config/capybara.rb @@ -3,6 +3,7 @@ require 'capybara/chromedriver/logger' require 'pageflow/support/config/capybara/selenium_error_patch' +require 'pageflow/support/config/capybara/chrome_reset_patch' Capybara.register_driver :selenium_chrome_headless_no_sandbox do |app| options = Selenium::WebDriver::Options.chrome( @@ -45,7 +46,11 @@ # Ignore failure of debounced request to refresh partials while db # has already been cleaned - /partials - Failed to load resource: the server responded with a status of 401/ + /partials - Failed to load resource: the server responded with a status of 401/, + + # Logged by Chrome 158+ when jQuery 1.x registers an unload handler + # inside iframes + /Permissions policy violation: unload is not allowed in this document/ ] RSpec.configure do |config| diff --git a/spec/support/pageflow/support/config/capybara/chrome_reset_patch.rb b/spec/support/pageflow/support/config/capybara/chrome_reset_patch.rb new file mode 100644 index 0000000000..afa5037306 --- /dev/null +++ b/spec/support/pageflow/support/config/capybara/chrome_reset_patch.rb @@ -0,0 +1,38 @@ +# Chrome 158 rejects the '*' origin Capybara passes to +# Storage.clearDataForOrigin in ChromeDriver#reset!, which makes every +# js spec fail in its after hook. +# +# Can be removed once the following PR has been released: +# https://github.com/teamcapybara/capybara/pull/2868 +module Pageflow + module ChromeResetPatch + def reset! + @origin_before_reset = current_origin_for_reset if @browser + super + end + + private + + def execute_cdp(cmd, params = {}) + super + rescue Selenium::WebDriver::Error::InvalidArgumentError + raise unless cmd == 'Storage.clearDataForOrigin' && params[:origin] == '*' + + super('Network.clearBrowserCookies') + return unless @origin_before_reset && clear_all_storage? + + super('Storage.clearDataForOrigin', + origin: @origin_before_reset, + storageTypes: 'local_storage') + end + + def current_origin_for_reset + origin = Addressable::URI.parse(current_url)&.origin + origin if origin&.start_with?('http') + rescue Selenium::WebDriver::Error::WebDriverError + nil + end + end +end + +Capybara::Selenium::Driver::ChromeDriver.prepend(Pageflow::ChromeResetPatch)