|
Line 0
Link Here
|
|
|
1 |
package t::lib::Page; |
| 2 |
|
| 3 |
# Copyright 2015 Open Source Freedom Fighters |
| 4 |
# |
| 5 |
# This file is part of Koha. |
| 6 |
# |
| 7 |
# Koha is free software; you can redistribute it and/or modify it |
| 8 |
# under the terms of the GNU General Public License as published by |
| 9 |
# the Free Software Foundation; either version 3 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# Koha is distributed in the hope that it will be useful, but |
| 13 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License |
| 18 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
| 19 |
|
| 20 |
use Modern::Perl; |
| 21 |
use Test::More; |
| 22 |
|
| 23 |
use C4::Context; |
| 24 |
|
| 25 |
use t::lib::WebDriverFactory; |
| 26 |
|
| 27 |
use Koha::Exception::BadParameter; |
| 28 |
|
| 29 |
=head NAME t::lib::Page |
| 30 |
|
| 31 |
=head SYNOPSIS |
| 32 |
|
| 33 |
PageObject-pattern parent class. Extend this to implement specific pages shown to our users. |
| 34 |
|
| 35 |
PageObjects are used to make robust and reusable integration test components to test |
| 36 |
various front-end features. PageObjects load a Selenium::Remote::Driver implementation, |
| 37 |
phantomjs by default and use this to do scripted user actions in the browser, |
| 38 |
eg. clicking HTML-elements, accepting popup dialogs, entering text to input fields. |
| 39 |
|
| 40 |
PageObjects encapsulate those very low-level operations into clear and easily usable |
| 41 |
actions or services, like doPasswordLogin(). |
| 42 |
PageObjects also seamlessly deal with navigation from one page to another, eg. |
| 43 |
my $mainpage = t::lib::Page::Mainpage->new(); |
| 44 |
$mainpage->doPasswordLogin('admin', '1234')->gotoPatrons()-> |
| 45 |
searchPatrons({keywordSearch => "Jane Doe"}); |
| 46 |
|
| 47 |
=head Class variables |
| 48 |
|
| 49 |
Selenium::Remote::Driver driver, contains the driver implementation used to run these tests |
| 50 |
t::Page::Common::Header header, the header page component (not implemented) |
| 51 |
t::Page::Common::Footer footer, the footer page component (not implemented) |
| 52 |
|
| 53 |
=cut |
| 54 |
|
| 55 |
our $hostProtocol = 'http'; |
| 56 |
our $hostAddress = 'localhost'; |
| 57 |
our $hostPort = '443'; |
| 58 |
|
| 59 |
sub new { |
| 60 |
my ($class, $params) = @_; |
| 61 |
$params = _mergeDefaultConfig($params); |
| 62 |
|
| 63 |
my $self = {}; |
| 64 |
bless($self, $class); |
| 65 |
unless ($params->{driver}) { |
| 66 |
my ($driver) = t::lib::WebDriverFactory::getUserAgentDrivers({phantomjs => $params}); |
| 67 |
$self->{driver} = $driver; |
| 68 |
} |
| 69 |
$self->{type} = $params->{type}; #This parameter is mandatory. _mergeDefaultConfig() dies without it. |
| 70 |
$self->{resource} = $params->{resource} || '/'; |
| 71 |
$self->{resource} .= "?".join('&', @{$params->{getParams}}) if $params->{getParams}; |
| 72 |
$self->{header} = $params->{header} || undef; |
| 73 |
$self->{footer} = $params->{footer} || undef; |
| 74 |
|
| 75 |
$self->{driver}->get( $self->{resource} ); |
| 76 |
return $self; |
| 77 |
} |
| 78 |
|
| 79 |
=head _mergeDefaultConfig |
| 80 |
|
| 81 |
@THROWS Koha::Exception::BadParameter |
| 82 |
=cut |
| 83 |
|
| 84 |
sub _mergeDefaultConfig { |
| 85 |
my ($params) = @_; |
| 86 |
unless (ref($params) eq 'HASH' && $params->{type}) { |
| 87 |
Koha::Exception::BadParameter->throw(error => "t::lib::Page:> When instantiating Page-objects, you must define the 'type'-parameter."); |
| 88 |
} |
| 89 |
|
| 90 |
my $testServerConfigs = C4::Context->config('testservers'); |
| 91 |
my $conf = $testServerConfigs->{ $params->{type} }; |
| 92 |
Koha::Exception::BadParameter->throw(error => "t::lib::Page:> Unknown 'type'-parameter '".$params->{type}."'. Values 'opac', 'staff' and 'rest' are supported.") |
| 93 |
unless $conf; |
| 94 |
#Merge given $params-config on top of the $KOHA_CONF's testservers-directives |
| 95 |
@$conf{keys %$params} = values %$params; |
| 96 |
return $conf; |
| 97 |
} |
| 98 |
|
| 99 |
=head quit |
| 100 |
Wrapper for Selenium::Remote::Driver->quit(), |
| 101 |
Delete the session & close open browsers. |
| 102 |
|
| 103 |
When ending this browser session, it is polite to quit, or there is a risk of leaving |
| 104 |
floating test browsers floating around. |
| 105 |
=cut |
| 106 |
|
| 107 |
sub quit { |
| 108 |
my $self = shift; |
| 109 |
$self->getDriver()->quit(); |
| 110 |
} |
| 111 |
|
| 112 |
=head isPasswordLoginAvailable |
| 113 |
|
| 114 |
$page->isPasswordLoginAvailable(); |
| 115 |
|
| 116 |
@RETURN t::lib::Page-object |
| 117 |
@CROAK if password login is unavailable. |
| 118 |
=cut |
| 119 |
|
| 120 |
sub isPasswordLoginAvailable { |
| 121 |
my $self = shift; |
| 122 |
my $d = $self->getDriver(); |
| 123 |
|
| 124 |
_getPasswordLoginElements($d); |
| 125 |
ok(($d->get_title() =~ /Log in to Koha/), "PasswordLoginAvailable"); |
| 126 |
return $self; |
| 127 |
} |
| 128 |
sub doPasswordLogin { |
| 129 |
my ($self, $username, $password) = @_; |
| 130 |
my $d = $self->getDriver(); |
| 131 |
|
| 132 |
my ($submitButton, $useridInput, $passwordInput) = _getPasswordLoginElements($d); |
| 133 |
$useridInput->send_keys($username); |
| 134 |
$passwordInput->send_keys($password); |
| 135 |
$submitButton->click(); |
| 136 |
|
| 137 |
my $cookies = $d->get_all_cookies(); |
| 138 |
my @cgisessid = grep {$_->{name} eq 'CGISESSID'} @$cookies; |
| 139 |
|
| 140 |
ok(($d->get_title() !~ /Log in to Koha/ && #No longer in the login page |
| 141 |
$cgisessid[0]) #Cookie CGISESSID defined! |
| 142 |
, "PasswordLoginSucceeded"); |
| 143 |
|
| 144 |
return $self; #After a succesfull password login, we are directed to the same page we tried to access. |
| 145 |
} |
| 146 |
sub _getPasswordLoginElements { |
| 147 |
my $d = shift; |
| 148 |
my $submitButton = $d->find_element('#submit'); |
| 149 |
my $useridInput = $d->find_element('#userid'); |
| 150 |
my $passwordInput = $d->find_element('#password'); |
| 151 |
return ($submitButton, $useridInput, $passwordInput); |
| 152 |
} |
| 153 |
|
| 154 |
################################################ |
| 155 |
## INTRODUCING OBJECT ACCESSORS ## |
| 156 |
################################################ |
| 157 |
sub setDriver { |
| 158 |
my ($self, $driver) = @_; |
| 159 |
$self->{driver} = $driver; |
| 160 |
} |
| 161 |
sub getDriver { |
| 162 |
my ($self) = @_; |
| 163 |
return $self->{driver}; |
| 164 |
} |
| 165 |
|
| 166 |
################################################ |
| 167 |
## INTRODUCING TESTING HELPERS ## |
| 168 |
################################################ |
| 169 |
sub debugOutput { |
| 170 |
my ($self) = @_; |
| 171 |
my $driver = $self->getDriver(); |
| 172 |
|
| 173 |
print $driver->get_title()."\n"; |
| 174 |
print $driver->get_all_cookies()."\n"; |
| 175 |
print $driver->get_page_source()."\n"; |
| 176 |
} |
| 177 |
|
| 178 |
1; #Make the compiler happy! |