From d482aef2e420cc2466a6e645eebea9c0c8d4a19d Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 12 Dec 2017 15:42:56 -0300 Subject: [PATCH] Bug 19802: Move Selenium code to t::lib::Selenium To make it reusable easily. Test plan: The basic_workflow.t tests should still pass after this change. Signed-off-by: Mark Tompsett Signed-off-by: Jonathan Druart --- t/db_dependent/selenium/basic_workflow.t | 41 ++----- t/lib/Selenium.pm | 147 +++++++++++++++++++++++ 2 files changed, 156 insertions(+), 32 deletions(-) create mode 100644 t/lib/Selenium.pm diff --git a/t/db_dependent/selenium/basic_workflow.t b/t/db_dependent/selenium/basic_workflow.t index 116a26c7b0..420ca3ce76 100644 --- a/t/db_dependent/selenium/basic_workflow.t +++ b/t/db_dependent/selenium/basic_workflow.t @@ -42,12 +42,9 @@ use Test::More tests => 20; use MARC::Record; use MARC::Field; +use t::lib::Selenium; + my $dbh = C4::Context->dbh; -my $login = $ENV{KOHA_USER} || 'koha'; -my $password = $ENV{KOHA_PASS} || 'koha'; -my $base_url= ( $ENV{KOHA_INTRANET_URL} || C4::Context->preference("staffClientBaseURL") ) . "/cgi-bin/koha/"; -my $selenium_addr = $ENV{SELENIUM_ADDR} || 'localhost'; -my $selenium_port = $ENV{SELENIUM_PORT} || 4444; my $number_of_biblios_to_insert = 3; our $sample_data = { @@ -90,29 +87,29 @@ SKIP: { open my $fh, '>>', '/tmp/output.txt'; - my $driver = Selenium::Remote::Driver->new( - port => $selenium_port, - remote_server_addr => $selenium_addr - ); + my $s = t::lib::Selenium->new; + + my $driver = $s->driver; + my $base_url = $s->base_url; $start = gettimeofday; $prev_time = $start; $driver->get($base_url."mainpage.pl"); like( $driver->get_title(), qr(Log in to Koha), ); - auth( $driver, $login, $password ); + $s->auth; time_diff("main"); $driver->get($base_url.'admin/categories.pl'); like( $driver->get_title(), qr(Patron categories), ); $driver->find_element('//a[@id="newcategory"]')->click; like( $driver->get_title(), qr(New category), ); - fill_form( $driver, $sample_data->{category} ); + $s->fill_form( $sample_data->{category} ); $driver->find_element('//fieldset[@class="action"]/input[@type="submit"]')->click; time_diff("add patron category"); $driver->get($base_url.'/members/memberentry.pl?op=add&categorycode='.$sample_data->{category}{categorycode}); like( $driver->get_title(), qr(Add .*$sample_data->{category}{description}), ); - fill_form( $driver, $sample_data->{patron} ); + $s->fill_form( $sample_data->{patron} ); $driver->find_element('//button[@id="saverecord"]')->click; like( $driver->get_title(), qr(Patron details for $sample_data->{patron}{surname}), ); @@ -205,26 +202,6 @@ END { cleanup() if $cleanup_needed; }; -sub auth { - my ( $driver, $login, $password) = @_; - fill_form( $driver, { userid => $login, password => $password } ); - my $login_button = $driver->find_element('//input[@id="submit"]'); - $login_button->submit(); -} - -sub fill_form { - my ( $driver, $values ) = @_; - while ( my ( $id, $value ) = each %$values ) { - my $element = $driver->find_element('//*[@id="'.$id.'"]'); - my $tag = $element->get_tag_name(); - if ( $tag eq 'input' ) { - $driver->find_element('//input[@id="'.$id.'"]')->send_keys($value); - } elsif ( $tag eq 'select' ) { - $driver->find_element('//select[@id="'.$id.'"]/option[@value="'.$value.'"]')->click; - } - } -} - sub cleanup { my $dbh = C4::Context->dbh; $dbh->do(q|DELETE FROM issues where borrowernumber=?|, {}, $borrowernumber); diff --git a/t/lib/Selenium.pm b/t/lib/Selenium.pm new file mode 100644 index 0000000000..2fcd5d2091 --- /dev/null +++ b/t/lib/Selenium.pm @@ -0,0 +1,147 @@ +package t::lib::Selenium; + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# 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. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + + +use Modern::Perl; +use C4::Context; + +use base qw(Class::Accessor); +__PACKAGE__->mk_accessors(qw(login password base_url selenium_addr selenium_port driver)); + +sub new { + my ( $class, $params ) = @_; + my $self = {}; + my $config = $class->config; + $self->{login} = $params->{login} || $config->{login}; + $self->{password} = $params->{password} || $config->{password}; + $self->{base_url} = $params->{base_url} || $config->{base_url}; + $self->{selenium_addr} = $params->{selenium_addr} || $config->{selenium_addr}; + $self->{selenium_port} = $params->{selenium_port} || $config->{selenium_port}; + $self->{driver} = Selenium::Remote::Driver->new( + port => $self->{selenium_port}, + remote_server_addr => $self->{selenium_addr}, + ); + return bless $self, $class; +} + +sub config { + return { + login => $ENV{KOHA_USER} || 'koha', + password => $ENV{KOHA_PASS} || 'koha', + base_url => ( $ENV{KOHA_INTRANET_URL} || C4::Context->preference("staffClientBaseURL") ) . "/cgi-bin/koha/", + selenium_addr => $ENV{SELENIUM_ADDR} || 'localhost', + selenium_port => $ENV{SELENIUM_PORT} || 4444, + }; +} + +sub auth { + my ( $self, $login, $password ) = @_; + + $login ||= $self->login; + $password ||= $self->password; + my $mainpage = $self->base_url . 'mainpage.pl'; + + $self->driver->get($mainpage); + $self->fill_form( { userid => $login, password => $password } ); + my $login_button = $self->driver->find_element('//input[@id="submit"]'); + $login_button->submit(); +} + +sub fill_form { + my ( $self, $values ) = @_; + while ( my ( $id, $value ) = each %$values ) { + my $element = $self->driver->find_element('//*[@id="'.$id.'"]'); + my $tag = $element->get_tag_name(); + if ( $tag eq 'input' ) { + $self->driver->find_element('//input[@id="'.$id.'"]')->send_keys($value); + } elsif ( $tag eq 'select' ) { + $self->driver->find_element('//select[@id="'.$id.'"]/option[@value="'.$value.'"]')->click; + } + } +} + +=head1 NAME + +t::lib::Selenium - Selenium helper module + +=head1 SYNOPSIS + + my $s = t::lib::Selenium->new; + my $driver = $s->driver; + my $base_url = $s->base_url; + $s->auth; + $driver->get($s->base_url . 'mainpage.pl'); + $s->fill_form({ input_id => 'value' }); + +=head1 DESCRIPTION + +The goal of this module is to group the different actions we need +when we use automation test using Selenium +=head1 METHODS + +=head2 new + + my $s = t::lib::Selenium->new; + + Constructor - Returns the object Selenium + You can pass login, password, base_url, selenium_addr, selenium_port + If not passed, the environment variables will be used + KOHA_USER, KOHA_PASS, KOHA_INTRANET_URL, SELENIUM_ADDR SELENIUM_PORT + Or koha, koha, syspref staffClientBaseURL, localhost, 4444 + +=head2 auth + + $s->auth; + + Will login into Koha. + +=head2 fill_form + + $driver->get($url) + $s->fill_form({ + input_id => 'value', + element_id => 'other_value', + }); + + Will fill the different elements of a form. + The keys must be element ids (input and select are supported so far) + The values must a string. + +=head1 AUTHOR + +Jonathan Druart + +Koha Development Team + +=head1 COPYRIGHT + +Copyright 2017 - Koha Development Team + +=head1 LICENSE + +This file is part of Koha. + +Koha is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 3 of the License, or (at your option) any later version. + +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. + +You should have received a copy of the GNU General Public License along with Koha; if not, see . + +=cut + +1; -- 2.39.5