From 6bf051bdb8aa3c873e0def927ecd724dcac06895 Mon Sep 17 00:00:00 2001 From: Brian Shand Date: Mon, 13 Oct 2025 10:03:02 +0100 Subject: [PATCH] Add fix for capybara/chromedriver issue Our feature tests using Capybara and Chromedriver are intermittently failing with the following error: unknown error: unhandled inspector error: {"code":-32000,"message":"Node with given id does not belong to the document"} This can usually be fixed by rerunning the tests. In our CI environments and end-to-end tests we pin chrome to 128 where the issue doesn't occur. It's harder to pin chrome in local development environments. This commit takes a different approach found in a comment on the capabara issue: It adds a monkey patch to the Selenium driver to intercept the initialization of `UnknownError` and raise a `StaleElementReferenceError` instead. This makes the test retry. This commit can be reverted once the issue is resolved. teamcapybara/capybara#2800 --- test/selenium_error_patch.rb | 37 ++++++++++++++++++++++++++++++++++++ test/test_helper.rb | 1 + 2 files changed, 38 insertions(+) create mode 100644 test/selenium_error_patch.rb diff --git a/test/selenium_error_patch.rb b/test/selenium_error_patch.rb new file mode 100644 index 00000000..ad06f2c0 --- /dev/null +++ b/test/selenium_error_patch.rb @@ -0,0 +1,37 @@ +# Taken from https://github.com/alphagov/forms-admin/commit/5ea98767d765a396ed06cf2cba8f9afb1b10fc0e#diff-c36c07a0c7f499b1b95634f9dce1a10ebf4d10a9dee872f39f746a9efa555ddbR1-R34 +# Monkey patch for a specific intermittent Selenium error. +# +# Intermittently, Selenium/Chromedriver raises `Selenium::WebDriver::Error::UnknownError` +# with the message "Node with given id does not belong to the document". + +# Capybara's automatic waiting/retrying mechanism doesn't catch it, +# leading to failure. +# +# We intercept the initialization of `UnknownError`. If the message matches this specific +# case, we raise a `StaleElementReferenceError` instead. This uses Capybara's +# retry logic which makes doesn't fail the test +# +# This can be removed once the following issue is resolved: +# https://github.com/teamcapybara/capybara/issues/2800 +# +# taken from the following issue: +# https://github.com/teamcapybara/capybara/issues/2800#issuecomment-3049956982 + +# rubocop:disable Style/Alias, Style/IfUnlessModifier, Style/StringLiterals +module Selenium + module WebDriver + module Error + class UnknownError + alias_method :old_initialize, :initialize + def initialize(msg = nil) + if msg&.include?("Node with given id does not belong to the document") + raise StaleElementReferenceError, msg + end + + old_initialize(msg) + end + end + end + end +end +# rubocop:enable Style/Alias, Style/IfUnlessModifier, Style/StringLiterals diff --git a/test/test_helper.rb b/test/test_helper.rb index 6cdb35bf..47caa019 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -19,6 +19,7 @@ require 'pry' require 'capybara/email' require 'create_records_helper' +require 'selenium_error_patch' require_relative 'download_helpers'