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 t::lib::WebDriverFactory; |
24 |
|
25 |
use Koha::Exception::BadParameter; |
26 |
|
27 |
=head NAME t::lib::Page |
28 |
|
29 |
=head SYNOPSIS |
30 |
|
31 |
PageObject-pattern parent class |
32 |
|
33 |
=head Class variables |
34 |
|
35 |
Selenium::Remote::Driver driver, contains the driver implementation used to run these tests |
36 |
t::Page::Common::Header header, the header page component |
37 |
t::Page::Common::Footer footer, the footer page component |
38 |
=cut |
39 |
|
40 |
our $hostProtocol = 'http'; |
41 |
our $hostAddress = 'localhost'; |
42 |
our $hostPort = '443'; |
43 |
|
44 |
sub new { |
45 |
my ($class, $params) = @_; |
46 |
$params = _mergeDefaultConfig($params); |
47 |
|
48 |
my $self = {}; |
49 |
bless($self, $class); |
50 |
unless ($params->{driver}) { |
51 |
my ($driver) = t::lib::WebDriverFactory::getUserAgentDrivers({phantomjs => $params}); |
52 |
$self->{driver} = $driver; |
53 |
} |
54 |
$self->{type} = $params->{type}; #This parameter is mandatory. _mergeDefaultConfig() dies without it. |
55 |
$self->{resource} = $params->{resource} || '/'; |
56 |
$self->{header} = $params->{header} || undef; |
57 |
$self->{footer} = $params->{footer} || undef; |
58 |
|
59 |
$self->{driver}->get( $params->{resource} ); |
60 |
return $self; |
61 |
} |
62 |
|
63 |
=head _mergeDefaultConfig |
64 |
|
65 |
@THROWS Koha::Exception::BadParameter |
66 |
=cut |
67 |
|
68 |
sub _mergeDefaultConfig { |
69 |
my ($params) = @_; |
70 |
unless (ref($params) eq 'HASH' && $params->{type}) { |
71 |
Koha::Exception::BadParameter->throw(error => "t::lib::Page:> When instantiating Page-objects, you must define the 'type'-parameter."); |
72 |
} |
73 |
|
74 |
my $testServerConfigs = C4::Context->config('testservers'); |
75 |
my $conf = $testServerConfigs->{ $params->{type} }; |
76 |
Koha::Exception::BadParameter->throw(error => "t::lib::Page:> Unknown 'type'-parameter '".$params->{type}."'. Values 'opac', 'staff' and 'rest' are supported.") |
77 |
unless $conf; |
78 |
#Merge given $params-config on top of the $KOHA_CONF's testservers-directives |
79 |
@$conf{keys %$params} = values %$params; |
80 |
return $conf; |
81 |
} |
82 |
|
83 |
=head isPasswordLoginAvailable |
84 |
|
85 |
$page->isPasswordLoginAvailable(); |
86 |
|
87 |
@RETURN t::lib::Page-object |
88 |
@CROAK if password login is unavailable. |
89 |
=cut |
90 |
|
91 |
sub isPasswordLoginAvailable { |
92 |
my $self = shift; |
93 |
my $d = $self->getDriver(); |
94 |
|
95 |
_getPasswordLoginElements($d); |
96 |
ok(($d->get_title() =~ /Log in to Koha/), "PasswordLoginAvailable"); |
97 |
return $self; |
98 |
} |
99 |
sub doPasswordLogin { |
100 |
my ($self, $username, $password) = @_; |
101 |
my $d = $self->getDriver(); |
102 |
|
103 |
my ($submitButton, $useridInput, $passwordInput) = _getPasswordLoginElements($d); |
104 |
$useridInput->send_keys($username); |
105 |
$passwordInput->send_keys($password); |
106 |
$submitButton->click(); |
107 |
|
108 |
my $cookies = $d->get_all_cookies(); |
109 |
my @cgisessid = grep {$_->{name} eq 'CGISESSID'} @$cookies; |
110 |
|
111 |
ok(($d->get_title() !~ /Log in to Koha/ && #No longer in the login page |
112 |
$cgisessid[0]) #Cookie CGISESSID defined! |
113 |
, "PasswordLoginSucceeded"); |
114 |
|
115 |
return $self; #After a succesfull password login, we are directed to the same page we tried to access. |
116 |
} |
117 |
sub _getPasswordLoginElements { |
118 |
my $d = shift; |
119 |
my $submitButton = $d->find_element('#submit'); |
120 |
my $useridInput = $d->find_element('#userid'); |
121 |
my $passwordInput = $d->find_element('#password'); |
122 |
return ($submitButton, $useridInput, $passwordInput); |
123 |
} |
124 |
|
125 |
################################################ |
126 |
## INTRODUCING OBJECT ACCESSORS ## |
127 |
################################################ |
128 |
sub setDriver { |
129 |
my ($self, $driver) = @_; |
130 |
$self->{driver} = $driver; |
131 |
} |
132 |
sub getDriver { |
133 |
my ($self) = @_; |
134 |
return $self->{driver}; |
135 |
} |
136 |
1; #Make the compiler happy! |