From 527f8fadcceed3c5373c9d28755ab474061711b2 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Mon, 21 Nov 2022 07:55:52 +0100 Subject: [PATCH] Bug 32010: 2AF selenium tests - Alert can appear when we are waiting for ajax If we are waiting for ajax (there is a sleep 1s) and the alert pops up at the same time, Selenium is raising "unexpected alert open" We should not wait for the ajax request, but better wait for the alert actually. Signed-off-by: Tomas Cohen Arazi --- t/db_dependent/selenium/authentication_2fa.t | 3 +-- t/lib/Selenium.pm | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/t/db_dependent/selenium/authentication_2fa.t b/t/db_dependent/selenium/authentication_2fa.t index a6bf221e5d..8c9979be5d 100755 --- a/t/db_dependent/selenium/authentication_2fa.t +++ b/t/db_dependent/selenium/authentication_2fa.t @@ -279,8 +279,7 @@ SKIP: { $driver->find_element('//*[@id="pin_code"]')->clear; $driver->find_element('//*[@id="pin_code"]')->send_keys($pin_code); $driver->find_element('//*[@id="register-2FA"]')->click; - $s->wait_for_ajax; - is( $driver->get_alert_text, + is( $s->get_next_alert_text, "Two-factor authentication correctly configured. You will be redirected to the login screen." ); $driver->accept_alert; diff --git a/t/lib/Selenium.pm b/t/lib/Selenium.pm index 72b55f5c82..1117ce7762 100644 --- a/t/lib/Selenium.pm +++ b/t/lib/Selenium.pm @@ -208,6 +208,24 @@ sub wait_for_ajax { } } +sub get_next_alert_text { + my ( $self ) = @_; + + my $alert_text; + my $max_retries = $self->max_retries; + my $i; + $self->remove_error_handler; + while ( not $alert_text ) { + $alert_text = eval { $self->driver->get_alert_text }; + $self->driver->pause(1000) unless $alert_text; + + die "Cannot wait more for next alert (get_next_alert)" + if $max_retries <= ++$i; + } + $self->add_error_handler; + return $alert_text; +} + sub show_all_entries { my ( $self, $xpath_selector ) = @_; -- 2.39.5