View | Details | Raw Unified | Return to bug 18974
Collapse All | Expand All

(-)a/t/db_dependent/selenium/purchase_workflow.t (-1 / +259 lines)
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
#Make new biblio
94
    $borrowernumber = $dbh->selectcol_arrayref(q|SELECT borrowernumber FROM borrowers WHERE userid=?|, {}, $sample_data->{patron}{userid} )->[0];
95
96
    my @biblionumbers;
97
    for my $i ( 1 .. $number_of_biblios_to_insert ) {
98
        my $biblio = MARC::Record->new();
99
        my $title = 'test biblio '.$i;
100
        if ( C4::Context->preference('marcflavour') eq 'UNIMARC' ) {
101
            $biblio->append_fields(
102
                MARC::Field->new('200', ' ', ' ', a => 'test biblio '.$i),
103
                MARC::Field->new('200', ' ', ' ', f => 'test author '.$i),
104
            );
105
        } else {
106
            $biblio->append_fields(
107
                MARC::Field->new('245', ' ', ' ', a => 'test biblio '.$i),
108
                MARC::Field->new('100', ' ', ' ', a => 'test author '.$i),
109
            );
110
        }
111
        my ($biblionumber, $biblioitemnumber) = AddBiblio($biblio, '');
112
        push @biblionumbers, $biblionumber;
113
    }
114
115
    time_diff("add biblio");
116
117
#Add items to the biblio
118
    my $itemtype = $dbh->selectcol_arrayref(q|SELECT itemtype FROM itemtypes|);
119
    $itemtype = $itemtype->[0];
120
121
    for my $biblionumber ( @biblionumbers ) {
122
        $driver->get($base_url."/cataloguing/additem.pl?biblionumber=$biblionumber");
123
        like( $driver->get_title(), qr(test biblio \d+ by test author), );
124
        my $form = $driver->find_element('//form[@name="f"]');
125
        my $inputs = $driver->find_child_elements($form, '//input[@type="text"]');
126
        for my $input ( @$inputs ) {
127
            next if $input->is_hidden();
128
129
            my $id = $input->get_attribute('id');
130
            next unless $id =~ m|^tag_952_subfield|;
131
132
            $input->send_keys('t_value_bib'.$biblionumber);
133
        }
134
135
        $driver->find_element('//input[@name="add_submit"]')->click;
136
        like( $driver->get_title(), qr($biblionumber.*Items) );
137
138
        $dbh->do(q|UPDATE items SET notforloan=0 WHERE biblionumber=?|, {}, $biblionumber );
139
        $dbh->do(q|UPDATE biblioitems SET itemtype=? WHERE biblionumber=?|, {}, $itemtype, $biblionumber);
140
        $dbh->do(q|UPDATE items SET itype=? WHERE biblionumber=?|, {}, $itemtype, $biblionumber);
141
    }
142
143
    time_diff("add items");
144
145
#Search for items in the OPAC
146
   $driver->get($opac_url);
147
   like( $driver->get_title(), qr(Koha online catalog), );
148
   $driver->find_element_by_xpath('/html/body/div/div[1]/div/div/div/ul/li[3]/a/i')->click;
149
   $driver->find_element_by_xpath('/html/body/div/div[1]/div/div/div/ul/li[3]/ul/li[3]/a')->click;
150
   $driver->find_element_by_xpath('/html/body/div/div[4]/div/div[1]/div[1]/div/div[2]/div/ul/li[8]/a')->click;
151
152
#Purchase suggestion page
153
   $driver->find_element('//a[@href="/cgi-bin/koha/opac-suggestions.pl?op=add"]')->click;
154
155
#Create purchase suggestion page
156
   $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");
157
   $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");
158
   $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");
159
   $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");
160
   $driver->find_element_by_xpath('/html/body/div/div[4]/div/div/div[2]/div/form/fieldset[1]/ol/li[8]/select')->click;
161
   $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;
162
   $driver->find_element_by_xpath('/html/body/div/div[4]/div/div/div[2]/div/form/fieldset[1]/ol/li[9]/select')->click;
163
   $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;
164
   $driver->find_element_by_xpath('/html/body/div/div[4]/div/div/div[2]/div/form/fieldset[2]/input[3]')->click;
165
   my $submittedstatus = $driver->find_element_by_xpath('/html/body/div/div[4]/div/div/div[2]/div/div')->get_text();
166
   if ($submittedstatus eq "Your suggestion has been submitted.") {
167
       time_diff("Purchase suggestion submitted");
168
   } else {
169
       time_diff("Purchase suggestion failed");
170
   }
171
172
# Process the purchase suggestion in intranet
173
   $driver->get($base_url."mainpage.pl");
174
   $driver->find_element_by_xpath('/html/body/div[4]/div/div[1]/div/div[2]/div/div/a')->click;
175
176
    warn $driver->get_title();
177
   $driver->find_element('//a[@id="CheckAllASKED"]')->click;
178
    $driver->find_element_by_xpath('/html/body/div[4]/div/div[1]/div/div[2]/div/form/fieldset[1]/div/div/select[1]')->click;
179
    $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;
180
    $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;
181
    $driver->find_element_by_xpath('/html/body/div[4]/div/div[1]/div/div[2]/div/form/fieldset[2]/input')->click;
182
    time_diff("Purchase suggestion approved in Intranet");
183
184
    $driver->find_element_by_xpath('/html/body/div[2]/h1/a')->click;
185
    warn $driver->get_title();
186
    $driver->find_element('//a[@class="icon_general icon_acquisitions"]')->click;
187
    warn $driver->get_title();
188
    $driver ->get($base_url."acqui/supplier.pl?op=enter");
189
    warn $driver->get_title();
190
    $driver->find_element('//input[@id="company"]')->send_keys("Test vendor");
191
    $driver->find_element('//input[@value="Save"]')->click;
192
193
#Create basket
194
    $driver->find_element_by_xpath('/html/body/div[4]/div/div[1]/div/div[2]/div/span[2]/a')->click;
195
    $driver->find_element('//input[@id="basketname"]')->send_keys("Test basket");
196
    $driver->find_element('//input[@value="Save"]')->click;
197
198
#Add to basket
199
    $driver->find_element_by_xpath('/html/body/div[4]/div/div[1]/div/div[1]/div[1]')->click;
200
    $driver->find_element_by_xpath('/html/body/div[4]/div/div[1]/div/div[6]/div/div/div[1]/fieldset/ul/li[2]/a')->click;
201
    $driver->find_element('//a[@href="neworderempty.pl?booksellerid=1&basketno=1&suggestionid=1"]')->click;
202
203
    $driver->find_element_by_xpath('/html/body/div[4]/div/div[1]/div/form/fieldset[4]/div[2]/div/fieldset/input[1]')->click;
204
    $driver->find_element_by_xpath('/html/body/div[4]/div/div[1]/div/form/fieldset[6]/input[2]')->click;
205
    if ($driver->find_element('//div[@id="orders_wrapper"]')) {
206
        time_diff("Purchase suggestion order placed");
207
    } else {
208
        time_diff("Purchase suggestion order not placed");
209
    }
210
211
   close $fh;
212
   $driver->quit();
213
};
214
215
END {
216
    cleanup() if $cleanup_needed;
217
};
218
219
sub auth {
220
    my ( $driver, $login, $password) = @_;
221
    fill_form( $driver, { userid => 'koha', password => 'koha' } );
222
    my $login_button = $driver->find_element('//input[@id="submit"]');
223
    $login_button->submit();
224
}
225
226
sub fill_form {
227
    my ( $driver, $values ) = @_;
228
    while ( my ( $id, $value ) = each %$values ) {
229
        my $element = $driver->find_element('//*[@id="'.$id.'"]');
230
        my $tag = $element->get_tag_name();
231
        if ( $tag eq 'input' ) {
232
            $driver->find_element('//input[@id="'.$id.'"]')->send_keys($value);
233
        } elsif ( $tag eq 'select' ) {
234
            $driver->find_element('//select[@id="'.$id.'"]/option[@value="'.$value.'"]')->click;
235
        }
236
    }
237
}
238
239
sub cleanup {
240
    my $dbh = C4::Context->dbh;
241
    $dbh->do(q|DELETE FROM categories WHERE categorycode = ?|, {}, $sample_data->{category}{categorycode});
242
    $dbh->do(q|DELETE FROM borrowers WHERE userid = ?|, {}, $sample_data->{patron}{userid});
243
    for my $i ( 1 .. $number_of_biblios_to_insert ) {
244
        $dbh->do(qq|DELETE FROM biblio WHERE title = "test biblio $i"|);
245
    };
246
247
    $dbh->do(q|DELETE FROM issues where borrowernumber=?|, {}, $borrowernumber);
248
    $dbh->do(q|DELETE FROM old_issues where borrowernumber=?|, {}, $borrowernumber);
249
    for my $i ( 1 .. $number_of_biblios_to_insert ) {
250
        $dbh->do(qq|DELETE items, biblio FROM biblio INNER JOIN items ON biblio.biblionumber = items.biblionumber WHERE biblio.title = "test biblio$i"|);
251
    };
252
}
253
254
sub time_diff {
255
    my $lib = shift;
256
    my $now = gettimeofday;
257
    warn "CP $lib = " . sprintf("%.2f", $now - $prev_time ) . "\n";
258
    $prev_time = $now;
259
}

Return to bug 18974