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