|
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 |
use Koha::Exception::SystemCall; |
| 29 |
|
| 30 |
=head NAME t::lib::Page |
| 31 |
|
| 32 |
=head SYNOPSIS |
| 33 |
|
| 34 |
PageObject-pattern parent class. Extend this to implement specific pages shown to our users. |
| 35 |
|
| 36 |
PageObjects are used to make robust and reusable integration test components to test |
| 37 |
various front-end features. PageObjects load a Selenium::Remote::Driver implementation, |
| 38 |
phantomjs by default and use this to do scripted user actions in the browser, |
| 39 |
eg. clicking HTML-elements, accepting popup dialogs, entering text to input fields. |
| 40 |
|
| 41 |
PageObjects encapsulate those very low-level operations into clear and easily usable |
| 42 |
actions or services, like doPasswordLogin(). |
| 43 |
PageObjects also seamlessly deal with navigation from one page to another, eg. |
| 44 |
my $mainpage = t::lib::Page::Mainpage->new(); |
| 45 |
$mainpage->doPasswordLogin('admin', '1234')->gotoPatrons()-> |
| 46 |
searchPatrons({keywordSearch => "Jane Doe"}); |
| 47 |
|
| 48 |
=head Class variables |
| 49 |
|
| 50 |
Selenium::Remote::Driver driver, contains the driver implementation used to run these tests |
| 51 |
t::Page::Common::Header header, the header page component (not implemented) |
| 52 |
t::Page::Common::Footer footer, the footer page component (not implemented) |
| 53 |
Scalar userInteractionDelay, How many milliseconds to wait for javascript |
| 54 |
to stop processing by default after user actions? |
| 55 |
|
| 56 |
=head DEBUGGING |
| 57 |
|
| 58 |
Set Environment value |
| 59 |
$ENV{KOHA_PAGEOBJECT_DEBUG} = 1; |
| 60 |
Before creating the first PageObject to enable debugging. |
| 61 |
Debugging output is written to /tmp/PageObjectDebug/ by default, but you can change it |
| 62 |
using the same environment variable |
| 63 |
$ENV{KOHA_PAGEOBJECT_DEBUG} = "/tmp/generalDebugging/"; |
| 64 |
|
| 65 |
=cut |
| 66 |
|
| 67 |
sub new { |
| 68 |
my ($class, $params) = @_; |
| 69 |
$params = _mergeDefaultConfig($params); |
| 70 |
|
| 71 |
my $self = {}; |
| 72 |
bless($self, $class); |
| 73 |
unless ($params->{driver}) { |
| 74 |
my ($driver) = t::lib::WebDriverFactory::getUserAgentDrivers({phantomjs => $params}); |
| 75 |
$self->{driver} = $driver; |
| 76 |
} |
| 77 |
$self->{type} = $params->{type}; #This parameter is mandatory. _mergeDefaultConfig() dies without it. |
| 78 |
$self->{resource} = $params->{resource} || '/'; |
| 79 |
$self->{resource} .= "?".join('&', @{$params->{getParams}}) if $params->{getParams}; |
| 80 |
$self->{header} = $params->{header} || undef; |
| 81 |
$self->{footer} = $params->{footer} || undef; |
| 82 |
|
| 83 |
$self->{userInteractionDelay} = $params->{userInteractionDelay} || 500; |
| 84 |
|
| 85 |
$self->{driver}->set_window_size(1280, 960); |
| 86 |
$self->{driver}->get( $self->{resource} ); |
| 87 |
|
| 88 |
$self->debugSetEnvironment(); #If debugging is enabled |
| 89 |
|
| 90 |
return $self; |
| 91 |
} |
| 92 |
|
| 93 |
=head rebrandFromPageObject |
| 94 |
When we are getting redirected from one page to another we rebrand the existing PageObject |
| 95 |
as another PageObject to have the new page's services available. |
| 96 |
|
| 97 |
@RETURNS The desired new PageObject Page |
| 98 |
=cut |
| 99 |
|
| 100 |
sub rebrandFromPageObject { |
| 101 |
my ($class, $self) = @_; |
| 102 |
bless($self, $class); |
| 103 |
my $d = $self->getDriver(); |
| 104 |
$d->pause(250); #Wait for javascript to load. |
| 105 |
$self->debugTakeSessionSnapshot(); |
| 106 |
ok(1, "Navigated to $class"); |
| 107 |
return $self; |
| 108 |
} |
| 109 |
|
| 110 |
=head _mergeDefaultConfig |
| 111 |
|
| 112 |
@THROWS Koha::Exception::BadParameter |
| 113 |
=cut |
| 114 |
|
| 115 |
sub _mergeDefaultConfig { |
| 116 |
my ($params) = @_; |
| 117 |
unless (ref($params) eq 'HASH' && $params->{type}) { |
| 118 |
Koha::Exception::BadParameter->throw(error => "t::lib::Page:> When instantiating Page-objects, you must define the 'type'-parameter."); |
| 119 |
} |
| 120 |
|
| 121 |
my $testServerConfigs = C4::Context->config('testservers'); |
| 122 |
my $conf = $testServerConfigs->{ $params->{type} }; |
| 123 |
Koha::Exception::BadParameter->throw(error => "t::lib::Page:> Unknown 'type'-parameter '".$params->{type}."'. Values 'opac', 'staff' and 'rest' are supported.") |
| 124 |
unless $conf; |
| 125 |
#Merge given $params-config on top of the $KOHA_CONF's testservers-directives |
| 126 |
@$conf{keys %$params} = values %$params; |
| 127 |
return $conf; |
| 128 |
} |
| 129 |
|
| 130 |
=head quit |
| 131 |
Wrapper for Selenium::Remote::Driver->quit(), |
| 132 |
Delete the session & close open browsers. |
| 133 |
|
| 134 |
When ending this browser session, it is polite to quit, or there is a risk of leaving |
| 135 |
floating test browsers floating around. |
| 136 |
=cut |
| 137 |
|
| 138 |
sub quit { |
| 139 |
my ($self) = @_; |
| 140 |
$self->getDriver()->quit(); |
| 141 |
} |
| 142 |
|
| 143 |
=head pause |
| 144 |
Wrapper for Selenium::Remote::Driver->pause(), |
| 145 |
=cut |
| 146 |
|
| 147 |
sub pause { |
| 148 |
my ($self, $pauseMillis) = @_; |
| 149 |
$self->getDriver()->pause($pauseMillis); |
| 150 |
return $self; |
| 151 |
} |
| 152 |
|
| 153 |
=head |
| 154 |
Wrapper for Selenium::Remote::Driver->refresh() |
| 155 |
=cut |
| 156 |
|
| 157 |
sub refresh { |
| 158 |
my ($self) = @_; |
| 159 |
$self->getDriver()->refresh(); |
| 160 |
$self->debugTakeSessionSnapshot(); |
| 161 |
return $self; |
| 162 |
} |
| 163 |
|
| 164 |
=head poll |
| 165 |
Polls anonymous subroutine $func at given rate $pauseMillis for given times $polls or |
| 166 |
until $func succeeds without exceptions. |
| 167 |
|
| 168 |
In case of an exception, optional anonymous subroutine $success is called to confirm |
| 169 |
whether or not the action was successful. If this subroutine is not defined or it returns |
| 170 |
false, polling continues. |
| 171 |
|
| 172 |
Default pause for polling is 50ms and the polling runs by default for 20 times. |
| 173 |
|
| 174 |
@PARAM1 $func Anonymous subroutine to be polled |
| 175 |
@PARAM2 $success OPTIONAL Success function to check if action was successful |
| 176 |
@PARAM3 $polls OPTIONAL Defines the times polling will be ran |
| 177 |
@PARAM4 $pauseMillis OPTIONAL Defines the wait between two polls |
| 178 |
|
| 179 |
@RETURNS 1 if polling was success, otherwise die |
| 180 |
=cut |
| 181 |
|
| 182 |
sub poll { |
| 183 |
my ($self, $func, $success, $polls, $pauseMillis) = @_; |
| 184 |
|
| 185 |
# initialize default values if not given |
| 186 |
$polls = 20 unless defined $polls; |
| 187 |
$pauseMillis = 50 unless defined $pauseMillis; |
| 188 |
|
| 189 |
for (my $i = 0; $i < $polls; $i++){ |
| 190 |
eval { |
| 191 |
&$func(); |
| 192 |
}; |
| 193 |
if ($@) { |
| 194 |
return 1 if defined $success and &$success(); |
| 195 |
$self->getDriver()->pause($pauseMillis); |
| 196 |
next; |
| 197 |
} |
| 198 |
return 1 unless $@; # if no errors, return true |
| 199 |
} |
| 200 |
die $@; |
| 201 |
} |
| 202 |
|
| 203 |
=head mockConfirmPopup |
| 204 |
|
| 205 |
Workaround to a missing feature in PhantomJS v1.9 |
| 206 |
Confirm popup's cannot be negotiated with. This preparatory method makes confirm dialogs |
| 207 |
always return 'true' or 'false' without showing the actual dialog. |
| 208 |
|
| 209 |
@PARAM1 Boolean, the confirm popup dialog's return value. |
| 210 |
|
| 211 |
=cut |
| 212 |
|
| 213 |
sub mockConfirmPopup { |
| 214 |
my ($self, $retVal) = @_; |
| 215 |
my $d = $self->getDriver(); |
| 216 |
|
| 217 |
my $script = q{ |
| 218 |
var retVal = (arguments[0] == 1) ? true : false; |
| 219 |
var callback = arguments[arguments.length-1]; |
| 220 |
window.confirm = function(){return retVal;}; |
| 221 |
callback(); |
| 222 |
}; |
| 223 |
$d->execute_async_script($script, ($retVal ? 1 : 0)); |
| 224 |
} |
| 225 |
|
| 226 |
=head mockPromptPopup |
| 227 |
|
| 228 |
Workaround to a missing feature in PhantomJS v1.9 |
| 229 |
Prompt popup's cannot be negotiated with. This preparatory method makes prompt dialogs |
| 230 |
always return 'true' or 'false' without showing the actual dialog. |
| 231 |
|
| 232 |
@PARAM1 Boolean, the prompt popup dialog's return value. |
| 233 |
|
| 234 |
=cut |
| 235 |
|
| 236 |
sub mockPromptPopup { |
| 237 |
my ($self, $retVal) = @_; |
| 238 |
my $d = $self->getDriver(); |
| 239 |
|
| 240 |
my $script = q{ |
| 241 |
var callback = arguments[arguments.length-1]; |
| 242 |
window.prompt = function(){return arguments[0];}; |
| 243 |
callback(); |
| 244 |
}; |
| 245 |
$d->execute_async_script($script, ($retVal ? 1 : 0)); |
| 246 |
} |
| 247 |
|
| 248 |
=head mockAlertPopup |
| 249 |
|
| 250 |
Workaround to a missing feature in PhantomJS v1.9 |
| 251 |
Alert popup's cannot be negotiated with and they freeze PageObject testing. |
| 252 |
This preparatory method makes alert dialogs always return NULL without showing |
| 253 |
the actual dialog. |
| 254 |
|
| 255 |
=cut |
| 256 |
|
| 257 |
sub mockAlertPopup { |
| 258 |
my ($self, $retVal) = @_; |
| 259 |
my $d = $self->getDriver(); |
| 260 |
|
| 261 |
my $script = q{ |
| 262 |
var callback = arguments[arguments.length-1]; |
| 263 |
window.alert = function(){return;}; |
| 264 |
callback(); |
| 265 |
}; |
| 266 |
$d->execute_async_script($script); |
| 267 |
} |
| 268 |
|
| 269 |
################################################ |
| 270 |
## INTRODUCING OBJECT ACCESSORS ## |
| 271 |
################################################ |
| 272 |
sub setDriver { |
| 273 |
my ($self, $driver) = @_; |
| 274 |
$self->{driver} = $driver; |
| 275 |
} |
| 276 |
sub getDriver { |
| 277 |
my ($self) = @_; |
| 278 |
return $self->{driver}; |
| 279 |
} |
| 280 |
|
| 281 |
################################################ |
| 282 |
## INTRODUCING TESTING HELPERS ## |
| 283 |
################################################ |
| 284 |
sub debugSetEnvironment { |
| 285 |
my ($self) = @_; |
| 286 |
if ($ENV{KOHA_PAGEOBJECT_DEBUG}) { |
| 287 |
$self->{debugSessionId} = sprintf("%03i",rand(999)); |
| 288 |
$self->{debugSessionTmpDirectory} = "/tmp/PageObjectDebug/"; |
| 289 |
$self->{debugSessionTmpDirectory} = $ENV{KOHA_PAGEOBJECT_DEBUG} if (not(ref($ENV{KOHA_PAGEOBJECT_DEBUG})) && length($ENV{KOHA_PAGEOBJECT_DEBUG}) > 1); |
| 290 |
my $error = system(("mkdir", "-p", $self->{debugSessionTmpDirectory})); |
| 291 |
Koha::Exception::SystemCall->throw(error => "Trying to create a temporary directory for PageObject debugging session '".$self->{debugSessionId}."' failed:\n $?") |
| 292 |
if $error; |
| 293 |
$self->{debugInternalCounter} = 1; |
| 294 |
|
| 295 |
print "\n\n--Starting PageObject debugging session '".$self->{debugSessionId}."'\n\n"; |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
sub debugTakeSessionSnapshot { |
| 300 |
my ($self) = @_; |
| 301 |
if ($ENV{KOHA_PAGEOBJECT_DEBUG}) { |
| 302 |
my ($actionIdentifier, $actionFile) = $self->_debugGetSessionIdentifier(2); |
| 303 |
|
| 304 |
$self->_debugWriteHTML($actionIdentifier, $actionFile); |
| 305 |
$self->_debugWriteScreenshot($actionIdentifier, $actionFile); |
| 306 |
$self->{debugInternalCounter}++; |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
sub _debugGetSessionIdentifier { |
| 311 |
my ($self, $callerDepth) = @_; |
| 312 |
$callerDepth = $callerDepth || 2; |
| 313 |
##Create a unique and descriptive identifier for this program state. |
| 314 |
my ($package, $filename, $line, $subroutine) = caller($callerDepth); #Get where we are called from |
| 315 |
$subroutine = $2 if ($subroutine =~ /(::|->)([^:->]+)$/); #Get the last part of the package, the subroutine name. |
| 316 |
my $actionIdentifier = "[session '".$self->{debugSessionId}."', counter '".sprintf("%03i",$self->{debugInternalCounter})."', caller '$subroutine']"; |
| 317 |
my $actionFile = $self->{debugSessionId}.'_'.sprintf("%03i",$self->{debugInternalCounter}).'_'.$subroutine; |
| 318 |
return ($actionIdentifier, $actionFile); |
| 319 |
} |
| 320 |
|
| 321 |
sub _debugWriteHTML { |
| 322 |
require Data::Dumper; |
| 323 |
my ($self, $actionIdentifier, $actionFile) = @_; |
| 324 |
my $d = $self->getDriver(); |
| 325 |
|
| 326 |
##Write the current Response data |
| 327 |
open(my $fh, ">:encoding(UTF-8)", $self->{debugSessionTmpDirectory}.$actionFile.'.html') |
| 328 |
or die "Trying to open a filehandle for PageObject debugging output $actionIdentifier:\n $@"; |
| 329 |
print $fh $d->get_title()."\n"; |
| 330 |
print $fh "ALL COOKIES DUMP:\n".Data::Dumper::Dumper($d->get_all_cookies()); |
| 331 |
print $fh $d->get_page_source()."\n"; |
| 332 |
close $fh; |
| 333 |
} |
| 334 |
|
| 335 |
sub _debugWriteScreenshot { |
| 336 |
my ($self, $actionIdentifier, $actionFile) = @_; |
| 337 |
my $d = $self->getDriver(); |
| 338 |
|
| 339 |
##Write a screenshot of the view to file. |
| 340 |
my $ok = $d->capture_screenshot($self->{debugSessionTmpDirectory}.$actionFile.'.png'); |
| 341 |
Koha::Exception::SystemCall->throw(error => "Cannot capture a screenshot for PageObject $actionIdentifier") |
| 342 |
unless $ok; |
| 343 |
} |
| 344 |
1; #Make the compiler happy! |