From ebb3cfeffc1c67b6c1c6c8e3b9622798cc58cdcd Mon Sep 17 00:00:00 2001 From: MAEDA Go Date: Tue, 24 Nov 2026 19:14:09 +0900 Subject: [PATCH] Work around session reset failures of Capybara with Chrome 158 Capybara 3.40.0 resets a session by sending Storage.clearDataForOrigin with the origin "*", which Chrome 158 rejects. The reset then fails, the cookies are kept, and the next system test starts logged in, so many system tests fail. Clear the cookies with Network.clearBrowserCookies and the other storage with the origin of the application instead. The workaround has no effect once Capybara stops sending the origin "*", and should be removed when a fixed version is released. See https://github.com/teamcapybara/capybara/issues/2865 Co-Authored-By: Claude Opus 5.5 --- test/application_system_test_case.rb | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/test/application_system_test_case.rb b/test/application_system_test_case.rb index 1c04620e8..244fa2b28 100644 --- a/test/application_system_test_case.rb +++ b/test/application_system_test_case.rb @@ -22,6 +22,29 @@ require_relative 'test_helper' class ApplicationSystemTestCase < ActionDispatch::SystemTestCase DOWNLOADS_PATH = File.expand_path(File.join(Rails.root, 'tmp', 'downloads')) + # Capybara 3.40.0 resets a session by clearing the storage with + # Storage.clearDataForOrigin and the origin "*", but Chrome 158 and later + # reject it. If the reset fails, cookies are kept and the next test starts + # logged in. This workaround has no effect if Capybara does not send the + # origin "*". See https://github.com/teamcapybara/capybara/issues/2865 + # TODO: Remove this workaround when a fixed version of Capybara is released. + module ClearStorageWithoutWildcardOrigin + private + + def execute_cdp(cmd, params = {}) + return super unless cmd == 'Storage.clearDataForOrigin' && params[:origin] == '*' + + types = params[:storageTypes].to_s.split(',') + super('Network.clearBrowserCookies') if types.delete('cookies') + + origin = ENV['CAPYBARA_APP_HOST'] || Capybara.current_session.server&.base_url + super(cmd, params.merge(origin: origin, storageTypes: types.join(','))) if origin && types.any? + end + end + require 'capybara/selenium/driver' + require 'capybara/selenium/driver_specializations/chrome_driver' + Capybara::Selenium::Driver::ChromeDriver.prepend(ClearStorageWithoutWildcardOrigin) + # Allow running Capybara default server on custom IP address and/or port Capybara.server_host = ENV['CAPYBARA_SERVER_HOST'] if ENV['CAPYBARA_SERVER_HOST'] Capybara.server_port = ENV['CAPYBARA_SERVER_PORT'] if ENV['CAPYBARA_SERVER_PORT'] -- 2.55.0