From e77ce6c931fbc560cbd80d771f92b304e086421e Mon Sep 17 00:00:00 2001
From: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Date: Mon, 3 Jan 2022 12:38:29 +0100
Subject: [PATCH] Bug 29779: Make selenium tests return green even if lib is
 missing

Some selenium tests are not correctly written and they fail is
Selenium::Remote::Driver is missing.

Test plan:
1. Remove the lib rm `pmpath Selenium::Remote::Driver`
2. prove t/db_dependent/selenium/
should return green

Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
---
 t/db_dependent/selenium/administration_tasks.t  |  2 +-
 t/db_dependent/selenium/basic_workflow.t        |  4 ++--
 t/db_dependent/selenium/patrons_search.t        |  9 +++++++--
 t/db_dependent/selenium/regressions.t           |  8 ++++++--
 t/db_dependent/selenium/remove_from_cart.t      | 10 +++++++---
 t/db_dependent/selenium/self_registration.t     |  8 ++++++--
 t/db_dependent/selenium/update_child_to_adult.t |  8 ++++++--
 7 files changed, 35 insertions(+), 14 deletions(-)

diff --git a/t/db_dependent/selenium/administration_tasks.t b/t/db_dependent/selenium/administration_tasks.t
index 080400d2a3..5440a5fb58 100755
--- a/t/db_dependent/selenium/administration_tasks.t
+++ b/t/db_dependent/selenium/administration_tasks.t
@@ -41,7 +41,7 @@ our ($cleanup_needed);
 
 SKIP: {
     eval { require Selenium::Remote::Driver; };
-    skip "Selenium::Remote::Driver is needed for selenium tests.", 1 if $@;
+    skip "Selenium::Remote::Driver is needed for selenium tests.", 3 if $@;
 
     $cleanup_needed = 1;
 
diff --git a/t/db_dependent/selenium/basic_workflow.t b/t/db_dependent/selenium/basic_workflow.t
index 4bf121b022..095c47c954 100755
--- a/t/db_dependent/selenium/basic_workflow.t
+++ b/t/db_dependent/selenium/basic_workflow.t
@@ -85,14 +85,14 @@ our $sample_data = {
 };
 our ( $borrowernumber, $start, $prev_time, $cleanup_needed );
 
-$dbh->do(q|INSERT INTO itemtypes(itemtype) VALUES (?)|, undef, $sample_data->{itemtype}{itemtype});
-
 SKIP: {
     eval { require Selenium::Remote::Driver; };
     skip "Selenium::Remote::Driver is needed for selenium tests.", 22 if $@;
 
     $cleanup_needed = 1;
 
+    $dbh->do(q|INSERT INTO itemtypes(itemtype) VALUES (?)|, undef, $sample_data->{itemtype}{itemtype});
+
     open my $fh, '>>', '/tmp/output.txt';
 
     my $s = t::lib::Selenium->new;
diff --git a/t/db_dependent/selenium/patrons_search.t b/t/db_dependent/selenium/patrons_search.t
index 5161282d03..0d338298d9 100755
--- a/t/db_dependent/selenium/patrons_search.t
+++ b/t/db_dependent/selenium/patrons_search.t
@@ -20,7 +20,7 @@ use Modern::Perl;
 use C4::Context;
 
 use utf8;
-use Test::More tests => 1;
+use Test::More;
 use Test::MockModule;
 
 use C4::Context;
@@ -30,7 +30,12 @@ use t::lib::Selenium;
 use t::lib::TestBuilder;
 
 eval { require Selenium::Remote::Driver; };
-skip "Selenium::Remote::Driver is needed for selenium tests.", 1 if $@;
+if ( $@ ) {
+    plan skip_all => "Selenium::Remote::Driver is needed for selenium tests.";
+} else {
+    plan tests => 1;
+}
+
 
 my $s             = t::lib::Selenium->new;
 my $driver        = $s->driver;
diff --git a/t/db_dependent/selenium/regressions.t b/t/db_dependent/selenium/regressions.t
index aa7717f806..3a15611a87 100755
--- a/t/db_dependent/selenium/regressions.t
+++ b/t/db_dependent/selenium/regressions.t
@@ -20,7 +20,7 @@ use utf8;
 
 use C4::Context;
 
-use Test::More tests => 7;
+use Test::More;
 use Test::MockModule;
 
 use C4::Context;
@@ -33,7 +33,11 @@ use t::lib::TestBuilder;
 use t::lib::Mocks;
 
 eval { require Selenium::Remote::Driver; };
-skip "Selenium::Remote::Driver is needed for selenium tests.", 7 if $@;
+if ( $@ ) {
+    plan skip_all => "Selenium::Remote::Driver is needed for selenium tests.";
+} else {
+    plan tests => 7;
+}
 
 my $s = t::lib::Selenium->new;
 
diff --git a/t/db_dependent/selenium/remove_from_cart.t b/t/db_dependent/selenium/remove_from_cart.t
index c1f4c6c662..f477e99f63 100755
--- a/t/db_dependent/selenium/remove_from_cart.t
+++ b/t/db_dependent/selenium/remove_from_cart.t
@@ -19,7 +19,7 @@ use Modern::Perl;
 use File::Basename qw(dirname );
 
 use Test::WWW::Mechanize;
-use Test::More tests => 1;
+use Test::More;
 use Test::MockModule;
 
 use C4::Context;
@@ -30,7 +30,11 @@ use t::lib::Mocks;
 use t::lib::Mocks::Zebra;
 
 eval { require Selenium::Remote::Driver; };
-skip "Selenium::Remote::Driver is needed for selenium tests.", 1 if $@;
+if ( $@ ) {
+    plan skip_all => "Selenium::Remote::Driver is needed for selenium tests.";
+} else {
+    plan tests => 1;
+}
 
 my $s = t::lib::Selenium->new;
 
@@ -101,5 +105,5 @@ subtest 'OPAC - Remove from cart' => sub {
 
 END {
     C4::Context->set_preference('SearchEngine', $SearchEngine_value);
-    $mock_zebra->cleanup;
+    $mock_zebra->cleanup if $mock_zebra;
 };
diff --git a/t/db_dependent/selenium/self_registration.t b/t/db_dependent/selenium/self_registration.t
index 737dd691c8..228db4b70d 100755
--- a/t/db_dependent/selenium/self_registration.t
+++ b/t/db_dependent/selenium/self_registration.t
@@ -20,7 +20,7 @@ use utf8;
 
 use C4::Context;
 
-use Test::More tests => 1;
+use Test::More;
 use Test::MockModule;
 
 use C4::Context;
@@ -30,7 +30,11 @@ use t::lib::TestBuilder;
 use t::lib::Mocks;
 
 eval { require Selenium::Remote::Driver; };
-skip "Selenium::Remote::Driver is needed for selenium tests.", 1 if $@;
+if ( $@ ) {
+    plan skip_all => "Selenium::Remote::Driver is needed for selenium tests.";
+} else {
+    plan tests => 1;
+}
 
 my $s = t::lib::Selenium->new;
 
diff --git a/t/db_dependent/selenium/update_child_to_adult.t b/t/db_dependent/selenium/update_child_to_adult.t
index 1b20b8ba4d..f821fe6399 100755
--- a/t/db_dependent/selenium/update_child_to_adult.t
+++ b/t/db_dependent/selenium/update_child_to_adult.t
@@ -19,7 +19,7 @@ use Modern::Perl;
 
 use C4::Context;
 
-use Test::More tests => 1;
+use Test::More;
 use Test::MockModule;
 
 use C4::Context;
@@ -29,7 +29,11 @@ use t::lib::Selenium;
 use t::lib::TestBuilder;
 
 eval { require Selenium::Remote::Driver; };
-skip "Selenium::Remote::Driver is needed for selenium tests.", 1 if $@;
+if ( $@ ) {
+    plan skip_all => "Selenium::Remote::Driver is needed for selenium tests.";
+} else {
+    plan tests => 1;
+}
 
 my $s             = t::lib::Selenium->new;
 my $driver        = $s->driver;
-- 
2.20.1