Line 0
Link Here
|
|
|
1 |
package t::lib::WebDriverFactory; |
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 |
|
22 |
use Koha::Exception::VersionMismatch; |
23 |
use Koha::Exception::UnknownProgramState; |
24 |
|
25 |
=head NAME t::lib::WebDriverFactory |
26 |
|
27 |
=head SYNOPSIS |
28 |
|
29 |
This Factory is responsible for creating all WebTesters/WebDrivers supported by Koha. |
30 |
|
31 |
=cut |
32 |
|
33 |
=head getUserAgentDrivers |
34 |
|
35 |
my ($phantomjs) = getUserAgentDrivers('phantomjs'); |
36 |
my ($phantomjs, $ff) = getUserAgentDrivers(['phantomjs', 'firefox']); |
37 |
my ($firefox) = getUserAgentDrivers({firefox => { version => '39.0', platform => 'LINUX' }}); |
38 |
my ($ff1, $ff2) = getUserAgentDrivers({firefox1 => { version => '39.0', platform => 'LINUX' }, |
39 |
firefox2 => { version => '38.0', platform => 'WINDOWS' }, #yuck... |
40 |
}); |
41 |
|
42 |
Test Driver factory-method to get various web-userAgents. |
43 |
This is a direct wrapper for Selenium::Remote::Driver->new(), check the valid parameters from it's perldoc. |
44 |
|
45 |
Default configuration: |
46 |
{ |
47 |
javascript => 1, #Javascript is enabled Selenium::Remote::Driver-based UserAgents. |
48 |
accept_ssl_certs => 1, #Accept self-signed ssl-certificates |
49 |
default_finder => 'css', #Use css as the default HTML element finder instead of xpath. |
50 |
#css is selected because Test::Mojo uses it and it is generally more widely used. |
51 |
} |
52 |
|
53 |
Valid userAgent names: |
54 |
'phantomjs', is a headless browser which can be ran as standalone without an installed |
55 |
GUI( like X-server ), this is recommended for test servers. |
56 |
See Selenium::PhantomJS for installation instructions. |
57 |
'firefox', launches a Firefox-instance to run the automated tests. |
58 |
See Selenium::Firefox for installation instructions. |
59 |
'mojolicious' is the Test::Mojo-test userAgent used to test Mojolicious framework routes. |
60 |
Is installed with the Mojolicius framework. |
61 |
No accepted configuration parameters at this time. |
62 |
You can give the 'version', but we default to the only version we currently have, 'V1'. |
63 |
|
64 |
@PARAM1 String, the name of the userAgent requested with default config, eg. 'selenium' or 'firefox' |
65 |
@RETURNS List of, the requested Selenium::Remote::Driver-implementation, eg. Selenium::PhantomJS |
66 |
@OR |
67 |
@PARAM1 ARRAYRef, names of the userAgents requested with default config |
68 |
@RETURNS List of, the requested Selenium::Remote::Driver-implementations |
69 |
@OR |
70 |
@PARAM1 HASHRef, names of the userAgents requested as HASH keys, keys must start with |
71 |
the desired userAgent-implementation name and be suffixed with an identifier |
72 |
so the keys don't conflict with each other. |
73 |
UserAgent keys correspond to HASHRefs of extra configuration parameters for |
74 |
Selenium::Remote::Driver->new() |
75 |
@RETURNS List of, the requested Selenium::Remote::Driver-implementations |
76 |
|
77 |
@THROWS Koha::Exception::UnknownProgramState, see _getTestMojoDriver() |
78 |
@THROWS Koha::Exception::VersionMismatch, see _getTestMojoDriver() |
79 |
=cut |
80 |
|
81 |
sub getUserAgentDrivers { |
82 |
my ($requestedUserAgents) = @_; |
83 |
|
84 |
my $requestedUserAgentNames; |
85 |
if( ref($requestedUserAgents) eq 'HASH' ) { |
86 |
$requestedUserAgentNames = [keys(%$requestedUserAgents)]; |
87 |
} |
88 |
elsif ( ref($requestedUserAgents) eq 'ARRAY' ) { |
89 |
$requestedUserAgentNames = $requestedUserAgents; |
90 |
} |
91 |
else { |
92 |
$requestedUserAgentNames = [$requestedUserAgents]; |
93 |
} |
94 |
|
95 |
##Collect the user agents requested for. |
96 |
#Find out if the $requestedUserAgents-parameters contain a configuration |
97 |
#HASH for all/some of the requested user agents, and merge that over the |
98 |
#default configuration values for each user agent. |
99 |
#For some reason the Selenium constructors want HASHes in List-context? |
100 |
my @userAgents; |
101 |
foreach my $reqUAName (@$requestedUserAgentNames) { |
102 |
my $reqUAConf = $requestedUserAgents->{$reqUAName} if ref($requestedUserAgents) eq 'HASH'; |
103 |
|
104 |
if ($reqUAName =~ /^phantomjs/) { |
105 |
require Selenium::PhantomJS; |
106 |
my $defaultConf = { |
107 |
javascript => 1, |
108 |
accept_ssl_certs => 1, |
109 |
default_finder => 'css', |
110 |
}; |
111 |
@$defaultConf{keys %$reqUAConf} = values %$reqUAConf if ref($reqUAConf) eq 'HASH'; |
112 |
|
113 |
my @hashInListContext = %$defaultConf; |
114 |
push @userAgents, Selenium::PhantomJS->new(@hashInListContext); |
115 |
} |
116 |
elsif ($reqUAName =~ /^firefox/) { |
117 |
require Selenium::Firefox; |
118 |
my $defaultConf = { |
119 |
javascript => 1, |
120 |
accept_ssl_certs => 1, |
121 |
default_finder => 'css', |
122 |
}; |
123 |
@$defaultConf{keys %$reqUAConf} = values %$reqUAConf if ref($reqUAConf) eq 'HASH'; |
124 |
|
125 |
my @hashInListContext = %$defaultConf; |
126 |
push @userAgents, Selenium::Firefox->new(@hashInListContext); |
127 |
} |
128 |
elsif ($reqUAName =~ /^mojolicious/) { |
129 |
my $defaultConf = { |
130 |
version => 'V1', |
131 |
}; |
132 |
@$defaultConf{keys %$reqUAConf} = values %$reqUAConf if ref($reqUAConf) eq 'HASH'; |
133 |
|
134 |
push @userAgents, _getTestMojoDriver($defaultConf); |
135 |
} |
136 |
} |
137 |
|
138 |
return @userAgents; |
139 |
} |
140 |
|
141 |
=head _getTestMojoDriver |
142 |
|
143 |
@THROWS Koha::Exception::UnknownProgramState, if Test::Mojo doesn't die out of failure, but we get no Test Driver. |
144 |
@THROWS Koha::Exception::VersionMismatch, if we try to get an unsupported API version test driver. |
145 |
=cut |
146 |
sub _getTestMojoDriver { |
147 |
require Test::Mojo; |
148 |
my ($config) = @_; |
149 |
|
150 |
if ((uc($config->{version}) eq 'V1') || not(exists($config->{version}))) { #Default to V1 |
151 |
$ENV{MOJO_LOGFILES} = undef; |
152 |
$ENV{MOJO_CONFIG} = undef; |
153 |
my $mojoDriver = Test::Mojo->new('Koha::REST::V1'); |
154 |
return $mojoDriver if $mojoDriver; |
155 |
Koha::Exception::UnknownProgramState->throw(error => "WebDriverFactory::_getTestMojoDriver():> Unexpected exception."); |
156 |
} |
157 |
else { |
158 |
Koha::Exception::VersionMismatch->throw(error => "WebDriverFactory::_getTestMojoDriver():> Unknown version, supported version 'V1'"); |
159 |
} |
160 |
} |
161 |
|
162 |
1; #Make the compiler happy! |