Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
3 |
# This file is part of Koha. |
4 |
# |
5 |
# Copyright (C) 2017 Catalyst IT |
6 |
# |
7 |
# Koha is free software; you can redistribute it and/or modify it |
8 |
# under the terms of the GNU General Public License as published by |
9 |
# the Free Software Foundation; either version 3 of the License, or |
10 |
# (at your option) any later version. |
11 |
# |
12 |
# Koha is distributed in the hope that it will be useful, but |
13 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
# GNU General Public License for more details. |
16 |
# |
17 |
# You should have received a copy of the GNU General Public License |
18 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
19 |
|
20 |
# This Selenium test goes through the item purchase workflow. Itstarts with a purchase suggestion being placed in the OPAC using the Z39.50 server |
21 |
# This suggestion is processed in the intranet, and a vendor and associated basket is created. |
22 |
# |
23 |
# If a purchase suggestion item is added to the basket successfully then the test output 'Purchase suggestion order placed' is displayed |
24 |
|
25 |
use Modern::Perl; |
26 |
|
27 |
use Time::HiRes qw(gettimeofday); |
28 |
use C4::Context; |
29 |
use C4::Biblio qw( AddBiblio ); # We shouldn't use it |
30 |
|
31 |
use Test::More tests => 2; |
32 |
use MARC::Record; |
33 |
use MARC::Field; |
34 |
|
35 |
my $dbh = C4::Context->dbh; |
36 |
my $login = 'koha'; |
37 |
my $password = 'koha'; |
38 |
my $base_url= 'http://'.C4::Context->preference("staffClientBaseURL")."/cgi-bin/koha/"; |
39 |
my $opac_url= C4::Context->preference("OPACBaseURL"); |
40 |
|
41 |
my $number_of_biblios_to_insert = 3; |
42 |
our $sample_data = { |
43 |
category => { |
44 |
categorycode => 'test_cat', |
45 |
description => 'test cat description', |
46 |
enrolmentperiod => '12', |
47 |
category_type => 'A' |
48 |
}, |
49 |
patron => { |
50 |
surname => 'test_patron_surname', |
51 |
cardnumber => '4242424242', |
52 |
userid => 'test_username', |
53 |
password => 'password', |
54 |
password2 => 'password' |
55 |
}, |
56 |
}; |
57 |
|
58 |
my $patronusername="test_username"; |
59 |
my $patronpassword="password"; |
60 |
|
61 |
our ( $borrowernumber, $start, $prev_time, $cleanup_needed ); |
62 |
|
63 |
SKIP: { |
64 |
eval { require Selenium::Remote::Driver; }; |
65 |
skip "Selenium::Remote::Driver is needed for selenium tests.", 20 if $@; |
66 |
|
67 |
$cleanup_needed = 1; |
68 |
|
69 |
open my $fh, '>>', '/tmp/output.txt'; |
70 |
|
71 |
# Go to the mainpage and log in as default user |
72 |
my $driver = Selenium::Remote::Driver->new; |
73 |
$start = gettimeofday; |
74 |
$prev_time = $start; |
75 |
$driver->get($base_url."mainpage.pl"); |
76 |
like( $driver->get_title(), qr(Log in to Koha), ); |
77 |
auth( $driver, $login, $password ); |
78 |
time_diff("main"); |
79 |
|
80 |
# Load OPAC and create purchase suggestion |
81 |
$driver->get($opac_url); |
82 |
like( $driver->get_title(), qr(Koha online catalog), ); |
83 |
$driver->pause(2000); |
84 |
$driver->find_element('//a[@id="listsmenu"]')->click; |
85 |
$driver->find_element('//a[@href="/cgi-bin/koha/opac-shelves.pl?op=list&category=1"]')->click; |
86 |
$driver->pause(20000); |
87 |
$driver->find_element_by_xpath('/html/body/div/div[4]/div/div[1]/div[1]/div/div[2]/div/ul/li[7]/a')->click; |
88 |
$driver->pause(20000); |
89 |
$driver->find_element('//a[@href="/cgi-bin/koha/opac-suggestions.pl?op=add"]')->click; |
90 |
$driver->find_element_by_xpath('/html/body/div/div[4]/div/div/div[2]/div/form/fieldset[1]/ol/li[1]/input')->send_keys("The Iliad"); |
91 |
$driver->find_element_by_xpath('/html/body/div/div[4]/div/div/div[2]/div/form/fieldset[1]/ol/li[2]/input')->send_keys("Homer"); |
92 |
$driver->find_element_by_xpath('/html/body/div/div[4]/div/div/div[2]/div/form/fieldset[1]/ol/li[4]/input')->send_keys("978-0140275360"); |
93 |
$driver->find_element_by_xpath('/html/body/div/div[4]/div/div/div[2]/div/form/fieldset[1]/ol/li[5]/input')->send_keys("Pengiun Classics"); |
94 |
$driver->find_element_by_xpath('/html/body/div/div[4]/div/div/div[2]/div/form/fieldset[1]/ol/li[8]/select')->click; |
95 |
$driver->find_element_by_xpath('/html/body/div/div[4]/div/div/div[2]/div/form/fieldset[1]/ol/li[8]/select/option[2]')->click; |
96 |
$driver->find_element_by_xpath('/html/body/div/div[4]/div/div/div[2]/div/form/fieldset[1]/ol/li[9]/select')->click; |
97 |
$driver->find_element_by_xpath('/html/body/div/div[4]/div/div/div[2]/div/form/fieldset[1]/ol/li[9]/select/option[3]')->click; |
98 |
$driver->find_element_by_xpath('/html/body/div/div[4]/div/div/div[2]/div/form/fieldset[2]/input[3]')->click; |
99 |
my $submittedstatus = $driver->find_element_by_xpath('/html/body/div/div[4]/div/div/div[2]/div/div')->get_text(); |
100 |
if ($submittedstatus eq "Your suggestion has been submitted.") { |
101 |
time_diff("Purchase suggestion submitted"); |
102 |
} else { |
103 |
time_diff("Purchase suggestion failed"); |
104 |
} |
105 |
|
106 |
# Process the purchase suggestion in intranet |
107 |
$driver->get($base_url."mainpage.pl"); |
108 |
$driver->find_element_by_xpath('/html/body/div[4]/div/div[1]/div/div[2]/div/div/a')->click; |
109 |
$driver->pause(20000); |
110 |
$driver->find_element('//a[@id="CheckAllASKED"]')->click; |
111 |
$driver->find_element_by_xpath('/html/body/div[4]/div/div[1]/div/div[2]/div/form/fieldset[1]/div/div/select[1]')->click; |
112 |
$driver->find_element_by_xpath('/html/body/div[4]/div/div[1]/div/div[2]/div/form/fieldset[1]/div/div/select[1]/option[3]')->click; $driver->find_element_by_xpath('/html/body/div[4]/div/div[1]/div/div[2]/div/form/fieldset[1]/div/div/select[2]')->click; |
113 |
$driver->find_element_by_xpath('/html/body/div[4]/div/div[1]/div/div[2]/div/form/fieldset[1]/div/div/select[2]/option[3]')->click; |
114 |
$driver->find_element_by_xpath('/html/body/div[4]/div/div[1]/div/div[2]/div/form/fieldset[2]/input')->click; |
115 |
time_diff("Purchase suggestion approved in Intranet"); |
116 |
|
117 |
# Create vendor |
118 |
$driver->find_element_by_xpath('/html/body/div[2]/h1/a')->click; |
119 |
$driver->pause(20000); |
120 |
$driver->find_element('//a[@class="icon_general icon_acquisitions"]')->click; |
121 |
$driver->pause(20000); |
122 |
$driver ->get($base_url."acqui/supplier.pl?op=enter"); |
123 |
$driver->pause(20000); |
124 |
$driver->find_element('//input[@id="company"]')->send_keys("Test vendor"); |
125 |
$driver->find_element('//input[@value="Save"]')->click; |
126 |
|
127 |
# Create basket |
128 |
$driver->find_element_by_xpath('/html/body/div[4]/div/div[1]/div/div[2]/div/span[2]/a')->click; |
129 |
$driver->pause(50000); |
130 |
$driver->find_element_by_xpath('/html/body/div[4]/div/div/form/fieldset[1]/ol/li[1]/input')->send_keys("Test basket"); |
131 |
$driver->find_element('//input[@value="Save"]')->click; |
132 |
|
133 |
# Add to basket |
134 |
$driver->find_element('//a[@href="#addtoBasket"]')->click; |
135 |
$driver->find_element('//a[text() = "From a suggestion"]')->click; |
136 |
$driver->pause(20000); |
137 |
$driver->find_element_by_xpath('/html/body/div[4]/div/div[1]/div/div/table/tbody/tr/td[4]/a')->click; |
138 |
$driver->pause(20000); |
139 |
$driver->find_element('//input[@value="Add item"]')->click; |
140 |
$driver->find_element('//select[@id="budget_id"]')->click; |
141 |
$driver->find_element_by_xpath("/html/body/div[4]/div/div[1]/div/form/fieldset[5]/ol/li[2]/select/option[2]")->click; |
142 |
$driver->find_element('//input[@value="Save"]')->click; |
143 |
if ($driver->find_element('//div[@id="orders_wrapper"]')) { |
144 |
time_diff("Purchase suggestion order placed"); |
145 |
} else { |
146 |
time_diff("Purchase suggestion order not placed"); |
147 |
} |
148 |
|
149 |
close $fh; |
150 |
$driver->quit(); |
151 |
}; |
152 |
|
153 |
END { |
154 |
cleanup() if $cleanup_needed; |
155 |
}; |
156 |
|
157 |
sub auth { |
158 |
my ( $driver, $login, $password) = @_; |
159 |
fill_form( $driver, { userid => 'koha', password => 'koha' } ); |
160 |
my $login_button = $driver->find_element('//input[@id="submit"]'); |
161 |
$login_button->submit(); |
162 |
} |
163 |
|
164 |
sub fill_form { |
165 |
my ( $driver, $values ) = @_; |
166 |
while ( my ( $id, $value ) = each %$values ) { |
167 |
my $element = $driver->find_element('//*[@id="'.$id.'"]'); |
168 |
my $tag = $element->get_tag_name(); |
169 |
if ( $tag eq 'input' ) { |
170 |
$driver->find_element('//input[@id="'.$id.'"]')->send_keys($value); |
171 |
} elsif ( $tag eq 'select' ) { |
172 |
$driver->find_element('//select[@id="'.$id.'"]/option[@value="'.$value.'"]')->click; |
173 |
} |
174 |
} |
175 |
} |
176 |
|
177 |
sub cleanup { |
178 |
my $dbh = C4::Context->dbh; |
179 |
$dbh->do(q|DELETE FROM categories WHERE categorycode = ?|, {}, $sample_data->{category}{categorycode}); |
180 |
$dbh->do(q|DELETE FROM borrowers WHERE userid = ?|, {}, $sample_data->{patron}{userid}); |
181 |
for my $i ( 1 .. $number_of_biblios_to_insert ) { |
182 |
$dbh->do(qq|DELETE FROM biblio WHERE title = "test biblio $i"|); |
183 |
}; |
184 |
|
185 |
$dbh->do(q|DELETE FROM issues where borrowernumber=?|, {}, $borrowernumber); |
186 |
$dbh->do(q|DELETE FROM old_issues where borrowernumber=?|, {}, $borrowernumber); |
187 |
for my $i ( 1 .. $number_of_biblios_to_insert ) { |
188 |
$dbh->do(qq|DELETE items, biblio FROM biblio INNER JOIN items ON biblio.biblionumber = items.biblionumber WHERE biblio.title = "test biblio$i"|); |
189 |
}; |
190 |
} |
191 |
|
192 |
sub time_diff { |
193 |
my $lib = shift; |
194 |
my $now = gettimeofday; |
195 |
warn "CP $lib = " . sprintf("%.2f", $now - $prev_time ) . "\n"; |
196 |
$prev_time = $now; |
197 |
} |