From ecf50be31f6c74a1902a48204a1843d816fcb97d Mon Sep 17 00:00:00 2001
From: otegami <otegami@clear-code.com>
Date: Mon, 17 Mar 2025 09:35:12 +0900
Subject: Use Capybara's assert_current_path in login steps to wait for page
 load
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

# Problem

The login step in the system test sometimes fails
because the expected page load isn’t complete when the
assertion is executed as follows.

```
$ cd ../redmine && RAILS_ENV=test bin/rails redmine:plugins:test NAME=full_text_search
# Running:

...............Capybara starting Puma...
* Version 6.6.0 , codename: Return to Forever
* Min threads: 0, max threads: 4
* Listening on http://127.0.0.1:43813
.[Screenshot Image]: /home/runner/work/redmine_full_text_search/redmine/tmp/screenshots/failures_test_keep_search_target.png
F

Failure:
FullTextSearch::SearchTest#test_keep_search_target [test/application_system_test_case.rb:79]:
--- expected
+++ actual
@@ -1 +1 @@
-"/my/page"
+"/login"

rails test plugins/full_text_search/test/system/full_text_search/search_test.rb:58

............SSS..........................................................................

Finished in 124.323414s, 0.8526 runs/s, 1.0778 assertions/s.
106 runs, 134 assertions, 1 failures, 0 errors, 3 skips
```

## Cause

The test was using `assert_equal` to verify the
current path immediately after form submission. Since
`assert_equal` does not wait for the page to load
fully, it leads to intermittent failures in
environments where page rendering is slower.

## Solution

Replace `assert_equal` with `assert_current_path` in
the login steps. This change leverages Capybara’s
built-in waiting mechanism, ensuring that the test
only proceeds once the browser has navigated to the expected page.

ref: https://www.rubydoc.info/gems/capybara/Capybara%2FSessionMatchers:assert_current_path
---
 test/application_system_test_case.rb | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/test/application_system_test_case.rb b/test/application_system_test_case.rb
index 4a6fd0d30..af400bfcd 100644
--- a/test/application_system_test_case.rb
+++ b/test/application_system_test_case.rb
@@ -68,13 +68,13 @@ class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
   # using default browser locale which depend on system locale for "real" browsers drivers
   def log_user(login, password)
     visit '/my/page'
-    assert_equal '/login', current_path
+    assert_current_path '/login'
     within('#login-form form') do
       fill_in 'username', :with => login
       fill_in 'password', :with => password
       find('input[name=login]').click
     end
-    assert_equal '/my/page', current_path
+    assert_current_path '/my/page'
   end
 
   def wait_for_ajax
-- 
2.34.1

