From a2aaa594fd452d314f07f3c8895d76c6bf4fe826 Mon Sep 17 00:00:00 2001 From: Hampton Lintorn-Catlin Date: Fri, 3 Apr 2026 15:58:37 -0500 Subject: [PATCH] Fix flaky system tests: wait for Turbo redirect before asserting content After clicking 'Sign In', Turbo Drive replaces the page body via a redirect to the plans index. If Selenium asserts have_content('Sign out') before the navigation completes, it references a stale DOM node from the sign-in page, causing 'Node with given id does not belong to the document'. Fix: add have_current_path(root_path) after click_button to wait for the redirect to complete before asserting on the new page's content. Applied to all three system spec files that share this sign-in pattern. Amp-Thread-ID: https://ampcode.com/threads/T-019d5520-a72e-723a-9dc4-7ee62fb4fb5d Co-authored-by: Amp --- spec/system/comment_anchoring_spec.rb | 1 + spec/system/comment_ux_spec.rb | 5 +++++ spec/system/tokens_spec.rb | 1 + 3 files changed, 7 insertions(+) diff --git a/spec/system/comment_anchoring_spec.rb b/spec/system/comment_anchoring_spec.rb index 706c53f..c7b4f47 100644 --- a/spec/system/comment_anchoring_spec.rb +++ b/spec/system/comment_anchoring_spec.rb @@ -34,6 +34,7 @@ visit sign_in_path fill_in "Email address", with: user.email click_button "Sign In" + expect(page).to have_current_path(root_path) expect(page).to have_content("Sign out") end diff --git a/spec/system/comment_ux_spec.rb b/spec/system/comment_ux_spec.rb index fff1cd5..aa492e3 100644 --- a/spec/system/comment_ux_spec.rb +++ b/spec/system/comment_ux_spec.rb @@ -34,6 +34,11 @@ def sign_in(user) visit sign_in_path fill_in "Email address", with: user.email click_button "Sign In" + # Wait for the Turbo-driven redirect to the plans index to complete before + # asserting page content; without this, Selenium can reference a stale DOM + # node from the sign-in page mid-navigation, causing "Node with given id + # does not belong to the document". + expect(page).to have_current_path(root_path) expect(page).to have_content("Sign out") end diff --git a/spec/system/tokens_spec.rb b/spec/system/tokens_spec.rb index 7104ede..45e686c 100644 --- a/spec/system/tokens_spec.rb +++ b/spec/system/tokens_spec.rb @@ -5,6 +5,7 @@ visit sign_in_path fill_in "Email address", with: "testuser@example.com" click_button "Sign In" + expect(page).to have_current_path(root_path) expect(page).to have_content("Sign out") end