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

(-)a/t/db_dependent/selenium/reporting_workflow.t (-1 / +212 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 functionality to create and save a report.
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
    #Navigate into Reports and create a new report
128
    $driver->get($base_url."mainpage.pl");
129
    $driver->find_element_by_xpath('/html/body/div[4]/div/div[1]/div/div[1]/div[2]/div/ul/li[3]/a')->click;
130
    $driver->pause(20000);
131
    $driver->find_element_by_xpath('/html/body/div[4]/div/div[1]/div/div[1]/ul[1]/li[2]/a')->click;
132
    $driver->pause(20000);
133
    $driver->find_element_by_xpath('/html/body/div[5]/div/div[1]/div/form/fieldset[1]/ol/li[1]/select')->click;
134
    $driver->find_element_by_xpath('/html/body/div[5]/div/div[1]/div/form/fieldset[1]/ol/li[1]/select/option[2]')->click;
135
    $driver->find_element_by_xpath('/html/body/div[5]/div/div[1]/div/form/fieldset[2]/input[2]')->click;
136
    $driver->find_element_by_xpath('/html/body/div[5]/div/div[1]/div[1]/form/fieldset[2]/input[3]')->click;
137
    $driver->find_element_by_xpath('/html/body/div[5]/div/div[1]/div/form/fieldset/div/div[1]/div[1]/select/optgroup[1]/option[1]')->click;
138
    $driver->find_element_by_xpath('/html/body/div[5]/div/div[1]/div/form/fieldset/div/div[1]/div[2]/input[1]')->click;
139
    $driver->find_element_by_xpath('/html/body/div[5]/div/div[1]/div/form/fieldset/div/div[1]/div[1]/select/optgroup[1]/option[2]')->click;
140
    $driver->find_element_by_xpath('/html/body/div[5]/div/div[1]/div/form/fieldset/div/div[1]/div[2]/input[1]')->click;
141
    $driver->find_element_by_xpath('/html/body/div[5]/div/div[1]/div/form/div/fieldset/input[3]')->click;
142
    $driver->find_element_by_xpath('/html/body/div[5]/div/div[1]/div/form/fieldset[2]/input[3]')->click;
143
    $driver->find_element_by_xpath('/html/body/div[5]/div/div[1]/div/form/fieldset[2]/input[3]')->click;
144
    $driver->pause(20000);
145
    $driver->find_element_by_xpath('/html/body/div[5]/div/div[1]/div/form/fieldset[2]/input[2]')->click;
146
    $driver->pause(20000);
147
    $driver->find_element_by_xpath('/html/body/div[5]/div/div[1]/div/form/fieldset/input[2]')->click;
148
    $driver->pause(20000);
149
    $driver->find_element('//input[@id="reportname"]')->send_keys("Test report");
150
    $driver->find_element_by_xpath('/html/body/div[5]/div/div[1]/div/form/fieldset[1]/ol/li[4]/textarea')->send_keys("This test retrieves item information from the catalog");
151
    $driver->find_element_by_xpath('/html/body/div[5]/div/div[1]/div/form/fieldset[2]/input[2]')->click;
152
    $driver->find_element_by_xpath('/html/body/div[5]/div/div[1]/div/ul/li[1]/a')->click;
153
    if ($driver->find_element_by_xpath('/html/body/div[5]/div/div[1]/div/table/tbody/tr[1]/th[1]')) {
154
        time_diff("Report successfully created");
155
    } else {
156
        time_diff("Report not successfully created");
157
    }
158
159
    close $fh;
160
    $driver->quit();
161
};
162
163
END {
164
    cleanup() if $cleanup_needed;
165
};
166
167
sub auth {
168
    my ( $driver, $login, $password) = @_;
169
    fill_form( $driver, { userid => 'koha', password => 'koha' } );
170
    my $login_button = $driver->find_element('//input[@id="submit"]');
171
    $login_button->submit();
172
}
173
174
sub patron_auth {
175
    my ( $driver,$patronusername, $patronpassword) = @_;
176
    fill_form( $driver, { userid => $patronusername, password => $patronpassword } );
177
    my $login_button = $driver->find_element('//input[@id="submit"]');
178
    $login_button->submit();
179
}
180
181
sub patron_opac_auth {
182
    my ( $driver,$patronusername, $patronpassword) = @_;
183
    fill_form( $driver, { userid => $patronusername, password => $patronpassword } );
184
    my $login_button = $driver->find_element('//input[@value="Log in"]');
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
}
206
207
sub time_diff {
208
    my $lib = shift;
209
    my $now = gettimeofday;
210
    warn "CP $lib = " . sprintf("%.2f", $now - $prev_time ) . "\n";
211
    $prev_time = $now;
212
}

Return to bug 19359