Line 0
Link Here
|
0 |
- |
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 |
|
19 |
|
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 |
25 |
# |
26 |
# Remove the rentalcharge: |
27 |
# % UPDATE itemtypes SET rentalcharge = 0; |
28 |
# |
29 |
# Then you can execute the test file. |
30 |
# |
31 |
# If you get: |
32 |
# Wide character in print at /usr/local/share/perl/5.20.2/Test2/Formatter/TAP.pm line 105. |
33 |
# # 'Koha › Patrons › Add patron test_patron_surname (Adult)' |
34 |
# # doesn't match '(?^u:Patron details for test_patron_surname)' |
35 |
# |
36 |
# Ignore and retry (FIXME LATER...) |
37 |
|
38 |
use Modern::Perl; |
39 |
|
40 |
use Time::HiRes qw(gettimeofday); |
41 |
use C4::Context; |
42 |
use C4::Biblio qw( AddBiblio ); # We shouldn't use it |
43 |
|
44 |
use Test::More tests => 12; |
45 |
use MARC::Record; |
46 |
use MARC::Field; |
47 |
|
48 |
my $dbh = C4::Context->dbh; |
49 |
my $login = 'koha'; |
50 |
my $password = 'koha'; |
51 |
my $base_url= 'http://'.C4::Context->preference("staffClientBaseURL")."/cgi-bin/koha/"; |
52 |
my $opac_url= C4::Context->preference("OPACBaseURL"); |
53 |
|
54 |
my $number_of_biblios_to_insert = 3; |
55 |
our $sample_data = { |
56 |
category => { |
57 |
categorycode => 'test_cat', |
58 |
description => 'test cat description', |
59 |
enrolmentperiod => '12', |
60 |
category_type => 'A' |
61 |
}, |
62 |
patron => { |
63 |
surname => 'test_patron_surname', |
64 |
cardnumber => '4242424242', |
65 |
userid => 'test_username', |
66 |
password => 'password', |
67 |
password2 => 'password' |
68 |
}, |
69 |
}; |
70 |
|
71 |
my $patronusername="test_username"; |
72 |
my $patronpassword="password"; |
73 |
|
74 |
our ( $borrowernumber, $start, $prev_time, $cleanup_needed ); |
75 |
|
76 |
SKIP: { |
77 |
eval { require Selenium::Remote::Driver; }; |
78 |
skip "Selenium::Remote::Driver is needed for selenium tests.", 20 if $@; |
79 |
|
80 |
$cleanup_needed = 1; |
81 |
|
82 |
open my $fh, '>>', '/tmp/output.txt'; |
83 |
|
84 |
# Go to the mainpage and log in as default user |
85 |
my $driver = Selenium::Remote::Driver->new; |
86 |
$start = gettimeofday; |
87 |
$prev_time = $start; |
88 |
$driver->get($base_url."mainpage.pl"); |
89 |
like( $driver->get_title(), qr(Log in to Koha), ); |
90 |
auth( $driver, $login, $password ); |
91 |
time_diff("main"); |
92 |
|
93 |
#Create a patron category |
94 |
$driver->get($base_url.'admin/categories.pl'); |
95 |
like( $driver->get_title(), qr(Patron categories), ); |
96 |
$driver->find_element('//a[@id="newcategory"]')->click; |
97 |
like( $driver->get_title(), qr(New category), ); |
98 |
fill_form( $driver, $sample_data->{category} ); |
99 |
$driver->find_element('//fieldset[@class="action"]/input[@type="submit"]')->click; |
100 |
|
101 |
#Add a patron |
102 |
time_diff("add patron category"); |
103 |
$driver->get($base_url.'/members/memberentry.pl?op=add&categorycode='.$sample_data->{category}{categorycode}); |
104 |
like( $driver->get_title(), qr(Add .*$sample_data->{category}{description}), ); |
105 |
fill_form( $driver, $sample_data->{patron} ); |
106 |
$driver->find_element('//button[@id="saverecord"]')->click; |
107 |
like( $driver->get_title(), qr(Patron details for $sample_data->{patron}{surname}), ); |
108 |
|
109 |
time_diff("add patron"); |
110 |
|
111 |
|
112 |
|
113 |
#Make new biblio |
114 |
$borrowernumber = $dbh->selectcol_arrayref(q|SELECT borrowernumber FROM borrowers WHERE userid=?|, {}, $sample_data->{patron}{userid} )->[0]; |
115 |
|
116 |
my @biblionumbers; |
117 |
for my $i ( 1 .. $number_of_biblios_to_insert ) { |
118 |
my $biblio = MARC::Record->new(); |
119 |
my $title = 'test biblio '.$i; |
120 |
if ( C4::Context->preference('marcflavour') eq 'UNIMARC' ) { |
121 |
$biblio->append_fields( |
122 |
MARC::Field->new('200', ' ', ' ', a => 'test biblio '.$i), |
123 |
MARC::Field->new('200', ' ', ' ', f => 'test author '.$i), |
124 |
); |
125 |
} else { |
126 |
$biblio->append_fields( |
127 |
MARC::Field->new('245', ' ', ' ', a => 'test biblio '.$i), |
128 |
MARC::Field->new('100', ' ', ' ', a => 'test author '.$i), |
129 |
); |
130 |
} |
131 |
my ($biblionumber, $biblioitemnumber) = AddBiblio($biblio, ''); |
132 |
push @biblionumbers, $biblionumber; |
133 |
} |
134 |
|
135 |
time_diff("add biblio"); |
136 |
|
137 |
#Add items to the biblio |
138 |
my $itemtype = $dbh->selectcol_arrayref(q|SELECT itemtype FROM itemtypes|); |
139 |
$itemtype = $itemtype->[0]; |
140 |
|
141 |
for my $biblionumber ( @biblionumbers ) { |
142 |
$driver->get($base_url."/cataloguing/additem.pl?biblionumber=$biblionumber"); |
143 |
like( $driver->get_title(), qr(test biblio \d+ by test author), ); |
144 |
my $form = $driver->find_element('//form[@name="f"]'); |
145 |
my $inputs = $driver->find_child_elements($form, '//input[@type="text"]'); |
146 |
for my $input ( @$inputs ) { |
147 |
next if $input->is_hidden(); |
148 |
|
149 |
my $id = $input->get_attribute('id'); |
150 |
next unless $id =~ m|^tag_952_subfield|; |
151 |
|
152 |
$input->send_keys('t_value_bib'.$biblionumber); |
153 |
} |
154 |
|
155 |
$driver->find_element('//input[@name="add_submit"]')->click; |
156 |
like( $driver->get_title(), qr($biblionumber.*Items) ); |
157 |
|
158 |
$dbh->do(q|UPDATE items SET notforloan=0 WHERE biblionumber=?|, {}, $biblionumber ); |
159 |
$dbh->do(q|UPDATE biblioitems SET itemtype=? WHERE biblionumber=?|, {}, $itemtype, $biblionumber); |
160 |
$dbh->do(q|UPDATE items SET itype=? WHERE biblionumber=?|, {}, $itemtype, $biblionumber); |
161 |
} |
162 |
|
163 |
time_diff("add items"); |
164 |
|
165 |
# Search for items in the intranet |
166 |
$driver->get($base_url."/catalogue/search.pl"); |
167 |
$driver->find_element('//input[@title="Enter search terms"]')->send_keys("test biblio "); |
168 |
$driver->find_element('//button[@class="btn btn-default btn-sm"]')->click; |
169 |
|
170 |
my $result; |
171 |
$result = $driver->find_element_by_xpath("/html/body/div[4]/div/div[1]/div/div[2]/form"); |
172 |
if ($result) { |
173 |
time_diff("Found item in intranet"); |
174 |
} |
175 |
|
176 |
#Search for items in the OPAC |
177 |
$driver->get($opac_url); |
178 |
like( $driver->get_title(), qr(Koha online catalog), ); |
179 |
$driver->find_element('//input[@id="translControl1"]')->send_keys("test biblio "); |
180 |
$driver->find_element('//button[@id="searchsubmit"]')->click; |
181 |
|
182 |
$result = $driver->find_element_by_xpath("/html/body/div/div[4]/div/div/div[2]/div/div[2]/form[1]/table/tbody/tr[1]/td[3]"); |
183 |
|
184 |
if ($result) { |
185 |
time_diff("Found item in OPAC"); |
186 |
} |
187 |
|
188 |
close $fh; |
189 |
$driver->quit(); |
190 |
}; |
191 |
|
192 |
END { |
193 |
cleanup() if $cleanup_needed; |
194 |
}; |
195 |
|
196 |
sub auth { |
197 |
my ( $driver, $login, $password) = @_; |
198 |
fill_form( $driver, { userid => 'koha', password => 'koha' } ); |
199 |
my $login_button = $driver->find_element('//input[@id="submit"]'); |
200 |
$login_button->submit(); |
201 |
} |
202 |
|
203 |
sub fill_form { |
204 |
my ( $driver, $values ) = @_; |
205 |
while ( my ( $id, $value ) = each %$values ) { |
206 |
my $element = $driver->find_element('//*[@id="'.$id.'"]'); |
207 |
my $tag = $element->get_tag_name(); |
208 |
if ( $tag eq 'input' ) { |
209 |
$driver->find_element('//input[@id="'.$id.'"]')->send_keys($value); |
210 |
} elsif ( $tag eq 'select' ) { |
211 |
$driver->find_element('//select[@id="'.$id.'"]/option[@value="'.$value.'"]')->click; |
212 |
} |
213 |
} |
214 |
} |
215 |
|
216 |
sub cleanup { |
217 |
my $dbh = C4::Context->dbh; |
218 |
$dbh->do(q|DELETE FROM categories WHERE categorycode = ?|, {}, $sample_data->{category}{categorycode}); |
219 |
$dbh->do(q|DELETE FROM borrowers WHERE userid = ?|, {}, $sample_data->{patron}{userid}); |
220 |
for my $i ( 1 .. $number_of_biblios_to_insert ) { |
221 |
$dbh->do(qq|DELETE FROM biblio WHERE title = "test biblio $i"|); |
222 |
}; |
223 |
|
224 |
$dbh->do(q|DELETE FROM issues where borrowernumber=?|, {}, $borrowernumber); |
225 |
$dbh->do(q|DELETE FROM old_issues where borrowernumber=?|, {}, $borrowernumber); |
226 |
for my $i ( 1 .. $number_of_biblios_to_insert ) { |
227 |
$dbh->do(qq|DELETE items, biblio FROM biblio INNER JOIN items ON biblio.biblionumber = items.biblionumber WHERE biblio.title = "test biblio$i"|); |
228 |
}; |
229 |
} |
230 |
|
231 |
sub time_diff { |
232 |
my $lib = shift; |
233 |
my $now = gettimeofday; |
234 |
warn "CP $lib = " . sprintf("%.2f", $now - $prev_time ) . "\n"; |
235 |
$prev_time = $now; |
236 |
} |