Bug 20568: (QA follow-up) Get rid of the id column
[koha.git] / t / lib / Selenium.pm
blob1ddab54e593dee915187a2be89ad9ff088b5963a
1 package t::lib::Selenium;
3 # This file is part of Koha.
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.
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.
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>.
19 use Modern::Perl;
20 use Carp qw( croak );
22 use C4::Context;
24 use base qw(Class::Accessor);
25 __PACKAGE__->mk_accessors(qw(login password base_url selenium_addr selenium_port driver));
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";
46 print STDERR "\n";
47 croak $selenium_error; }
49 return bless $self, $class;
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,
62 sub auth {
63 my ( $self, $login, $password ) = @_;
65 $login ||= $self->login;
66 $password ||= $self->password;
67 my $mainpage = $self->base_url . 'mainpage.pl';
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();
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;
88 sub submit_form {
89 my ( $self ) = @_;
91 my $default_submit_selector = '//fieldset[@class="action"]/input[@type="submit"]';
92 $self->click_when_visible( $default_submit_selector );
95 sub click {
96 my ( $self, $params ) = @_;
97 my $xpath_selector;
98 if ( exists $params->{main} ) {
99 $xpath_selector = '//div[@id="'.$params->{main}.'"]';
100 } elsif ( exists $params->{main_class} ) {
101 $xpath_selector = '//div[@class="'.$params->{main_class}.'"]';
103 if ( exists $params->{href} ) {
104 if ( ref( $params->{href} ) ) {
105 for my $k ( keys %{ $params->{href} } ) {
106 if ( $k eq 'ends-with' ) {
107 # ends-with version for xpath version 1
108 my $ends_with = $params->{href}{"ends-with"};
109 $xpath_selector .= '//a[substring(@href, string-length(@href) - string-length("'.$ends_with.'") + 1 ) = "'.$ends_with.'"]';
110 # ends-with version for xpath version 2
111 #$xpath_selector .= '//a[ends-with(@href, "'.$ends_with.'") ]';
113 } else {
114 die "Only ends-with is supported so far ($k)";
117 } else {
118 $xpath_selector .= '//a[contains(@href, "'.$params->{href}.'")]';
121 if ( exists $params->{id} ) {
122 $xpath_selector .= '//*[@id="'.$params->{id}.'"]';
124 $self->click_when_visible( $xpath_selector );
127 sub click_when_visible {
128 my ( $self, $xpath_selector ) = @_;
129 $self->driver->set_implicit_wait_timeout(20000);
130 my ($visible, $elt);
131 while ( not $visible ) {
132 $elt = $self->driver->find_element($xpath_selector);
133 $visible = $elt->is_displayed;
134 $self->driver->pause(1000) unless $visible;
136 $elt->click;
139 =head1 NAME
141 t::lib::Selenium - Selenium helper module
143 =head1 SYNOPSIS
145 my $s = t::lib::Selenium->new;
146 my $driver = $s->driver;
147 my $base_url = $s->base_url;
148 $s->auth;
149 $driver->get($s->base_url . 'mainpage.pl');
150 $s->fill_form({ input_id => 'value' });
152 =head1 DESCRIPTION
154 The goal of this module is to group the different actions we need
155 when we use automation test using Selenium
156 =head1 METHODS
158 =head2 new
160 my $s = t::lib::Selenium->new;
162 Constructor - Returns the object Selenium
163 You can pass login, password, base_url, selenium_addr, selenium_port
164 If not passed, the environment variables will be used
165 KOHA_USER, KOHA_PASS, KOHA_INTRANET_URL, SELENIUM_ADDR SELENIUM_PORT
166 Or koha, koha, syspref staffClientBaseURL, localhost, 4444
168 =head2 auth
170 $s->auth;
172 Will login into Koha.
174 =head2 fill_form
176 $driver->get($url)
177 $s->fill_form({
178 input_id => 'value',
179 element_id => 'other_value',
182 Will fill the different elements of a form.
183 The keys must be element ids (input and select are supported so far)
184 The values must a string.
186 =head2 submit_form
188 $s->submit_form;
190 It will submit the form using the submit button present in in the fieldset with a clas="action".
191 It should be the default way. If it does not work you should certainly fix the Koha interface.
193 =head2 click
195 $s->click
197 This is a bit dirty for now but will evolve depending on the needs
198 3 parameters possible but only the following 2 forms are used:
199 $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
200 $s->click({ id => 'element_id });
202 =head2 click_when_visible
204 $c->click_when_visible
206 Should always be called to avoid the "An element could not be located on the page" error
208 =head2
211 =head1 AUTHORS
213 Jonathan Druart <jonathan.druart@bugs.koha-community.org>
215 Alex Buckley <alexbuckley@catalyst.net.nz>
217 Koha Development Team
219 =head1 COPYRIGHT
221 Copyright 2017 - Koha Development Team
223 =head1 LICENSE
225 This file is part of Koha.
227 Koha is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
228 the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
230 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.
232 You should have received a copy of the GNU General Public License along with Koha; if not, see <http://www.gnu.org/licenses>.
234 =cut