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

(-)a/t/db_dependent/selenium/purchase_workflow.t (-1 / +252 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
27
use Modern::Perl;
28
29
use Time::HiRes qw(gettimeofday);
30
use C4::Context;
31
use C4::Biblio qw( AddBiblio ); # We shouldn't use it
32
33
use Test::More tests => 8;
34
use MARC::Record;
35
use MARC::Field;
36
37
my $dbh = C4::Context->dbh;
38
my $login = 'koha';
39
my $password = 'koha';
40
my $base_url= 'http://'.C4::Context->preference("staffClientBaseURL")."/cgi-bin/koha/";
41
my $opac_url= C4::Context->preference("OPACBaseURL");
42
43
my $number_of_biblios_to_insert = 3;
44
our $sample_data = {
45
    category => {
46
        categorycode    => 'test_cat',
47
        description     => 'test cat description',
48
        enrolmentperiod => '12',
49
        category_type   => 'A'
50
    },
51
    patron => {
52
        surname    => 'test_patron_surname',
53
        cardnumber => '4242424242',
54
        userid     => 'test_username',
55
        password   => 'password',
56
        password2  => 'password'
57
    },
58
};
59
60
my $patronusername="test_username";
61
my $patronpassword="password";
62
63
our ( $borrowernumber, $start, $prev_time, $cleanup_needed );
64
65
SKIP: {
66
    eval { require Selenium::Remote::Driver; };
67
    skip "Selenium::Remote::Driver is needed for selenium tests.", 20 if $@;
68
69
    $cleanup_needed = 1;
70
71
    open my $fh, '>>', '/tmp/output.txt';
72
73
# Go to the mainpage and log in as default user
74
    my $driver = Selenium::Remote::Driver->new;
75
    $start = gettimeofday;
76
    $prev_time = $start;
77
    $driver->get($base_url."mainpage.pl");
78
    like( $driver->get_title(), qr(Log in to Koha), );
79
    auth( $driver, $login, $password );
80
    time_diff("main");
81
82
# Make new biblio
83
    $borrowernumber = $dbh->selectcol_arrayref(q|SELECT borrowernumber FROM borrowers WHERE userid=?|, {}, $sample_data->{patron}{userid} )->[0];
84
85
    my @biblionumbers;
86
    for my $i ( 1 .. $number_of_biblios_to_insert ) {
87
        my $biblio = MARC::Record->new();
88
        my $title = 'test biblio '.$i;
89
        if ( C4::Context->preference('marcflavour') eq 'UNIMARC' ) {
90
            $biblio->append_fields(
91
                MARC::Field->new('200', ' ', ' ', a => 'test biblio '.$i),
92
                MARC::Field->new('200', ' ', ' ', f => 'test author '.$i),
93
            );
94
        } else {
95
            $biblio->append_fields(
96
                MARC::Field->new('245', ' ', ' ', a => 'test biblio '.$i),
97
                MARC::Field->new('100', ' ', ' ', a => 'test author '.$i),
98
            );
99
        }
100
        my ($biblionumber, $biblioitemnumber) = AddBiblio($biblio, '');
101
        push @biblionumbers, $biblionumber;
102
    }
103
104
    time_diff("add biblio");
105
106
# Add items to the biblio
107
    my $itemtype = $dbh->selectcol_arrayref(q|SELECT itemtype FROM itemtypes|);
108
    $itemtype = $itemtype->[0];
109
110
    for my $biblionumber ( @biblionumbers ) {
111
        $driver->get($base_url."/cataloguing/additem.pl?biblionumber=$biblionumber");
112
        like( $driver->get_title(), qr(test biblio \d+ by test author), );
113
        my $form = $driver->find_element('//form[@name="f"]');
114
        my $inputs = $driver->find_child_elements($form, '//input[@type="text"]');
115
        for my $input ( @$inputs ) {
116
            next if $input->is_hidden();
117
118
            my $id = $input->get_attribute('id');
119
            next unless $id =~ m|^tag_952_subfield|;
120
121
            $input->send_keys('t_value_bib'.$biblionumber);
122
        }
123
124
        $driver->find_element('//input[@name="add_submit"]')->click;
125
        like( $driver->get_title(), qr($biblionumber.*Items) );
126
127
        $dbh->do(q|UPDATE items SET notforloan=0 WHERE biblionumber=?|, {}, $biblionumber );
128
        $dbh->do(q|UPDATE biblioitems SET itemtype=? WHERE biblionumber=?|, {}, $itemtype, $biblionumber);
129
        $dbh->do(q|UPDATE items SET itype=? WHERE biblionumber=?|, {}, $itemtype, $biblionumber);
130
    }
131
132
    time_diff("add items");
133
134
# Load OPAC and create purchase suggestion
135
   $driver->get($opac_url);
136
   like( $driver->get_title(), qr(Koha online catalog), );
137
   warn $driver->get_title();
138
   $driver->find_element('//a[@id="listsmenu"]')->click;
139
   $driver->find_element('//a[@href="/cgi-bin/koha/opac-shelves.pl?op=list&category=1"]')->click;
140
   warn $driver->get_title();
141
   $driver->find_element_by_xpath('/html/body/div/div[4]/div/div[1]/div[1]/div/div[2]/div/ul/li[7]/a')->click;
142
   warn $driver->get_title();
143
   $driver->find_element('//a[@href="/cgi-bin/koha/opac-suggestions.pl?op=add"]')->click;
144
   $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");
145
   $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");
146
   $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");
147
   $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");
148
   $driver->find_element_by_xpath('/html/body/div/div[4]/div/div/div[2]/div/form/fieldset[1]/ol/li[8]/select')->click;
149
   $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;
150
   $driver->find_element_by_xpath('/html/body/div/div[4]/div/div/div[2]/div/form/fieldset[1]/ol/li[9]/select')->click;
151
   $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;
152
   $driver->find_element_by_xpath('/html/body/div/div[4]/div/div/div[2]/div/form/fieldset[2]/input[3]')->click;
153
   my $submittedstatus = $driver->find_element_by_xpath('/html/body/div/div[4]/div/div/div[2]/div/div')->get_text();
154
   if ($submittedstatus eq "Your suggestion has been submitted.") {
155
       time_diff("Purchase suggestion submitted");
156
   } else {
157
       time_diff("Purchase suggestion failed");
158
   }
159
160
# Process the purchase suggestion in intranet
161
   $driver->get($base_url."mainpage.pl");
162
   $driver->find_element_by_xpath('/html/body/div[4]/div/div[1]/div/div[2]/div/div/a')->click;
163
164
    warn $driver->get_title();
165
    $driver->find_element('//a[@id="CheckAllASKED"]')->click;
166
    $driver->find_element_by_xpath('/html/body/div[4]/div/div[1]/div/div[2]/div/form/fieldset[1]/div/div/select[1]')->click;
167
    $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;
168
    $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;
169
    $driver->find_element_by_xpath('/html/body/div[4]/div/div[1]/div/div[2]/div/form/fieldset[2]/input')->click;
170
    time_diff("Purchase suggestion approved in Intranet");
171
172
# Create vendor
173
    $driver->find_element_by_xpath('/html/body/div[2]/h1/a')->click;
174
    warn $driver->get_title();
175
    $driver->find_element('//a[@class="icon_general icon_acquisitions"]')->click;
176
    warn $driver->get_title();
177
    $driver ->get($base_url."acqui/supplier.pl?op=enter");
178
    warn $driver->get_title();
179
    $driver->find_element('//input[@id="company"]')->send_keys("Test vendor");
180
    $driver->find_element('//input[@value="Save"]')->click;
181
182
# Create basket
183
    $driver->find_element_by_xpath('/html/body/div[4]/div/div[1]/div/div[2]/div/span[2]/a')->click;
184
    $driver->pause(50000);
185
    $driver->find_element_by_xpath('/html/body/div[4]/div/div/form/fieldset[1]/ol/li[1]/input')->send_keys("Test basket");
186
    $driver->find_element('//input[@value="Save"]')->click;
187
188
# Add to basket
189
    $driver->find_element('//a[@href="#addtoBasket"]')->click;
190
    $driver->find_element('//a[text() = "From a suggestion"]')->click;
191
    warn $driver->get_title();
192
    $driver->find_element_by_xpath('/html/body/div[4]/div/div[1]/div/div/table/tbody/tr/td[4]/a')->click;
193
    $driver->pause(20000);
194
    $driver->find_element('//input[@value="Add item"]')->click;
195
    $driver->find_element('//select[@id="budget_id"]')->click;
196
    $driver->find_element_by_xpath("/html/body/div[4]/div/div[1]/div/form/fieldset[5]/ol/li[2]/select/option[2]")->click;
197
    $driver->find_element('//input[@value="Save"]')->click;
198
    if ($driver->find_element('//div[@id="orders_wrapper"]')) {
199
        time_diff("Purchase suggestion order placed");
200
    } else {
201
        time_diff("Purchase suggestion order not placed");
202
    }
203
204
   close $fh;
205
   $driver->quit();
206
};
207
208
END {
209
    cleanup() if $cleanup_needed;
210
};
211
212
sub auth {
213
    my ( $driver, $login, $password) = @_;
214
    fill_form( $driver, { userid => 'koha', password => 'koha' } );
215
    my $login_button = $driver->find_element('//input[@id="submit"]');
216
    $login_button->submit();
217
}
218
219
sub fill_form {
220
    my ( $driver, $values ) = @_;
221
    while ( my ( $id, $value ) = each %$values ) {
222
        my $element = $driver->find_element('//*[@id="'.$id.'"]');
223
        my $tag = $element->get_tag_name();
224
        if ( $tag eq 'input' ) {
225
            $driver->find_element('//input[@id="'.$id.'"]')->send_keys($value);
226
        } elsif ( $tag eq 'select' ) {
227
            $driver->find_element('//select[@id="'.$id.'"]/option[@value="'.$value.'"]')->click;
228
        }
229
    }
230
}
231
232
sub cleanup {
233
    my $dbh = C4::Context->dbh;
234
    $dbh->do(q|DELETE FROM categories WHERE categorycode = ?|, {}, $sample_data->{category}{categorycode});
235
    $dbh->do(q|DELETE FROM borrowers WHERE userid = ?|, {}, $sample_data->{patron}{userid});
236
    for my $i ( 1 .. $number_of_biblios_to_insert ) {
237
        $dbh->do(qq|DELETE FROM biblio WHERE title = "test biblio $i"|);
238
    };
239
240
    $dbh->do(q|DELETE FROM issues where borrowernumber=?|, {}, $borrowernumber);
241
    $dbh->do(q|DELETE FROM old_issues where borrowernumber=?|, {}, $borrowernumber);
242
    for my $i ( 1 .. $number_of_biblios_to_insert ) {
243
        $dbh->do(qq|DELETE items, biblio FROM biblio INNER JOIN items ON biblio.biblionumber = items.biblionumber WHERE biblio.title = "test biblio$i"|);
244
    };
245
}
246
247
sub time_diff {
248
    my $lib = shift;
249
    my $now = gettimeofday;
250
    warn "CP $lib = " . sprintf("%.2f", $now - $prev_time ) . "\n";
251
    $prev_time = $now;
252
}

Return to bug 18974