3 # This file is part of Koha.
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.
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.
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>.
20 # wget https://selenium-release.storage.googleapis.com/2.53/selenium-server-standalone-2.53.1.jar # Does not work with 3.4, did not test the ones between
21 # sudo apt-get install xvfb firefox-esr
22 # SELENIUM_PATH=/home/vagrant/selenium-server-standalone-2.53.1.jar
23 # Xvfb :1 -screen 0 1024x768x24 2>&1 >/dev/null &
24 # DISPLAY=:1 java -jar $SELENIUM_PATH
28 use Time::HiRes qw(gettimeofday);
30 use C4::Biblio qw( AddBiblio ); # We shouldn't use it
32 use Test::More tests => 20;
36 my $dbh = C4::Context->dbh;
38 my $password = 'koha';
39 my $base_url= 'http://'.C4::Context->preference("staffClientBaseURL")."/cgi-bin/koha/";
41 my $number_of_biblios_to_insert = 3;
44 categorycode => 'test_cat',
45 description => 'test cat description',
46 enrolmentperiod => '12',
50 surname => 'test_patron_surname',
51 cardnumber => '4242424242',
52 userid => 'test_username',
53 password => 'password',
54 password2 => 'password'
57 our ( $borrowernumber, $start, $prev_time, $cleanup_needed );
60 eval { require Selenium::Remote::Driver; };
61 skip "Selenium::Remote::Driver is needed for selenium tests.", 20 if $@;
65 open my $fh, '>>', '/tmp/output.txt';
67 my $driver = Selenium::Remote::Driver->new;
68 $start = gettimeofday;
70 $driver->get($base_url."mainpage.pl");
71 like( $driver->get_title(), qr(Log in to Koha), );
72 auth( $driver, $login, $password );
75 $driver->get($base_url.'admin/categories.pl');
76 like( $driver->get_title(), qr(Patron categories), );
77 $driver->find_element('//a[@id="newcategory"]')->click;
78 like( $driver->get_title(), qr(New category), );
79 fill_form( $driver, $sample_data->{category} );
80 $driver->find_element('//fieldset[@class="action"]/input[@type="submit"]')->click;
82 time_diff("add patron category");
83 $driver->get($base_url.'/members/memberentry.pl?op=add&categorycode='.$sample_data->{category}{categorycode});
84 like( $driver->get_title(), qr(Add .*$sample_data->{category}{description}), );
85 fill_form( $driver, $sample_data->{patron} );
86 $driver->find_element('//fieldset[@class="action"]/input[@type="submit"]')->click;
87 like( $driver->get_title(), qr(Patron details for $sample_data->{patron}{surname}), );
89 ####$driver->get($base_url.'/members/members-home.pl');
90 ####fill_form( $driver, { searchmember => $sample_data->{patron}{cardnumber} } );
91 ####$driver->find_element('//div[@id="header_search"]/div/form/input[@type="submit"]')->click;
92 ####like( $driver->get_title(), qr(Patron details for), );
94 time_diff("add patron");
96 $borrowernumber = $dbh->selectcol_arrayref(q|SELECT borrowernumber FROM borrowers WHERE userid=?|, {}, $sample_data->{patron}{userid} )->[0];
99 for my $i ( 1 .. $number_of_biblios_to_insert ) {
100 my $biblio = MARC::Record->new();
101 my $title = 'test biblio '.$i;
102 if ( C4::Context->preference('marcflavour') eq 'UNIMARC' ) {
103 $biblio->append_fields(
104 MARC::Field->new('200', ' ', ' ', a => 'test biblio '.$i),
105 MARC::Field->new('200', ' ', ' ', f => 'test author '.$i),
108 $biblio->append_fields(
109 MARC::Field->new('245', ' ', ' ', a => 'test biblio '.$i),
110 MARC::Field->new('100', ' ', ' ', a => 'test author '.$i),
113 my ($biblionumber, $biblioitemnumber) = AddBiblio($biblio, '');
114 push @biblionumbers, $biblionumber;
117 time_diff("add biblio");
119 my $itemtype = $dbh->selectcol_arrayref(q|SELECT itemtype FROM itemtypes|);
120 $itemtype = $itemtype->[0];
122 for my $biblionumber ( @biblionumbers ) {
123 $driver->get($base_url."/cataloguing/additem.pl?biblionumber=$biblionumber");
124 like( $driver->get_title(), qr(test biblio \d+ by test author), );
125 my $form = $driver->find_element('//form[@name="f"]');
126 my $inputs = $driver->find_child_elements($form, '//input[@type="text"]');
127 for my $input ( @$inputs ) {
128 next if $input->is_hidden();
129 $input->send_keys('t_value_bib'.$biblionumber);
132 $driver->find_element('//input[@name="add_submit"]')->click;
133 like( $driver->get_title(), qr($biblionumber.*Items) );
135 $dbh->do(q|UPDATE items SET notforloan=0 WHERE biblionumber=?|, {}, $biblionumber );
136 $dbh->do(q|UPDATE biblioitems SET itemtype=? WHERE biblionumber=?|, {}, $itemtype, $biblionumber);
137 $dbh->do(q|UPDATE items SET itype=? WHERE biblionumber=?|, {}, $itemtype, $biblionumber);
140 time_diff("add items");
142 my $nb_of_checkouts = 0;
143 for my $biblionumber ( @biblionumbers ) {
144 $driver->get($base_url."/circ/circulation.pl?borrowernumber=".$borrowernumber);
145 $driver->find_element('//input[@id="barcode"]')->send_keys('t_value_bib'.$biblionumber);
146 $driver->find_element('//fieldset[@id="circ_circulation_issue"]/button[@type="submit"]')->click;
148 like( $driver->get_title(), qr(Checking out to $sample_data->{patron}{surname}) );
149 is( $driver->find_element('//a[@href="#checkouts"]')->get_attribute('text'), $nb_of_checkouts.' Checkout(s)', );
152 time_diff("checkout");
154 for my $biblionumber ( @biblionumbers ) {
155 $driver->get($base_url."/circ/returns.pl");
156 $driver->find_element('//input[@id="barcode"]')->send_keys('t_value_bib'.$biblionumber);
157 $driver->find_element('//form[@id="checkin-form"]/div/fieldset/input[@type="submit"]')->click;
158 like( $driver->get_title(), qr(Check in test biblio \d+) );
161 time_diff("checkin");
168 cleanup() if $cleanup_needed;
172 my ( $driver, $login, $password) = @_;
173 fill_form( $driver, { userid => 'koha', password => 'koha' } );
174 my $login_button = $driver->find_element('//input[@id="submit"]');
175 $login_button->submit();
179 my ( $driver, $values ) = @_;
180 while ( my ( $id, $value ) = each %$values ) {
181 my $element = $driver->find_element('//*[@id="'.$id.'"]');
182 my $tag = $element->get_tag_name();
183 if ( $tag eq 'input' ) {
184 $driver->find_element('//input[@id="'.$id.'"]')->send_keys($value);
185 } elsif ( $tag eq 'select' ) {
186 $driver->find_element('//select[@id="'.$id.'"]/option[@value="'.$value.'"]')->click;
192 my $dbh = C4::Context->dbh;
193 $dbh->do(q|DELETE FROM categories WHERE categorycode = ?|, {}, $sample_data->{category}{categorycode});
194 $dbh->do(q|DELETE FROM borrowers WHERE userid = ?|, {}, $sample_data->{patron}{userid});
195 for my $i ( 1 .. $number_of_biblios_to_insert ) {
196 $dbh->do(qq|DELETE FROM biblio WHERE title = "test biblio $i"|);
199 $dbh->do(q|DELETE FROM issues where borrowernumber=?|, {}, $borrowernumber);
200 $dbh->do(q|DELETE FROM old_issues where borrowernumber=?|, {}, $borrowernumber);
201 for my $i ( 1 .. $number_of_biblios_to_insert ) {
202 $dbh->do(qq|DELETE items, biblio FROM biblio INNER JOIN items ON biblio.biblionumber = items.biblionumber WHERE biblio.title = "test biblio$i"|);
208 my $now = gettimeofday;
209 warn "CP $lib = " . sprintf("%.2f", $now - $prev_time ) . "\n";