From bee0c0b4ab17d336feda519b842f8212edfd696f Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 12 Dec 2017 19:11:15 -0300 Subject: [PATCH] Bug 19243: Add selenium tests for the administration module This is just a start. Signed-off-by: Jonathan Druart --- .../selenium/administration_tasks.t | 141 ++++++++++++++++++ t/lib/Selenium.pm | 63 +++++++- 2 files changed, 203 insertions(+), 1 deletion(-) create mode 100644 t/db_dependent/selenium/administration_tasks.t diff --git a/t/db_dependent/selenium/administration_tasks.t b/t/db_dependent/selenium/administration_tasks.t new file mode 100644 index 0000000000..716ce535df --- /dev/null +++ b/t/db_dependent/selenium/administration_tasks.t @@ -0,0 +1,141 @@ +#!/usr/bin/perl + +# This file is part of Koha. +# +# 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 tests the Koha Administration module functionality including adding circ rules, item types and modifying frameworks + +#Note: If you are testing this on kohadevbox with selenium installed in kohadevbox then you need to set the staffClientBaseURL to localhost:8080 and the OPACBaseURL to http://localhost:80 + +use Modern::Perl; + +use C4::Context; + +use Test::More tests => 1; + +use t::lib::Selenium; + +my $login = $ENV{KOHA_USER} || 'koha'; + +my $itemtype = 'UT_DVD'; +my $frameworkcode = 'UTFW'; # frameworkcode is only 4 characters max! +my $branchcode = 'UT_BC'; +our ($cleanup_needed); + +SKIP: { + eval { require Selenium::Remote::Driver; }; + skip "Selenium::Remote::Driver is needed for selenium tests.", 1 if $@; + + $cleanup_needed = 1; + + my $s = t::lib::Selenium->new; + my $driver = $s->driver; + my $mainpage = $s->base_url . q|mainpage.pl|; + $driver->get($mainpage); + like( $driver->get_title(), qr(Log in to Koha), ); + $s->auth; + + { # Item types + # Navigate to the Administration area and create an item type + $s->click( { href => '/admin/admin-home.pl', main => 'doc3' } ) + ; # Koha administration + $s->click( { href => '/admin/itemtypes.pl', main => 'doc' } ); # Item Types + $s->click( { href => '/admin/itemtypes.pl?op=add_form', main => 'doc3' } ) + ; # New item type + $s->fill_form( + { itemtype => $itemtype, description => "Digital Optical Disc" } ); + $s->submit_form; + $s->click( + { + href => '/admin/itemtypes.pl?op=add_form&itemtype=' . $itemtype, + main => 'doc3' + } + ); # New item type + }; + + { # Circulation/fine rules + $driver->get($mainpage); + $s->click( { href => '/admin/admin-home.pl', main => 'doc3' } ) + ; # Koha administration + $s->click( { href => '/admin/smart-rules.pl', main => 'doc' } ) + ; # Circulation and fines rules + # TODO Create smart navigation here + }; + + { # Biblio frameworks + $driver->get($mainpage); + $s->click( { href => '/admin/admin-home.pl', main => 'doc3' } ) + ; # Koha administration + $s->click( { href => '/admin/biblio_framework.pl', main => 'doc' } ) + ; # MARC bibliographic framework + $s->click( + { href => '/admin/biblio_framework.pl?op=add_form', main => 'doc3' } ) + ; # New framework + $s->fill_form( + { + frameworkcode => $frameworkcode, + description => 'just a description' + } + ); + $s->submit_form; + $s->click( { id => 'frameworkactions' . $frameworkcode } ); + $s->click( + { + href => 'marctagstructure.pl?frameworkcode=' . $frameworkcode, + main => 'doc3' + } + ); # MARC structure # FIXME '/admin/' is missing in the url + # TODO Click on OK to create the MARC structure + }; + + { #Libraries + $driver->get($mainpage); + $s->click( { href => '/admin/admin-home.pl', main => 'doc3' } ) + ; # Koha administration + $s->click( { href => '/admin/branches.pl', main => 'doc' } ) + ; # Libraries and groups + $s->click( { href => '/admin/branches.pl?op=add_form', main => 'doc3' } ) + ; # New library + $s->fill_form( { branchcode => $branchcode, branchname => 'my library' } ); + $s->submit_form; + $s->click( + { + href => '/admin/branches.pl?op=add_form&branchcode=' . $branchcode, + main => 'doc3' + } + ); # Edit + $s->fill_form( { branchname => 'another branchname' } ); + $s->submit_form; + $s->click( + { + href => '/admin/branches.pl?op=delete_confirm&branchcode='. $branchcode, + main => 'doc3' + } + ); # Delete + }; + + $driver->quit(); +} + +END { + cleanup() if $cleanup_needed; +}; + +sub cleanup { + my $dbh = C4::Context->dbh; + $dbh->do(q|DELETE FROM itemtypes WHERE itemtype=?|, undef, $itemtype); + $dbh->do(q|DELETE FROM biblio_framework WHERE frameworkcode=?|, undef, $frameworkcode); + $dbh->do(q|DELETE FROM branches WHERE branchcode=?|, undef, $branchcode); +} diff --git a/t/lib/Selenium.pm b/t/lib/Selenium.pm index 57a553557e..f33e26981c 100644 --- a/t/lib/Selenium.pm +++ b/t/lib/Selenium.pm @@ -85,6 +85,40 @@ sub fill_form { } } +sub submit_form { + my ( $self ) = @_; + + my $default_submit_selector = '//fieldset[@class="action"]/input[@type="submit"]'; + $self->click_when_visible( $default_submit_selector ); +} + +sub click { + my ( $self, $params ) = @_; + my $xpath_selector; + if ( exists $params->{main} ) { + $xpath_selector = '//div[@id="'.$params->{main}.'"]'; + } + if ( exists $params->{href} ) { + $xpath_selector .= '//a[contains(@href, "'.$params->{href}.'")]'; + } + if ( exists $params->{id} ) { + $xpath_selector .= '//*[@id="'.$params->{id}.'"]'; + } + $self->click_when_visible( $xpath_selector ); +} + +sub click_when_visible { + my ( $self, $xpath_selector ) = @_; + $self->driver->set_implicit_wait_timeout(20000); + my ($visible, $elt); + while ( not $visible ) { + $elt = $self->driver->find_element($xpath_selector); + $visible = $elt->is_displayed; + $self->driver->pause(1000) unless $visible; + } + $elt->click; +} + =head1 NAME t::lib::Selenium - Selenium helper module @@ -132,10 +166,37 @@ when we use automation test using Selenium The keys must be element ids (input and select are supported so far) The values must a string. -=head1 AUTHOR +=head2 submit_form + + $s->submit_form; + + It will submit the form using the submit button present in in the fieldset with a clas="action". + It should be the default way. If it does not work you should certainly fix the Koha interface. + +=head2 click + + $s->click + + This is a bit dirty for now but will evolve depending on the needs + 3 parameters possible but only the following 2 forms are used: + $s->click({ href => '/module/script.pl?foo=bar', main => 'doc3' }); # Sometimes we have doc or doc3. To make sure we are not going to hit a link in the header + $s->click({ id => 'element_id }); + +=head2 click_when_visible + + $c->click_when_visible + + Should always be called to avoid the "An element could not be located on the page" error + +=head2 + + +=head1 AUTHORS Jonathan Druart +Alex Buckley + Koha Development Team =head1 COPYRIGHT -- 2.39.5