Bug 19243: Add selenium tests for the administration module
[koha.git] / t / lib / Selenium.pm
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 Carp qw( croak );
21
22 use C4::Context;
23
24 use base qw(Class::Accessor);
25 __PACKAGE__->mk_accessors(qw(login password base_url selenium_addr selenium_port driver));
26
27 sub new {
28     my ( $class, $params ) = @_;
29     my $self   = {};
30     my $config = $class->config;
31     $self->{login}    = $params->{login}    || $config->{login};
32     $self->{password} = $params->{password} || $config->{password};
33     $self->{base_url} = $params->{base_url} || $config->{base_url};
34     $self->{selenium_addr} = $params->{selenium_addr} || $config->{selenium_addr};
35     $self->{selenium_port} = $params->{selenium_port} || $config->{selenium_port};
36     $self->{driver} = Selenium::Remote::Driver->new(
37         port               => $self->{selenium_port},
38         remote_server_addr => $self->{selenium_addr},
39         error_handler => sub {
40             my $selenium_error = $_[1];
41             print STDERR "\nSTRACE:";
42             my $i = 1;
43             while ( (my @call_details = (caller($i++))) ){
44                 print STDERR "\t" . $call_details[1]. ":" . $call_details[2] . " in " . $call_details[3]."\n";
45             }
46             print STDERR "\n";
47             croak $selenium_error; }
48     );
49     return bless $self, $class;
50 }
51
52 sub config {
53     return {
54         login    => $ENV{KOHA_USER} || 'koha',
55         password => $ENV{KOHA_PASS} || 'koha',
56         base_url => ( $ENV{KOHA_INTRANET_URL} || C4::Context->preference("staffClientBaseURL") ) . "/cgi-bin/koha/",
57         selenium_addr => $ENV{SELENIUM_ADDR} || 'localhost',
58         selenium_port => $ENV{SELENIUM_PORT} || 4444,
59     };
60 }
61
62 sub auth {
63     my ( $self, $login, $password ) = @_;
64
65     $login ||= $self->login;
66     $password ||= $self->password;
67     my $mainpage = $self->base_url . 'mainpage.pl';
68
69     $self->driver->get($mainpage);
70     $self->fill_form( { userid => $login, password => $password } );
71     my $login_button = $self->driver->find_element('//input[@id="submit"]');
72     $login_button->submit();
73 }
74
75 sub fill_form {
76     my ( $self, $values ) = @_;
77     while ( my ( $id, $value ) = each %$values ) {
78         my $element = $self->driver->find_element('//*[@id="'.$id.'"]');
79         my $tag = $element->get_tag_name();
80         if ( $tag eq 'input' ) {
81             $self->driver->find_element('//input[@id="'.$id.'"]')->send_keys($value);
82         } elsif ( $tag eq 'select' ) {
83             $self->driver->find_element('//select[@id="'.$id.'"]/option[@value="'.$value.'"]')->click;
84         }
85     }
86 }
87
88 sub submit_form {
89     my ( $self ) = @_;
90
91     my $default_submit_selector = '//fieldset[@class="action"]/input[@type="submit"]';
92     $self->click_when_visible( $default_submit_selector );
93 }
94
95 sub click {
96     my ( $self, $params ) = @_;
97     my $xpath_selector;
98     if ( exists $params->{main} ) {
99         $xpath_selector = '//div[@id="'.$params->{main}.'"]';
100     }
101     if ( exists $params->{href} ) {
102         $xpath_selector .= '//a[contains(@href, "'.$params->{href}.'")]';
103     }
104     if ( exists $params->{id} ) {
105         $xpath_selector .= '//*[@id="'.$params->{id}.'"]';
106     }
107     $self->click_when_visible( $xpath_selector );
108 }
109
110 sub click_when_visible {
111     my ( $self, $xpath_selector ) = @_;
112     $self->driver->set_implicit_wait_timeout(20000);
113     my ($visible, $elt);
114     while ( not $visible ) {
115         $elt = $self->driver->find_element($xpath_selector);
116         $visible = $elt->is_displayed;
117         $self->driver->pause(1000) unless $visible;
118     }
119     $elt->click;
120 }
121
122 =head1 NAME
123
124 t::lib::Selenium - Selenium helper module
125
126 =head1 SYNOPSIS
127
128     my $s = t::lib::Selenium->new;
129     my $driver = $s->driver;
130     my $base_url = $s->base_url;
131     $s->auth;
132     $driver->get($s->base_url . 'mainpage.pl');
133     $s->fill_form({ input_id => 'value' });
134
135 =head1 DESCRIPTION
136
137 The goal of this module is to group the different actions we need
138 when we use automation test using Selenium
139 =head1 METHODS
140
141 =head2 new
142
143     my $s = t::lib::Selenium->new;
144
145     Constructor - Returns the object Selenium
146     You can pass login, password, base_url, selenium_addr, selenium_port
147     If not passed, the environment variables will be used
148     KOHA_USER, KOHA_PASS, KOHA_INTRANET_URL, SELENIUM_ADDR SELENIUM_PORT
149     Or koha, koha, syspref staffClientBaseURL, localhost, 4444
150
151 =head2 auth
152
153     $s->auth;
154
155     Will login into Koha.
156
157 =head2 fill_form
158
159     $driver->get($url)
160     $s->fill_form({
161         input_id => 'value',
162         element_id => 'other_value',
163     });
164
165     Will fill the different elements of a form.
166     The keys must be element ids (input and select are supported so far)
167     The values must a string.
168
169 =head2 submit_form
170
171     $s->submit_form;
172
173     It will submit the form using the submit button present in in the fieldset with a clas="action".
174     It should be the default way. If it does not work you should certainly fix the Koha interface.
175
176 =head2 click
177
178     $s->click
179
180     This is a bit dirty for now but will evolve depending on the needs
181     3 parameters possible but only the following 2 forms are used:
182     $s->click({ href => '/module/script.pl?foo=bar', main => 'doc3' }); # Sometimes we have doc or doc3. To make sure we are not going to hit a link in the header
183     $s->click({ id => 'element_id });
184
185 =head2 click_when_visible
186
187     $c->click_when_visible
188
189     Should always be called to avoid the "An element could not be located on the page" error
190
191 =head2
192
193
194 =head1 AUTHORS
195
196 Jonathan Druart <jonathan.druart@bugs.koha-community.org>
197
198 Alex Buckley <alexbuckley@catalyst.net.nz>
199
200 Koha Development Team
201
202 =head1 COPYRIGHT
203
204 Copyright 2017 - Koha Development Team
205
206 =head1 LICENSE
207
208 This file is part of Koha.
209
210 Koha is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
211 the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
212
213 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.
214
215 You should have received a copy of the GNU General Public License along with Koha; if not, see <http://www.gnu.org/licenses>.
216
217 =cut
218
219 1;