Bug 32926: Fix selenium tests
[koha.git] / t / db_dependent / selenium / administration_tasks.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 #This selenium test tests the Koha Administration module functionality including adding circ rules, item types and modifying frameworks
19
20 #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
21
22 use Modern::Perl;
23
24 use C4::Context;
25
26 use Test::More tests => 3;
27
28 use t::lib::Selenium;
29 use t::lib::TestBuilder;
30
31 my $builder = t::lib::TestBuilder->new;
32
33 my $login = $ENV{KOHA_USER} || 'koha';
34
35 my $itemtype      = 'UT_DVD';
36 my $frameworkcode = 'UTFW';     # frameworkcode is only 4 characters max!
37 my $branchcode    = 'UT_BC';
38 my $av_category   = 'AV_CAT_TEST';
39 my $category_code = 'PATRON_CAT';
40 our ($cleanup_needed);
41
42 SKIP: {
43     eval { require Selenium::Remote::Driver; };
44     skip "Selenium::Remote::Driver is needed for selenium tests.", 3 if $@;
45
46     $cleanup_needed = 1;
47
48     my $s        = t::lib::Selenium->new;
49     my $driver   = $s->driver;
50     $driver->set_window_size(3840,1080);
51     my $mainpage = $s->base_url . q|mainpage.pl|;
52     $driver->get($mainpage);
53     like( $driver->get_title(), qr(Log in to Koha), );
54     $s->auth;
55     { # Item types
56         # Navigate to the Administration area and create an item type
57         $s->click( { href => '/admin/admin-home.pl', main => 'container-main' } )
58           ;    # Koha administration
59         $s->click( { href => '/admin/itemtypes.pl', main_class => 'main container-fluid' } );  # Item Types
60         $s->click( { href => '/admin/itemtypes.pl?op=add_form', main_class => 'main container-fluid' } )
61           ;    # New item type
62         $s->fill_form(
63             { itemtype => $itemtype, description => "Digital Optical Disc" } );
64         $s->submit_form;
65         $s->click(
66             {
67                 href => '/admin/itemtypes.pl?op=add_form&itemtype=' . $itemtype,
68                 main_class => 'main container-fluid'
69             }
70         );     # New item type
71     };
72
73     { # Circulation/fine rules
74         my $itype = $builder->build_object({ class => "Koha::ItemTypes" });
75         $driver->get($mainpage);
76         $s->click( { href => '/admin/admin-home.pl', main => 'container-main' } )
77           ;    # Koha administration
78         $s->click( { href => '/admin/smart-rules.pl', main_class => 'main container-fluid' } )
79           ;    # Circulation and fines rules
80         my $elt = $driver->find_element('//tr[@id="edit_row"]/td/select[@id="matrixitemtype"]/option[@value="'.$itype->itemtype.'"]');
81         is( $elt->get_text(),$itype->description,"Our new itemtype is in the list");
82         $elt->click();
83         $elt = $driver->find_element('//tr[@id="edit_row"]/td[@class="actions"]/button[@type="submit"]');
84         $elt->click();
85         $elt = $driver->find_elements('//table[@id="default-circulation-rules"]/tbody/tr/td[contains(text(),"'.$itype->description.'")]/following-sibling::td/span[text() = "Unlimited"]');
86         is( @$elt, 5, "We have unlimited checkouts and holds" );
87         #Clean up
88         Koha::CirculationRules->search( { itemtype => $itype->itemtype } )->delete;
89         $itype->delete;
90                # TODO Create more smart rules navigation here
91     };
92
93     { # Biblio frameworks
94         $driver->get($mainpage);
95         $s->click( { href => '/admin/admin-home.pl', main => 'container-main' } )
96           ;    # Koha administration
97         $s->click( { href => '/admin/biblio_framework.pl', main_class => 'main container-fluid' } )
98           ;    # MARC bibliographic framework
99         $s->click(
100             { href => '/admin/biblio_framework.pl?op=add_form', main_class => 'main container-fluid' } )
101           ;    # New framework
102         $s->fill_form(
103             {
104                 frameworkcode => $frameworkcode,
105                 description   => 'just a description'
106             }
107         );
108         $s->submit_form;
109         $s->click( { id => 'frameworkactions' . $frameworkcode } );
110         $s->click(
111             {
112                 href => 'marctagstructure.pl?frameworkcode=' . $frameworkcode,
113                 main_class => 'main container-fluid'
114             }
115         );    # MARC structure # FIXME '/admin/' is missing in the url
116               # TODO Click on OK to create the MARC structure
117     };
118
119     { #Libraries
120         $driver->get($mainpage);
121         $s->click( { href => '/admin/admin-home.pl', main => 'container-main' } )
122           ;    # Koha administration
123         $s->click( { href => '/admin/branches.pl', main_class => 'main container-fluid' } )
124           ;    # Libraries and groups
125         $s->click( { href => '/admin/branches.pl?op=add_form', main_class => 'main container-fluid' } )
126           ;    # New library
127         $s->fill_form( { branchcode => $branchcode, branchname => 'my library' } );
128         $s->submit_form;
129
130         # Select "Show all" in the datatable "Show x entries" dropdown list to make sure our library is not hidden
131         $s->show_all_entries('//div[@id="libraries_wrapper"]');
132         $s->click(
133             {
134                 href => '/admin/branches.pl?op=add_form&branchcode=' . $branchcode,
135                 main_class => 'main container-fluid'
136             }
137         );     # Edit
138         $s->fill_form( { branchname => 'another branchname' } );
139         $s->submit_form;
140
141         # Select "Show all" in the datatable "Show x entries" dropdown list to make sure our library is not hidden
142         $s->show_all_entries('//div[@id="libraries_wrapper"]');
143         $s->click(
144             {
145                 id => 'delete_library_'.$branchcode,
146             }
147         );     # Delete
148     };
149
150     { #Authorized values
151         $driver->get($mainpage);
152         $s->click( { href => '/admin/admin-home.pl', main => 'container-main' } ); #Koha administration
153
154         $s->click( { href => '/admin/authorised_values.pl', main_class => 'main container-fluid' } ); #Authorized values
155
156         $s->click( { href => { 'ends-with' => '/admin/authorised_values.pl?op=add_form' }, main_class => 'main container-fluid' } ); # New category
157         $s->fill_form( { category => $av_category } );
158         $s->submit_form;
159
160         $s->click(
161             {
162                 href => '/admin/authorised_values.pl?op=add_form&category=' . $av_category,
163                 main_class => 'main container-fluid'
164             }
165         );    # New authorised value for ...
166         $s->fill_form(
167             {
168                 authorised_value => "$av_category" . "_xxx",
169                 lib              => "This is a description for staff",
170                 lib_opac         => "This is a description for OPAC"
171             }
172         );
173         $s->submit_form;
174
175         my $dbh = C4::Context->dbh;
176         my ( $av_id ) = $dbh->selectrow_array(q|
177             SELECT id FROM authorised_values WHERE category=?|, undef, $av_category );
178         $s->click(
179             {
180                 href => '/admin/authorised_values.pl?op=delete&searchfield=' . $av_category . '&id=' . $av_id,
181                 main_class => 'main container-fluid'
182             }
183         );
184         $s->driver->accept_alert; # Accept the modal "Are you sure you want to delete this authorized value?"
185
186         # For an unknown reason the click on the next admin-home link does not work
187         # We were still on the authorised value list view.
188         # A workaround (and dirty fix) is to wait for 1 second and see if it fixes the problem
189         sleep 1;
190     };
191
192     { # Patron categories
193         $driver->get($mainpage);
194         $s->click( { href => '/admin/admin-home.pl', main => 'container-main' } ); # Koha administration
195         $s->click( { href => '/admin/categories.pl', main_class => 'main container-fluid' } ); # Patron categories
196         $s->click( { href => '/admin/categories.pl?op=add_form', main_class => 'main container-fluid' } ); # New patron category
197
198         $s->fill_form( { categorycode => $category_code, description => 'Test category', enrolmentperiod => 12, category_type => 'A' } );
199         $s->submit_form;
200
201         # Select "Show all" in the datatable "Show x entries" dropdown list to make sure our category is not hidden
202         $s->show_all_entries('//div[@id="patron_categories_wrapper"]');
203
204         $s->click(
205             {
206                 href => '/admin/categories.pl?op=delete_confirm&categorycode=' . $category_code,
207                 main_class => 'main container-fluid'
208             }
209         ); # Delete button
210
211         $s->submit_form; # Delete this category
212
213         # TODO Make sure the category has been deleted
214     };
215
216     $driver->quit();
217 }
218
219 END {
220     cleanup() if $cleanup_needed;
221 };
222
223 sub cleanup {
224     my $dbh = C4::Context->dbh;
225     $dbh->do(q|DELETE FROM itemtypes WHERE itemtype=?|, undef, $itemtype);
226     $dbh->do(q|DELETE FROM biblio_framework WHERE frameworkcode=?|, undef, $frameworkcode);
227     $dbh->do(q|DELETE FROM branches WHERE branchcode=?|, undef, $branchcode);
228     $dbh->do(q|DELETE FROM authorised_value_categories WHERE category_name=?|, undef, $av_category);
229     $dbh->do(q|DELETE FROM categories WHERE categorycode=?|, undef, $category_code);
230 }