From a96433446e87c03ac6e253a54eb067ac93a33575 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Thu, 6 Aug 2020 15:08:48 +0200 Subject: [PATCH] Bug 26162: Make Selenium click action more robust See https://stackoverflow.com/questions/12967541/how-to-avoid-staleelementreferenceexception-in-selenium https://www.selenium.dev/exceptions/ https://developer.mozilla.org/en-US/docs/Web/WebDriver/Errors/StaleElementReference This patch will fix the following failure we get under D11: 18:47:07 selenium_1 | 09:47:07.478 WARN - Exception: Element not found in the cache - perhaps the page has changed since it was looked up 18:47:07 selenium_1 | For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html 18:47:07 selenium_1 | Build info: version: '2.53.1', revision: 'a36b8b1', time: '2016-06-30 17:37:03' 18:47:07 selenium_1 | System info: host: '78b9a07f51f2', ip: '192.168.16.2', os.name: 'Linux', os.arch: 'amd64', os.version: '4.19.0-9-amd64', java.version: '1.8.0_91' 18:47:07 selenium_1 | Driver info: driver.version: unknown 18:47:07 koha_1 | 18:47:07 koha_1 | STRACE: /usr/share/perl5/Try/Tiny.pm:123 in Selenium::Remote::Driver::catch {...} 18:47:07 koha_1 | /usr/local/share/perl/5.26.1/Selenium/Remote/Driver.pm:353 in Try::Tiny::try 18:47:07 koha_1 | (eval 1571):1 in Selenium::Remote::Driver::__ANON__ 18:47:07 koha_1 | (eval 1573):2 in Selenium::Remote::Driver::__ANON__ 18:47:07 koha_1 | (eval 1546):17 in Selenium::Remote::Driver::_execute_command 18:47:07 koha_1 | /usr/local/share/perl/5.26.1/Selenium/Remote/WebElement.pm:63 in Selenium::Remote::WebElement::_execute_command 18:47:07 koha_1 | /kohadevbox/koha/t/lib/Selenium.pm:184 in Selenium::Remote::WebElement::click 18:47:07 koha_1 | /kohadevbox/koha/t/lib/Selenium.pm:172 in t::lib::Selenium::click_when_visible 18:47:07 koha_1 | t/db_dependent/selenium/administration_tasks.t:131 in t::lib::Selenium::click Signed-off-by: Martin Renvoize Signed-off-by: Jonathan Druart --- t/lib/Selenium.pm | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/t/lib/Selenium.pm b/t/lib/Selenium.pm index 4644d114f0..a4a36fc68f 100644 --- a/t/lib/Selenium.pm +++ b/t/lib/Selenium.pm @@ -177,11 +177,20 @@ sub click_when_visible { $self->driver->set_implicit_wait_timeout(20000); my ($visible, $elt); while ( not $visible ) { - $elt = $self->driver->find_element($xpath_selector); - $visible = $elt->is_displayed; + $elt = eval {$self->driver->find_element($xpath_selector) }; + $visible = $elt && $elt->is_displayed; $self->driver->pause(1000) unless $visible; } - $elt->click; + + my $clicked; + $self->remove_error_handler; + while ( not $clicked ) { + eval { $elt->click }; + $clicked = !$@; + $self->driver->pause(1000) unless $clicked; + } + $self->add_error_handler; + $elt->click unless $clicked; # finally Raise the error } =head1 NAME -- 2.39.5