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

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

Return to bug 19182