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 is to testb authentication, by performing the following: create a category and patron (same as basic_workflow.t). Then the superlibrarian logs out and the created patron must log into the staff intranet and OPAC |
21 |
|
22 |
# 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 |
23 |
# sudo apt-get install xvfb firefox-esr |
24 |
# SELENIUM_PATH=/home/vagrant/selenium-server-standalone-2.53.1.jar |
25 |
# Xvfb :1 -screen 0 1024x768x24 2>&1 >/dev/null & |
26 |
# DISPLAY=:1 java -jar $SELENIUM_PATH |
27 |
# |
28 |
# Remove the rentalcharge: |
29 |
# % UPDATE itemtypes SET rentalcharge = 0; |
30 |
# |
31 |
# Then you can execute the test file. |
32 |
# |
33 |
# If you get: |
34 |
# Wide character in print at /usr/local/share/perl/5.20.2/Test2/Formatter/TAP.pm line 105. |
35 |
# # 'Koha › Patrons › Add patron test_patron_surname (Adult)' |
36 |
# # doesn't match '(?^u:Patron details for test_patron_surname)' |
37 |
# |
38 |
# Ignore and retry (FIXME LATER...) |
39 |
|
40 |
#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 |
41 |
|
42 |
use Modern::Perl; |
43 |
|
44 |
use Time::HiRes qw(gettimeofday); |
45 |
use C4::Context; |
46 |
use C4::Biblio qw( AddBiblio ); # We shouldn't use it |
47 |
|
48 |
use Test::More tests => 9; |
49 |
use MARC::Record; |
50 |
use MARC::Field; |
51 |
|
52 |
my $dbh = C4::Context->dbh; |
53 |
my $login = 'koha'; |
54 |
my $password = 'koha'; |
55 |
my $base_url= 'http://'.C4::Context->preference("staffClientBaseURL")."/cgi-bin/koha/"; |
56 |
my $opac_url= C4::Context->preference("OPACBaseURL"); |
57 |
|
58 |
our $sample_data = { |
59 |
category => { |
60 |
categorycode => 'test_cat', |
61 |
description => 'test cat description', |
62 |
enrolmentperiod => '12', |
63 |
category_type => 'A' |
64 |
}, |
65 |
patron => { |
66 |
surname => 'test_patron_surname', |
67 |
cardnumber => '4242424242', |
68 |
userid => 'test_username', |
69 |
password => 'password', |
70 |
password2 => 'password' |
71 |
}, |
72 |
}; |
73 |
|
74 |
my $patronusername="test_username"; |
75 |
my $patronpassword="password"; |
76 |
|
77 |
our ( $borrowernumber, $start, $prev_time, $cleanup_needed ); |
78 |
|
79 |
SKIP: { |
80 |
eval { require Selenium::Remote::Driver; }; |
81 |
skip "Selenium::Remote::Driver is needed for selenium tests.", 20 if $@; |
82 |
|
83 |
$cleanup_needed = 1; |
84 |
|
85 |
open my $fh, '>>', '/tmp/output.txt'; |
86 |
|
87 |
my $driver = Selenium::Remote::Driver->new; |
88 |
$start = gettimeofday; |
89 |
$prev_time = $start; |
90 |
$driver->get($base_url."mainpage.pl"); |
91 |
like( $driver->get_title(), qr(Log in to Koha), ); |
92 |
auth( $driver, $login, $password ); |
93 |
time_diff("main"); |
94 |
|
95 |
$driver->get($base_url.'admin/categories.pl'); |
96 |
like( $driver->get_title(), qr(Patron categories), ); |
97 |
$driver->find_element('//a[@id="newcategory"]')->click; |
98 |
like( $driver->get_title(), qr(New category), ); |
99 |
fill_form( $driver, $sample_data->{category} ); |
100 |
$driver->find_element('//fieldset[@class="action"]/input[@type="submit"]')->click; |
101 |
|
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 |
time_diff("add patron"); |
109 |
|
110 |
$driver->get($base_url.'/mainpage.pl?logout.x=1'); |
111 |
like( $driver->get_title(), qr(Log in to Koha), ); |
112 |
time_diff("Logout"); |
113 |
|
114 |
$driver->get($base_url."mainpage.pl"); |
115 |
like( $driver->get_title(), qr(Log in to Koha), ); |
116 |
patron_auth( $driver, $sample_data->{patron} ); |
117 |
time_diff("New patron logs into intranet"); |
118 |
|
119 |
$driver->get($base_url.'/mainpage.pl?logout.x=1'); |
120 |
like( $driver->get_title(), qr(Log in to Koha), ); |
121 |
time_diff("Logout of new patron from staff intranet"); |
122 |
|
123 |
$driver->get($opac_url); |
124 |
like( $driver->get_title(), qr(Koha online catalog), ); |
125 |
patron_opac_auth( $driver, $sample_data->{patron} ); |
126 |
time_diff("New patron logs into OPAC"); |
127 |
|
128 |
close $fh; |
129 |
$driver->quit(); |
130 |
}; |
131 |
|
132 |
END { |
133 |
cleanup() if $cleanup_needed; |
134 |
}; |
135 |
|
136 |
sub auth { |
137 |
my ( $driver, $login, $password) = @_; |
138 |
fill_form( $driver, { userid => 'koha', password => 'koha' } ); |
139 |
my $login_button = $driver->find_element('//input[@id="submit"]'); |
140 |
$login_button->submit(); |
141 |
} |
142 |
|
143 |
sub patron_auth { |
144 |
my ( $driver,$patronusername, $patronpassword) = @_; |
145 |
fill_form( $driver, { userid => $patronusername, password => $patronpassword } ); |
146 |
my $login_button = $driver->find_element('//input[@id="submit"]'); |
147 |
$login_button->submit(); |
148 |
} |
149 |
|
150 |
sub patron_opac_auth { |
151 |
my ( $driver,$patronusername, $patronpassword) = @_; |
152 |
fill_form( $driver, { userid => $patronusername, password => $patronpassword } ); |
153 |
my $login_button = $driver->find_element('//input[@value="Log in"]'); |
154 |
$login_button->submit(); |
155 |
} |
156 |
|
157 |
sub fill_form { |
158 |
my ( $driver, $values ) = @_; |
159 |
while ( my ( $id, $value ) = each %$values ) { |
160 |
my $element = $driver->find_element('//*[@id="'.$id.'"]'); |
161 |
my $tag = $element->get_tag_name(); |
162 |
if ( $tag eq 'input' ) { |
163 |
$driver->find_element('//input[@id="'.$id.'"]')->send_keys($value); |
164 |
} elsif ( $tag eq 'select' ) { |
165 |
$driver->find_element('//select[@id="'.$id.'"]/option[@value="'.$value.'"]')->click; |
166 |
} |
167 |
} |
168 |
} |
169 |
|
170 |
sub cleanup { |
171 |
my $dbh = C4::Context->dbh; |
172 |
$dbh->do(q|DELETE FROM categories WHERE categorycode = ?|, {}, $sample_data->{category}{categorycode}); |
173 |
$dbh->do(q|DELETE FROM borrowers WHERE userid = ?|, {}, $sample_data->{patron}{userid}); |
174 |
} |
175 |
|
176 |
sub time_diff { |
177 |
my $lib = shift; |
178 |
my $now = gettimeofday; |
179 |
warn "CP $lib = " . sprintf("%.2f", $now - $prev_time ) . "\n"; |
180 |
$prev_time = $now; |
181 |
} |