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 <noreply@anthropic.com> Signed-off-by: Lucas Gass <lucas@bywatersolutions.com> Signed-off-by: Pedro Amorim <pedro.amorim@openfifth.co.uk>
This commit is contained in:
parent
a86b7a063d
commit
106fabbf9a
|
|
@ -17,6 +17,7 @@ package t::lib::Selenium;
|
||||||
|
|
||||||
use Modern::Perl;
|
use Modern::Perl;
|
||||||
use Carp qw( croak );
|
use Carp qw( croak );
|
||||||
|
use LWP::UserAgent;
|
||||||
|
|
||||||
use C4::Context;
|
use C4::Context;
|
||||||
|
|
||||||
|
|
@ -47,6 +48,7 @@ sub new {
|
||||||
bless $self, $class;
|
bless $self, $class;
|
||||||
$self->add_error_handler;
|
$self->add_error_handler;
|
||||||
$self->driver->set_implicit_wait_timeout(10000);
|
$self->driver->set_implicit_wait_timeout(10000);
|
||||||
|
$self->wait_for_server_ready( $self->{base_url} );
|
||||||
return $self;
|
return $self;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -297,6 +299,26 @@ sub click_when_visible {
|
||||||
$elt->click unless $clicked; # finally Raise the error
|
$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 }
|
sub max_retries { 10 }
|
||||||
|
|
||||||
=head1 NAME
|
=head1 NAME
|
||||||
|
|
@ -375,6 +397,14 @@ when we use automation test using Selenium
|
||||||
|
|
||||||
Wait 10s for an element to be hidden
|
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
|
=head2 wait_for_ajax
|
||||||
|
|
||||||
$s->wait_for_ajax;
|
$s->wait_for_ajax;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue