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