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

(-)a/t/db_dependent/selenium/basic_workflow.t (-32 / +9 lines)
Lines 42-53 use Test::More tests => 20; Link Here
42
use MARC::Record;
42
use MARC::Record;
43
use MARC::Field;
43
use MARC::Field;
44
44
45
use t::lib::Selenium;
46
45
my $dbh      = C4::Context->dbh;
47
my $dbh      = C4::Context->dbh;
46
my $login    = $ENV{KOHA_USER} || 'koha';
47
my $password = $ENV{KOHA_PASS} || 'koha';
48
my $base_url= ( $ENV{KOHA_INTRANET_URL} || C4::Context->preference("staffClientBaseURL") ) . "/cgi-bin/koha/";
49
my $selenium_addr = $ENV{SELENIUM_ADDR} || 'localhost';
50
my $selenium_port = $ENV{SELENIUM_PORT} || 4444;
51
48
52
my $number_of_biblios_to_insert = 3;
49
my $number_of_biblios_to_insert = 3;
53
our $sample_data = {
50
our $sample_data = {
Lines 90-118 SKIP: { Link Here
90
87
91
    open my $fh, '>>', '/tmp/output.txt';
88
    open my $fh, '>>', '/tmp/output.txt';
92
89
93
    my $driver = Selenium::Remote::Driver->new(
90
    my $s = t::lib::Selenium->new;
94
        port               => $selenium_port,
91
95
        remote_server_addr => $selenium_addr
92
    my $driver = $s->driver;
96
    );
93
    my $base_url = $s->base_url;
97
94
98
    $start = gettimeofday;
95
    $start = gettimeofday;
99
    $prev_time = $start;
96
    $prev_time = $start;
100
    $driver->get($base_url."mainpage.pl");
97
    $driver->get($base_url."mainpage.pl");
101
    like( $driver->get_title(), qr(Log in to Koha), );
98
    like( $driver->get_title(), qr(Log in to Koha), );
102
    auth( $driver, $login, $password );
99
    $s->auth;
103
    time_diff("main");
100
    time_diff("main");
104
101
105
    $driver->get($base_url.'admin/categories.pl');
102
    $driver->get($base_url.'admin/categories.pl');
106
    like( $driver->get_title(), qr(Patron categories), );
103
    like( $driver->get_title(), qr(Patron categories), );
107
    $driver->find_element('//a[@id="newcategory"]')->click;
104
    $driver->find_element('//a[@id="newcategory"]')->click;
108
    like( $driver->get_title(), qr(New category), );
105
    like( $driver->get_title(), qr(New category), );
109
    fill_form( $driver, $sample_data->{category} );
106
    $s->fill_form( $sample_data->{category} );
110
    $driver->find_element('//fieldset[@class="action"]/input[@type="submit"]')->click;
107
    $driver->find_element('//fieldset[@class="action"]/input[@type="submit"]')->click;
111
108
112
    time_diff("add patron category");
109
    time_diff("add patron category");
113
    $driver->get($base_url.'/members/memberentry.pl?op=add&categorycode='.$sample_data->{category}{categorycode});
110
    $driver->get($base_url.'/members/memberentry.pl?op=add&categorycode='.$sample_data->{category}{categorycode});
114
    like( $driver->get_title(), qr(Add .*$sample_data->{category}{description}), );
111
    like( $driver->get_title(), qr(Add .*$sample_data->{category}{description}), );
115
    fill_form( $driver, $sample_data->{patron} );
112
    $s->fill_form( $sample_data->{patron} );
116
    $driver->find_element('//button[@id="saverecord"]')->click;
113
    $driver->find_element('//button[@id="saverecord"]')->click;
117
    like( $driver->get_title(), qr(Patron details for $sample_data->{patron}{surname}), );
114
    like( $driver->get_title(), qr(Patron details for $sample_data->{patron}{surname}), );
118
115
Lines 205-230 END { Link Here
205
    cleanup() if $cleanup_needed;
202
    cleanup() if $cleanup_needed;
206
};
203
};
207
204
208
sub auth {
209
    my ( $driver, $login, $password) = @_;
210
    fill_form( $driver, { userid => $login, password => $password } );
211
    my $login_button = $driver->find_element('//input[@id="submit"]');
212
    $login_button->submit();
213
}
214
215
sub fill_form {
216
    my ( $driver, $values ) = @_;
217
    while ( my ( $id, $value ) = each %$values ) {
218
        my $element = $driver->find_element('//*[@id="'.$id.'"]');
219
        my $tag = $element->get_tag_name();
220
        if ( $tag eq 'input' ) {
221
            $driver->find_element('//input[@id="'.$id.'"]')->send_keys($value);
222
        } elsif ( $tag eq 'select' ) {
223
            $driver->find_element('//select[@id="'.$id.'"]/option[@value="'.$value.'"]')->click;
224
        }
225
    }
226
}
227
228
sub cleanup {
205
sub cleanup {
229
    my $dbh = C4::Context->dbh;
206
    my $dbh = C4::Context->dbh;
230
    $dbh->do(q|DELETE FROM issues where borrowernumber=?|, {}, $borrowernumber);
207
    $dbh->do(q|DELETE FROM issues where borrowernumber=?|, {}, $borrowernumber);
(-)a/t/lib/Selenium.pm (-1 / +147 lines)
Line 0 Link Here
0
- 
1
package t::lib::Selenium;
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
use Modern::Perl;
20
use C4::Context;
21
22
use base qw(Class::Accessor);
23
__PACKAGE__->mk_accessors(qw(login password base_url selenium_addr selenium_port driver));
24
25
sub new {
26
    my ( $class, $params ) = @_;
27
    my $self   = {};
28
    my $config = $class->config;
29
    $self->{login}    = $params->{login}    || $config->{login};
30
    $self->{password} = $params->{password} || $config->{password};
31
    $self->{base_url} = $params->{base_url} || $config->{base_url};
32
    $self->{selenium_addr} = $params->{selenium_addr} || $config->{selenium_addr};
33
    $self->{selenium_port} = $params->{selenium_port} || $config->{selenium_port};
34
    $self->{driver} = Selenium::Remote::Driver->new(
35
        port               => $self->{selenium_port},
36
        remote_server_addr => $self->{selenium_addr},
37
    );
38
    return bless $self, $class;
39
}
40
41
sub config {
42
    return {
43
        login    => $ENV{KOHA_USER} || 'koha',
44
        password => $ENV{KOHA_PASS} || 'koha',
45
        base_url => ( $ENV{KOHA_INTRANET_URL} || C4::Context->preference("staffClientBaseURL") ) . "/cgi-bin/koha/",
46
        selenium_addr => $ENV{SELENIUM_ADDR} || 'localhost',
47
        selenium_port => $ENV{SELENIUM_PORT} || 4444,
48
    };
49
}
50
51
sub auth {
52
    my ( $self, $login, $password ) = @_;
53
54
    $login ||= $self->login;
55
    $password ||= $self->password;
56
    my $mainpage = $self->base_url . 'mainpage.pl';
57
58
    $self->driver->get($mainpage);
59
    $self->fill_form( { userid => $login, password => $password } );
60
    my $login_button = $self->driver->find_element('//input[@id="submit"]');
61
    $login_button->submit();
62
}
63
64
sub fill_form {
65
    my ( $self, $values ) = @_;
66
    while ( my ( $id, $value ) = each %$values ) {
67
        my $element = $self->driver->find_element('//*[@id="'.$id.'"]');
68
        my $tag = $element->get_tag_name();
69
        if ( $tag eq 'input' ) {
70
            $self->driver->find_element('//input[@id="'.$id.'"]')->send_keys($value);
71
        } elsif ( $tag eq 'select' ) {
72
            $self->driver->find_element('//select[@id="'.$id.'"]/option[@value="'.$value.'"]')->click;
73
        }
74
    }
75
}
76
77
=head1 NAME
78
79
t::lib::Selenium - Selenium helper module
80
81
=head1 SYNOPSIS
82
83
    my $s = t::lib::Selenium->new;
84
    my $driver = $s->driver;
85
    my $base_url = $s->base_url;
86
    $s->auth;
87
    $driver->get($s->base_url . 'mainpage.pl');
88
    $s->fill_form({ input_id => 'value' });
89
90
=head1 DESCRIPTION
91
92
The goal of this module is to group the different actions we need
93
when we use automation test using Selenium
94
=head1 METHODS
95
96
=head2 new
97
98
    my $s = t::lib::Selenium->new;
99
100
    Constructor - Returns the object Selenium
101
    You can pass login, password, base_url, selenium_addr, selenium_port
102
    If not passed, the environment variables will be used
103
    KOHA_USER, KOHA_PASS, KOHA_INTRANET_URL, SELENIUM_ADDR SELENIUM_PORT
104
    Or koha, koha, syspref staffClientBaseURL, localhost, 4444
105
106
=head2 auth
107
108
    $s->auth;
109
110
    Will login into Koha.
111
112
=head2 fill_form
113
114
    $driver->get($url)
115
    $s->fill_form({
116
        input_id => 'value',
117
        element_id => 'other_value',
118
    });
119
120
    Will fill the different elements of a form.
121
    The keys must be element ids (input and select are supported so far)
122
    The values must a string.
123
124
=head1 AUTHOR
125
126
Jonathan Druart <jonathan.druart@bugs.koha-community.org>
127
128
Koha Development Team
129
130
=head1 COPYRIGHT
131
132
Copyright 2017 - Koha Development Team
133
134
=head1 LICENSE
135
136
This file is part of Koha.
137
138
Koha is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
139
the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
140
141
Koha is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
142
143
You should have received a copy of the GNU General Public License along with Koha; if not, see <http://www.gnu.org/licenses>.
144
145
=cut
146
147
1;

Return to bug 19802