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