From 106fabbf9a61cea7199eb62a984ab9cf4030c845 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Tue, 29 Sep 2026 15:21:00 +0100 Subject: [PATCH] Bug 43669: Make t::lib::Selenium wait for the server to be ready This patch adds a wait_for_server_ready check to t::lib::Selenium, so Selenium-based tests poll the server until it responds successfully before driving it, instead of assuming it is already up. This guards against a race we saw on Jenkins CI: t/db_dependent/selenium/00-onboarding.t and t/db_dependent/selenium/01-installation.t both failed with 'no such element: //div[@class="alert alert-success"]' right after the DB connection check step, immediately after run_tests.pl's get_commands_to_reset_db() restarted Apache/Plack. That helper restarts services and moves straight to running the tests with no readiness wait, so the very first requests can hit workers that are still warming up. The test then fails on a missing page element even though there is no bug in the installer flow itself. A companion issue has been filed against koha-misc4dev to add a readiness wait to run_tests.pl itself: https://gitlab.com/koha-community/koha-misc4dev/-/work_items/108 Test plan: 1. Apply the patch 2. Run t/db_dependent/selenium/01-installation.t (or 00-onboarding.t) against a fresh empty database with KOHA_TESTING=1, as usual 3. Confirm it still passes normally 4. To exercise the new wait: drop/recreate the database, then run "sudo service apache2 restart" and "sudo service koha-common restart", then immediately run the selenium test with no delay. Confirm it either passes (server came up in time) or fails fast with a clear "Cannot wait more for the server to be ready" message rather than a confusing missing-element error deep in the install wizard Co-Authored-By: Claude Sonnet 5 Signed-off-by: Lucas Gass Signed-off-by: Pedro Amorim --- t/lib/Selenium.pm | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/t/lib/Selenium.pm b/t/lib/Selenium.pm index 0ea49a65b7..3f06811bdf 100644 --- a/t/lib/Selenium.pm +++ b/t/lib/Selenium.pm @@ -17,6 +17,7 @@ package t::lib::Selenium; use Modern::Perl; use Carp qw( croak ); +use LWP::UserAgent; use C4::Context; @@ -47,6 +48,7 @@ sub new { bless $self, $class; $self->add_error_handler; $self->driver->set_implicit_wait_timeout(10000); + $self->wait_for_server_ready( $self->{base_url} ); return $self; } @@ -297,6 +299,26 @@ sub click_when_visible { $elt->click unless $clicked; # finally Raise the error } +sub wait_for_server_ready { + my ( $self, $url ) = @_; + + # CI restarts Apache/Plack right before running Selenium tests, but does not + # wait for the workers to finish warming up. Without this, the very first + # requests can hit a server that is not fully ready yet, causing pages to + # render incompletely. + my $ua = LWP::UserAgent->new( timeout => 5 ); + my $max_retries = $self->max_retries; + my ( $ready, $i ); + until ($ready) { + my $response = eval { $ua->get( $url . 'mainpage.pl' ) }; + $ready = $response && $response->is_success; + sleep 1 unless $ready; + + die "Cannot wait more for the server to be ready ($url)" + if $max_retries <= ++$i; + } +} + sub max_retries { 10 } =head1 NAME @@ -375,6 +397,14 @@ when we use automation test using Selenium Wait 10s for an element to be hidden +=head2 wait_for_server_ready + + $s->wait_for_server_ready($url); + + Poll $url (HTTP, outside of the webdriver session) until it responds + successfully, up to 10s. Called from the constructor so tests fail fast + with a clear error instead of racing a server that is still restarting. + =head2 wait_for_ajax $s->wait_for_ajax;