From b222162d5ac72bafcc05eb677148135f0a44617c Mon Sep 17 00:00:00 2001
From: Victor Grousset/tuxayo <victor@tuxayo.net>
Date: Tue, 28 Apr 2020 06:39:38 +0200
Subject: [PATCH] Bug 18974: Rewrite test using latest conventions + change
 strategy

i.e. conventions from current tests, administration_tasks.t was used
as a model.

Changes in the test logic:
- don't use Z39.50, use cataloging form
- test item and record deletion

== Test plan ==
1. Run the test
2. It should pass!
---
 t/db_dependent/selenium/cataloging_workflow.t | 149 +++++++-----------
 1 file changed, 58 insertions(+), 91 deletions(-)

diff --git a/t/db_dependent/selenium/cataloging_workflow.t b/t/db_dependent/selenium/cataloging_workflow.t
index b302619e2f..45f8ba7800 100644
--- a/t/db_dependent/selenium/cataloging_workflow.t
+++ b/t/db_dependent/selenium/cataloging_workflow.t
@@ -23,21 +23,11 @@
 
 use Modern::Perl;
 
-use Time::HiRes qw(gettimeofday);
 use C4::Context;
-use C4::Biblio qw( AddBiblio ); # We shouldn't use it
 
 use Test::More tests => 1;
-use MARC::Record;
-use MARC::Field;
-
+use t::lib::Selenium;
 my $dbh = C4::Context->dbh;
-my $login = $ENV{KOHA_USER} || 'koha';
-my $password = $ENV{KOHA_PASS} || 'koha';
-my $staff_client_base_url =
-    $ENV{KOHA_INTRANET_URL} || C4::Context->preference("staffClientBaseUrl") || q{};
-my $base_url= $staff_client_base_url . "/cgi-bin/koha/";
-my $opac_url = $ENV{KOHA_OPAC_URL} || C4::Context->preference("OPACBaseURL") || q{};
 
 my $number_of_biblios_to_insert = 3;
 our $sample_data = {
@@ -52,94 +42,77 @@ our $sample_data = {
 my $patronusername="test_username";
 my $patronpassword="password";
 
-our ( $borrowernumber, $start, $prev_time, $cleanup_needed );
+our ( $borrowernumber, $cleanup_needed );
 
 SKIP: {
     eval { require Selenium::Remote::Driver; };
-    skip "Selenium::Remote::Driver is needed for selenium tests.", 20 if $@;
+    skip "Selenium::Remote::Driver is needed for selenium tests.", 1 if $@;
 
     $cleanup_needed = 1;
 
-    open my $fh, '>>', '/tmp/output.txt';
-
-# Go to the mainpage and log in as default user
-    my $driver = Selenium::Remote::Driver->new;
-    $start = gettimeofday;
-    $prev_time = $start;
+    # Go to the mainpage and log in as default user
+    my $s        = t::lib::Selenium->new;
+    my $driver   = $s->driver;
+    my $base_url = $s->base_url;
     $driver->get($base_url."mainpage.pl");
     like( $driver->get_title(), qr(Log in to Koha), );
-    auth( $driver, $login, $password );
-    time_diff("main");
-
-# Go to Cataloging page
-    $driver->find_element_by_xpath('/html/body/div[4]/div/div[1]/div/div[1]/div[1]/div/ul/li[5]/a')->click;
-    $driver->find_element_by_xpath('/html/body/div[4]/div/div/div[1]/div[2]/button[1]')->click;
-    $driver->pause(10000);
-
-    my $handles = $driver->get_window_handles;
-    $driver->switch_to_window($handles->[1]);
-    warn $driver->get_title();
-    $driver->find_element_by_xpath('/html/body/div/div/form/div/div[1]/fieldset/ol/li[3]/input')->send_keys("The Iliad");
-    $driver->find_element_by_xpath('/html/body/div/div/form/div/div[1]/fieldset/ol/li[4]/input')->send_keys("Homer");
-    warn $driver->get_title();
-    $driver->find_element_by_xpath('/html/body/div/div/form/div/fieldset/input')->click;
-    $driver->pause(20000);
-
-# Import the MARC result
-    $driver->find_element_by_xpath('/html/body/div/div/div[1]/table/tbody/tr[1]/td[10]/a')->click;
-
-# Add Koha item type
-    $driver->switch_to_window($handles->[0]);
-    $driver->pause(20000);
-
-    warn $driver->get_title();
-    $driver->find_element_by_xpath('/html/body/div[4]/div/div/div/form/div[3]/div[1]/div[3]/div[2]/input')->click;
-    $driver->find_element_by_xpath('/html/body/div[4]/div/div/div/form/div[3]/ul/li[10]/a')->click;
-    $driver->find_element_by_xpath('/html/body/div[4]/div/div/div/form/div[3]/div[10]/div[2]/div[3]/div')->click;
-    $driver->find_element_by_xpath('/html/body/div[6]/ul/li[1]/div')->click;
-    $driver->find_element_by_xpath('/html/body/div[4]/div/div/div/form/div[1]/div[1]/button[2]')->click;
-    $driver->find_element_by_xpath('/html/body/div[4]/div/div/div/form/div[1]/div[1]/ul/li[2]/a')->click;
-
-    $driver->pause(20000);
-    $driver->find_element_by_xpath('/html/body/div[4]/div/div/div/div/form[2]/button')->click;
-
-# Add item
-    warn $driver->get_title();
-    $driver->find_element_by_xpath('/html/body/div[4]/div/div/div[2]/div[2]/div/form/fieldset[2]/span[1]/input[1]')->click;
-    if ($driver->find_element('//table[@id="itemst"]') ) {
-        time_diff("Item added");
-    } else {
-        time_diff("Item not added");
-    }
-
-   close $fh;
-   $driver->quit();
+    $s->auth;
+
+    # Go to Cataloging page open the cataloging
+    $s->click( { href => '/cataloguing/addbooks.pl',
+                 main => 'container-main' } );
+    $driver->find_element("//*[contains(text(), 'New record')]")->click();
+    $driver->find_element("//*[contains(text(), 'Default framework')]")->click();
+
+    # Try to save to trigger generation of some fields like OOO, 003, 005, 008
+    # TODO that might not be how Koha cataloging should be used! That was guesswork!
+    $s->click( { id => 'saverecord' } );
+    # Accept the modal about the fields not being filled (but now there are autogenerated)
+    $driver->accept_alert;
+
+    # Fill Transcribing agency
+    $driver->find_element_by_css("[id*=tag_040_subfield_c]")->send_keys("My transcribing agency");
+    # Fill Title
+    $s->click( { href => '#tab2XX' } );
+    $driver->find_element_by_css("[id*=tag_245_subfield_a]")->send_keys("My title".rand());
+
+    ## Select Koha item type ##
+    $s->click( { href => '#tab9XX' } );
+    # Open the list of item types
+    $driver->find_element_by_css("[aria-labelledby*=tag_942_subfield_c]")->click;
+    # Select the first item type in the list
+    $driver->find_element_by_css("[role=option]:nth-of-type(1)")->click;
+
+    # Now we can save for real
+    $s->click( { id => 'saverecord' } );
+
+    # Add item
+    $s->click( { id => 'addsingle' } );
+
+    ## Delete item ##
+    $driver->find_element_by_css("[id*=itemactions]")->click;
+    $driver->find_element_by_css(".delete")->click;
+    # Confirm
+    $driver->accept_alert;
+
+    # Go to the record detail page ("Normal tab")
+    $driver->find_element("//*[contains(text(), 'Normal')]")->click();
+
+    ## Delete the record ##
+    $driver->find_element("//*[contains(text(), 'Edit')]")->click();
+    $s->click( { id => 'deletebiblio' } );
+    # Confirm
+    $driver->accept_alert;
+    # Deletion confirmed by the redirection to the advanced search page
+    $driver->find_element_by_css("#advanced-search");
+
+    $driver->quit();
 };
 
 END {
     cleanup() if $cleanup_needed;
 };
 
-sub auth {
-    my ( $driver, $login, $password) = @_;
-    fill_form( $driver, { userid => $login, password => $password } );
-    my $login_button = $driver->find_element('//input[@id="submit"]');
-    $login_button->submit();
-}
-
-sub fill_form {
-    my ( $driver, $values ) = @_;
-    while ( my ( $id, $value ) = each %$values ) {
-        my $element = $driver->find_element('//*[@id="'.$id.'"]');
-        my $tag = $element->get_tag_name();
-        if ( $tag eq 'input' ) {
-            $driver->find_element('//input[@id="'.$id.'"]')->send_keys($value);
-        } elsif ( $tag eq 'select' ) {
-            $driver->find_element('//select[@id="'.$id.'"]/option[@value="'.$value.'"]')->click;
-        }
-    }
-}
-
 sub cleanup {
     my $dbh = C4::Context->dbh;
     $dbh->do(q|DELETE FROM categories WHERE categorycode = ?|, {}, $sample_data->{category}{categorycode});
@@ -155,9 +128,3 @@ sub cleanup {
     };
 }
 
-sub time_diff {
-    my $lib = shift;
-    my $now = gettimeofday;
-    warn "CP $lib = " . sprintf("%.2f", $now - $prev_time ) . "\n";
-    $prev_time = $now;
-}
-- 
2.28.0