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

(-)a/t/db_dependent/selenium/list_workflow.t (-1 / +224 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 tests the List module specifcally it tests the ability of a user to create a list and then add items to the list. 
21
22
#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
23
24
use Modern::Perl;
25
26
use Time::HiRes qw(gettimeofday);
27
use C4::Context;
28
use C4::Biblio qw( AddBiblio ); # We shouldn't use it
29
30
use Test::More tests => 7;
31
use MARC::Record;
32
use MARC::Field;
33
use Selenium::Remote::Driver::Firefox::Profile;
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
    my $driver = Selenium::Remote::Driver->new;
72
    my $browser_profile = Selenium::Remote::Driver::Firefox::Profile->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
    #Add biblio
81
    $borrowernumber = $dbh->selectcol_arrayref(q|SELECT borrowernumber FROM borrowers WHERE userid=?|, {}, $sample_data->{patron}{userid} )->[0];
82
83
    my @biblionumbers;
84
    for my $i ( 1 .. $number_of_biblios_to_insert ) {
85
       my $biblio = MARC::Record->new();
86
       my $title = 'test biblio '.$i;
87
       if ( C4::Context->preference('marcflavour') eq 'UNIMARC' )     {
88
          $biblio->append_fields(
89
          MARC::Field->new('200', ' ', ' ', a => 'test biblio     '.$i),
90
          MARC::Field->new('200', ' ', ' ', f => 'test author     '.$i),
91
          );
92
        } else {
93
          $biblio->append_fields(
94
          MARC::Field->new('245', ' ', ' ', a => 'test biblio     '.$i),
95
          MARC::Field->new('100', ' ', ' ', a => 'test author     '.$i),
96
          );
97
        }
98
        my ($biblionumber, $biblioitemnumber) = AddBiblio($biblio,     '');
99
        push @biblionumbers, $biblionumber;
100
    }
101
    time_diff("add biblio");
102
103
    #Add items to the biblio
104
    my $itemtype = $dbh->selectcol_arrayref(q|SELECT itemtype FROM itemtypes|);
105
    $itemtype = $itemtype->[0];
106
107
    for my $biblionumber ( @biblionumbers ) {
108
        $driver->get($base_url."/cataloguing/additem.pl?biblionumber=$biblionumber");
109
        like( $driver->get_title(), qr(test biblio \d+ by test author), );
110
        my $form = $driver->find_element('//form[@name="f"]');
111
        my $inputs = $driver->find_child_elements($form, '//input[@    type="text"]');
112
        for my $input ( @$inputs ) {
113
            next if $input->is_hidden();
114
            my $id = $input->get_attribute('id');
115
            next unless $id =~ m|^tag_952_subfield|;
116
            $input->send_keys('t_value_bib'.$biblionumber);
117
         }
118
         $driver->find_element('//input[@name="add_submit"]')->click;
119
         like( $driver->get_title(), qr($biblionumber.*Items) );
120
121
         $dbh->do(q|UPDATE items SET notforloan=0 WHERE biblionumber=?|, {}, $biblionumber );
122
         $dbh->do(q|UPDATE biblioitems SET itemtype=? WHERE biblionumber=?|, {}, $itemtype, $biblionumber);
123
         $dbh->do(q|UPDATE items SET itype=? WHERE biblionumber=?|,{}, $itemtype, $biblionumber);
124
    }
125
    time_diff("add items");
126
127
    $driver->get($base_url."mainpage.pl");
128
    $driver->find_element_by_xpath('/html/body/div[4]/div/div[1]/div/div[1]/div[1]/div/ul/li[4]/a')->click;
129
    $driver->pause(20000);
130
    $driver->find_element('//a[@id="newshelf"]')->click;
131
    $driver->pause(20000);
132
    $driver->find_element('//input[@id="shelfname"]')->send_keys("Test list");
133
    $driver->find_element('//select[@id="category"]')->click;
134
    $driver->find_element_by_xpath('/html/body/div[4]/div/div/div/div[2]/div[1]/form/fieldset[1]/ol/li[4]/select/option[2]')->click;
135
    $driver->find_element_by_xpath('/html/body/div[4]/div/div/div/div[2]/div[1]/form/fieldset[2]/input')->click;
136
    if ($driver->find_element_by_xpath('/html/body/div[4]/div/div[1]/div/div[2]/span') ) {
137
        time_diff("Public list successfully created");
138
    } else {
139
        time_diff("Public list not successfully created");
140
    }
141
142
    $driver->get($base_url."mainpage.pl");
143
    $driver->find_element('//a[@id="ui-id-5"]')->click;
144
    $driver->find_element('//input[@id="search-form"]')->send_keys("Test biblio");
145
    $driver->find_element_by_xpath('/html/body/div[2]/div/div[5]/form/input[2]')->click;
146
    $driver->pause(20000);
147
    $driver->find_element_by_xpath('/html/body/div[4]/div/div[1]/div/div[2]/form/table/tbody/tr[2]/td[2]/a')->click;
148
    $driver->find_element_by_xpath('/html/body/div[4]/div/div[1]/div/div[4]/div[1]/div[2]/table/tbody/tr/td[1]/input')->click;
149
    $driver->find_element_by_xpath('/html/body/div[4]/div/div[1]/div/div[1]/div[4]/button')->click;
150
    $driver->find_element_by_xpath('/html/body/div[4]/div/div[1]/div/div[1]/div[4]/ul/li[2]/a')->click;
151
    my $handles = $driver->get_window_handles;
152
    $driver->switch_to_window($handles->[1]);
153
    $driver->pause(20000);
154
    $driver->find_element_by_xpath('/html/body/div/div/form/fieldset[3]/input[2]')->click;
155
156
    $driver->switch_to_window($handles->[0]);
157
    #Check items added to the list
158
    $driver->find_element_by_xpath('/html/body/div[1]/div[1]/ul[1]/li[5]/a')->click;
159
    $driver->find_element_by_xpath('/html/body/div[1]/div[1]/ul[1]/li[5]/ul/li[1]/a')->click;
160
    $driver->pause(20000);
161
    $driver->find_element('//a[@id="ui-id-7"]')->click;
162
    $driver->pause(20000);
163
    $driver->find_element_by_xpath('//a[text()="Test list"]')->click;
164
    $driver->pause(20000);
165
    if ($driver->find_element_by_xpath('/html/body/div[4]/div/div/div/div[2]/form/table')){
166
        time_diff("List items successfully added");
167
    } else {
168
        time_diff("List items not successfully added");
169
    }
170
171
    close $fh;
172
    $driver->quit();
173
};
174
175
END {
176
    cleanup() if $cleanup_needed;
177
};
178
179
sub auth {
180
    my ( $driver, $login, $password) = @_;
181
    fill_form( $driver, { userid => 'koha', password => 'koha' } );
182
    my $login_button = $driver->find_element('//input[@id="submit"]');
183
    $login_button->submit();
184
}
185
186
sub patron_auth {
187
    my ( $driver,$patronusername, $patronpassword) = @_;
188
    fill_form( $driver, { userid => $patronusername, password => $patronpassword } );
189
    my $login_button = $driver->find_element('//input[@id="submit"]');
190
    $login_button->submit();
191
}
192
193
sub patron_opac_auth {
194
    my ( $driver,$patronusername, $patronpassword) = @_;
195
    fill_form( $driver, { userid => $patronusername, password => $patronpassword } );
196
    my $login_button = $driver->find_element('//input[@value="Log in"]');
197
    $login_button->submit();
198
}
199
200
sub fill_form {
201
    my ( $driver, $values ) = @_;
202
    while ( my ( $id, $value ) = each %$values ) {
203
        my $element = $driver->find_element('//*[@id="'.$id.'"]');
204
        my $tag = $element->get_tag_name();
205
        if ( $tag eq 'input' ) {
206
            $driver->find_element('//input[@id="'.$id.'"]')->send_keys($value);
207
        } elsif ( $tag eq 'select' ) {
208
            $driver->find_element('//select[@id="'.$id.'"]/option[@value="'.$value.'"]')->click;
209
        }
210
    }
211
}
212
213
sub cleanup {
214
    my $dbh = C4::Context->dbh;
215
    $dbh->do(q|DELETE FROM categories WHERE categorycode = ?|, {}, $sample_data->{category}{categorycode});
216
    $dbh->do(q|DELETE FROM borrowers WHERE userid = ?|, {}, $sample_data->{patron}{userid});
217
}
218
219
sub time_diff {
220
    my $lib = shift;
221
    my $now = gettimeofday;
222
    warn "CP $lib = " . sprintf("%.2f", $now - $prev_time ) . "\n";
223
    $prev_time = $now;
224
}

Return to bug 19379