From de74e872a9f2c91cdd1168727f8faa79d9663d1d Mon Sep 17 00:00:00 2001 From: Alex Buckley Date: Wed, 16 Aug 2017 18:53:19 +0000 Subject: [PATCH] Bug 19185 - Selenium test for the Koha web installer and onboarding tool This Selenium test will go through the web installer and onboarding tool testing the Koha installation workflow. Test plan (this test plan includes how to install Selenium as well as how to run this test, for the benefit of people who have not got Selenium installed on their machines): 1. wget https://selenium-release.storage.googleapis.com/2.53/selenium-server-standalone-2.53.0.jar 2. vim /etc/apt/sources.list.d/firefox.list 3. Paste into the file: deb http://packages.linuxmint.com debian import 4. sudo apt-get update 5. sudo apt-get install firefox 6. sudo apt-get install xvfb 7. Set the SELENIUM_PATH vartiable: SELENIUM_PATH=/home/vagrant/kohaclone/selenium-server-standalone-2.53.0.jar 8. Xvfb :1 -screen 0 1024x768x24 2>&1 >/dev/null & 9. DISPLAY=:1 java -jar $SELENIUM_PATH Note: This will start up the selenium server. Everytime you want to shutdown this terminal window and want to restart Selenium just run the step 9 command to restart the Selenium server 10. Open a new terminal window and write in: git clone https://github.com/gempesaw/Selenium-Remote-Driver --branch build/master --single-branch --depth 1 11. cd Selenium-Remote-Driver 12. perl Makefile.PL 13. make 14. make test 15. sudo make install 16. Now everything is installed and you can et up for running the selenium tests 17. In a new terminal restart memcached and drop and recreate the Koha database 18. sudo koha-shell kohadev 19. perl t/db_dependent/selenium/installkoha.t 20. Notice the db errors saying that database tables do not exist 21. Notice the CP test output messages stating what the test is doing Note: warns exist in this selenium test because using Selenium to test the web installer and onboarding tool has proved to be flaky. The use of warning out the title of the currently loaded page and implementing pauses has helped get the test to pass after many hours of troubleshooting. If anyone can suggest alternatives please do so. Sponsored-By: Catalyst IT --- t/db_dependent/selenium/installkoha.t | 228 ++++++++++++++++++++++++++++++++++ 1 file changed, 228 insertions(+) create mode 100644 t/db_dependent/selenium/installkoha.t diff --git a/t/db_dependent/selenium/installkoha.t b/t/db_dependent/selenium/installkoha.t new file mode 100644 index 0000000..adec920 --- /dev/null +++ b/t/db_dependent/selenium/installkoha.t @@ -0,0 +1,228 @@ +#!/usr/bin/perl + +# This file is part of Koha. +# +# Copyright (C) 2017 Catalyst IT +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +# This selenium test is to test the Koha web installer and onboarding tool. Before running the test make sure you have restarted memcached and have dropped and recreated the Koha database. + +# Note: When running the test database errors will be displayed initially however just ignore those errors and the test output will be displayed below it + +# Note: The staffClientBaseURL has been set manually to localhost:8080 and the OPACBaseURL to localhost:80. If you do not usually access the intranet with port 8081 and the opac with port 8080 then you will need to change the addresses in this test accordingly + +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; + +my $dbh = C4::Context->dbh; +my $login = 'koha_kohadev'; +my $password = 'password'; + +our $sample_data = { + category => { + categorycode => 'test_cat', + description => 'test cat description', + enrolmentperiod => '12', + category_type => 'A' + }, + patron => { + surname => 'test_patron_surname', + cardnumber => '4242424242', + userid => 'test_username', + password => 'password', + password2 => 'password' + }, +}; + +my $patronusername="test_username"; +my $patronpassword="password"; + +our ( $borrowernumber, $start, $prev_time, $cleanup_needed ); + +SKIP: { + eval { require Selenium::Remote::Driver; }; + skip "Selenium::Remote::Driver is needed for selenium tests.", 20 if $@; + + $cleanup_needed = 1; + + open my $fh, '>>', '/tmp/output.txt'; + + my $driver = Selenium::Remote::Driver->new; + $start = gettimeofday; + $prev_time = $start; + $driver->get("http://localhost:8080/cgi-bin/koha/mainpage.pl"); + auth( $driver, $login, $password ); + time_diff("main"); + + #Select a language + $driver->find_element_by_xpath('/html/body/div/div/div/form/div/select')->click(); + $driver->find_element('//option[@value="en"]')->click(); + $driver->find_element_by_xpath('/html/body/div/div/div/form/p/input')->click(); + time_diff("Selected english language"); + + $driver->maximize_window(); + $driver->pause(50000); + warn $driver->get_title(); + + #Check perl dependencies + $driver->pause(20000); + $driver->find_element_by_xpath('/html/body/div/div/div/form/p/input')->click(); + time_diff("Continued through checking perl dependencies"); + + #Check database settings + $driver->find_element_by_xpath('/html/body/div/div/div/form/p/input')->click(); + $driver->find_element_by_xpath('/html/body/div/div/div/form/p/input')->click(); + time_diff("Continued through checking database settings"); + + #Setup database + $driver->find_element_by_xpath('/html/body/div/div/div/form/p/input')->click(); + $driver->find_element_by_xpath('/html/body/div/div/div/form/p/input')->click(); + time_diff("Database set up"); + + #Install basic configs + $driver->find_element_by_xpath('/html/body/div/div/div/p[2]/a')->click(); + time_diff("Basic configs installed"); + + $driver->maximize_window(); + $driver->pause(50000); + + #Select MARC flavour + warn $driver->get_title(); + $driver->find_element('//input[@value="Continue to the next step"]')->click; + time_diff("MARC21 installed"); + + #Install default settings + $driver->find_element_by_xpath('/html/body/div/div/div/form/p[2]/input')->click(); + $driver->pause(20000); + $driver->find_element_by_xpath('/html/body/div/div/div/form/p[2]/input')->click(); + time_diff("Default settings installed"); + + #Start onboarding tool +#$driver->pause(20000); + warn $driver->get_title(); + $driver->find_element_by_xpath('/html/body/div/div/div/p/a')->click(); + time_diff("Start onboarding tool"); + + #Onboarding tool create a library + $driver->pause(50000); + warn $driver->get_title(); + $driver->find_element_by_xpath('/html/body/div/div/div/form/fieldset/ol/li[1]/input')->send_keys("Test"); + $driver->find_element_by_xpath('/html/body/div/div/div/form/fieldset/ol/li[2]/input')->send_keys("Test library"); + $driver->find_element_by_xpath('/html/body/div/div/div/form/fieldset/input[3]')->click(); + time_diff("Library created"); + + #Onboarding tool create a patron category + $driver->find_element_by_xpath('/html/body/div[1]/div/div/form/fieldset/ol/li[1]/input')->send_keys("PAT"); + $driver->find_element_by_xpath('/html/body/div[1]/div/div/form/fieldset/ol/li[2]/input')->send_keys("Test patron category"); + $driver->find_element_by_xpath('/html/body/div[1]/div/div/form/fieldset/ol/li[6]/fieldset/ol/li[3]/input')->send_keys('07/29/2020'); + $driver->find_element_by_xpath('/html/body/div[1]/div/div/form/fieldset/input[3]')->click; + time_diff("Patron category created"); + + $driver->set_window_size(1000, 1000); + $driver->maximize_window(); + + #Onboarding tool Create Koha adminstrator patron + $driver->pause(50000); + warn $driver->get_title(); + $driver->find_element_by_xpath('/html/body/div/div/div/form/fieldset/ol[1]/li[1]/input')->send_keys("Smith"); + $driver->find_element_by_xpath('/html/body/div/div/div/form/fieldset/ol[1]/li[2]/input')->send_keys("Mary"); + + $driver->pause(50000); + warn $driver->get_title(); + $driver->find_element_by_xpath('/html/body/div/div/div/form/fieldset/ol[2]/li[1]/input')->send_keys("10203"); + $driver->find_element_by_xpath('/html/body/div/div/div/form/fieldset/ol[4]/li[1]/input')->send_keys("Mary"); + $driver->find_element_by_xpath('/html/body/div/div/div/form/fieldset/ol[4]/li[2]/input')->send_keys("password"); + $driver->find_element_by_xpath('/html/body/div/div/div/form/fieldset/ol[4]/li[3]/input')->send_keys("password"); + $driver->find_element_by_xpath('/html/body/div/div/div/form/input')->click(); + time_diff("Create administrator patron"); + + #Onboarding tool Create item type + $driver->find_element_by_xpath('/html/body/div/div/div/form/fieldset/ol/li[1]/input')->send_keys("BOOK"); + $driver->find_element_by_xpath('/html/body/div/div/div/form/fieldset/ol/li[2]/input')->send_keys("Non-electronic book"); + $driver->find_element_by_xpath('/html/body/div/div/div/form/fieldset/input[3]')->click(); + time_diff("Item type created"); + + #Onboarding to Create circulation rule + $driver->find_element_by_xpath('/html/body/div/div/div/form/input')->click(); + $driver->find_element_by_xpath('/html/body/div/div/div/a')->click(); + time_diff("Circulation rule created"); + + #Login to Koha + $driver->find_element_by_xpath('/html/body/div/div/div/form/p[1]/input')->send_keys("Mary"); + $driver->find_element_by_xpath('/html/body/div/div/div/form/p[2]/input')->send_keys("password"); + $driver->find_element_by_xpath('/html/body/div/div/div/form/p[4]/input')->click(); + like( $driver->get_title(), qr(Koha staff client), ); + + close $fh; + $driver->quit(); +}; + +END { + cleanup() if $cleanup_needed; +}; + +sub auth { + my ( $driver, $login, $password) = @_; + fill_form( $driver, { userid => 'koha_kohadev', password => 'password' } ); + my $login_button = $driver->find_element_by_xpath('/html/body/div/div/div/form/fieldset/input'); + $login_button->submit(); +} + +sub patron_auth { + my ( $driver,$patronusername, $patronpassword) = @_; + fill_form( $driver, { userid => $patronusername, password => $patronpassword } ); + my $login_button = $driver->find_element('//input[@id="submit"]'); + $login_button->submit(); +} + +sub patron_opac_auth { + my ( $driver,$patronusername, $patronpassword) = @_; + fill_form( $driver, { userid => $patronusername, password => $patronpassword } ); + my $login_button = $driver->find_element('//input[@value="Log in"]'); + $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}); + $dbh->do(q|DELETE FROM borrowers WHERE userid = ?|, {}, $sample_data->{patron}{userid}); +} + +sub time_diff { + my $lib = shift; + my $now = gettimeofday; + warn "CP $lib = " . sprintf("%.2f", $now - $prev_time ) . "\n"; + $prev_time = $now; +} -- 2.1.4