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 |
=cut |
97 |
|
98 |
sub rebrandFromPageObject { |
99 |
my ($class, $self) = @_; |
100 |
bless($self, $class); |
101 |
return $self; |
102 |
} |
103 |
|
104 |
=head _mergeDefaultConfig |
105 |
|
106 |
@THROWS Koha::Exception::BadParameter |
107 |
=cut |
108 |
|
109 |
sub _mergeDefaultConfig { |
110 |
my ($params) = @_; |
111 |
unless (ref($params) eq 'HASH' && $params->{type}) { |
112 |
Koha::Exception::BadParameter->throw(error => "t::lib::Page:> When instantiating Page-objects, you must define the 'type'-parameter."); |
113 |
} |
114 |
|
115 |
my $testServerConfigs = C4::Context->config('testservers'); |
116 |
my $conf = $testServerConfigs->{ $params->{type} }; |
117 |
Koha::Exception::BadParameter->throw(error => "t::lib::Page:> Unknown 'type'-parameter '".$params->{type}."'. Values 'opac', 'staff' and 'rest' are supported.") |
118 |
unless $conf; |
119 |
#Merge given $params-config on top of the $KOHA_CONF's testservers-directives |
120 |
@$conf{keys %$params} = values %$params; |
121 |
return $conf; |
122 |
} |
123 |
|
124 |
=head quit |
125 |
Wrapper for Selenium::Remote::Driver->quit(), |
126 |
Delete the session & close open browsers. |
127 |
|
128 |
When ending this browser session, it is polite to quit, or there is a risk of leaving |
129 |
floating test browsers floating around. |
130 |
=cut |
131 |
|
132 |
sub quit { |
133 |
my ($self) = @_; |
134 |
$self->getDriver()->quit(); |
135 |
} |
136 |
|
137 |
=head pause |
138 |
Wrapper for Selenium::Remote::Driver->pause(), |
139 |
=cut |
140 |
|
141 |
sub pause { |
142 |
my ($self, $pauseMillis) = @_; |
143 |
$self->getDriver()->pause($pauseMillis); |
144 |
return $self; |
145 |
} |
146 |
|
147 |
################################################ |
148 |
## INTRODUCING OBJECT ACCESSORS ## |
149 |
################################################ |
150 |
sub setDriver { |
151 |
my ($self, $driver) = @_; |
152 |
$self->{driver} = $driver; |
153 |
} |
154 |
sub getDriver { |
155 |
my ($self) = @_; |
156 |
return $self->{driver}; |
157 |
} |
158 |
|
159 |
################################################ |
160 |
## INTRODUCING TESTING HELPERS ## |
161 |
################################################ |
162 |
sub debugSetEnvironment { |
163 |
my ($self) = @_; |
164 |
if ($ENV{KOHA_PAGEOBJECT_DEBUG}) { |
165 |
$self->{debugSessionId} = sprintf("%03i",rand(999)); |
166 |
$self->{debugSessionTmpDirectory} = "/tmp/PageObjectDebug/"; |
167 |
$self->{debugSessionTmpDirectory} = $ENV{KOHA_PAGEOBJECT_DEBUG} if (not(ref($ENV{KOHA_PAGEOBJECT_DEBUG})) && length($ENV{KOHA_PAGEOBJECT_DEBUG}) > 1); |
168 |
my $error = system(("mkdir", "-p", $self->{debugSessionTmpDirectory})); |
169 |
Koha::Exception::SystemCall->throw(error => "Trying to create a temporary directory for PageObject debugging session '".$self->{debugSessionId}."' failed:\n $?") |
170 |
if $error; |
171 |
$self->{debugInternalCounter} = 1; |
172 |
|
173 |
print "\n\n--Starting PageObject debugging session '".$self->{debugSessionId}."'\n\n"; |
174 |
} |
175 |
} |
176 |
|
177 |
sub debugTakeSessionSnapshot { |
178 |
my ($self) = @_; |
179 |
if ($ENV{KOHA_PAGEOBJECT_DEBUG}) { |
180 |
my ($actionIdentifier, $actionFile) = $self->_debugGetSessionIdentifier(2); |
181 |
|
182 |
$self->_debugWriteHTML($actionIdentifier, $actionFile); |
183 |
$self->_debugWriteScreenshot($actionIdentifier, $actionFile); |
184 |
$self->{debugInternalCounter}++; |
185 |
} |
186 |
} |
187 |
|
188 |
sub _debugGetSessionIdentifier { |
189 |
my ($self, $callerDepth) = @_; |
190 |
$callerDepth = $callerDepth || 2; |
191 |
##Create a unique and descriptive identifier for this program state. |
192 |
my ($package, $filename, $line, $subroutine) = caller($callerDepth); #Get where we are called from |
193 |
$subroutine = $2 if ($subroutine =~ /(::|->)([^:->]+)$/); #Get the last part of the package, the subroutine name. |
194 |
my $actionIdentifier = "[session '".$self->{debugSessionId}."', counter '".sprintf("%03i",$self->{debugInternalCounter})."', caller '$subroutine']"; |
195 |
my $actionFile = $self->{debugSessionId}.'_'.sprintf("%03i",$self->{debugInternalCounter}).'_'.$subroutine; |
196 |
return ($actionIdentifier, $actionFile); |
197 |
} |
198 |
|
199 |
sub _debugWriteHTML { |
200 |
require Data::Dumper; |
201 |
my ($self, $actionIdentifier, $actionFile) = @_; |
202 |
my $d = $self->getDriver(); |
203 |
|
204 |
##Write the current Response data |
205 |
open(my $fh, ">:encoding(UTF-8)", $self->{debugSessionTmpDirectory}.$actionFile.'.html') |
206 |
or die "Trying to open a filehandle for PageObject debugging output $actionIdentifier:\n $@"; |
207 |
print $fh $d->get_title()."\n"; |
208 |
print $fh "ALL COOKIES DUMP:\n".Data::Dumper::Dumper($d->get_all_cookies()); |
209 |
print $fh $d->get_page_source()."\n"; |
210 |
close $fh; |
211 |
} |
212 |
|
213 |
sub _debugWriteScreenshot { |
214 |
my ($self, $actionIdentifier, $actionFile) = @_; |
215 |
my $d = $self->getDriver(); |
216 |
|
217 |
##Write a screenshot of the view to file. |
218 |
my $ok = $d->capture_screenshot($self->{debugSessionTmpDirectory}.$actionFile.'.png'); |
219 |
Koha::Exception::SystemCall->throw(error => "Cannot capture a screenshot for PageObject $actionIdentifier") |
220 |
unless $ok; |
221 |
} |
222 |
1; #Make the compiler happy! |