View | Details | Raw Unified | Return to bug 14495
Collapse All | Expand All

(-)a/t/lib/WebDriverFactory.pm (+152 lines)
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
Valid userAgent names:
46
    'phantomjs',  is a headless browser which can be ran as standalone without an installed
47
                  GUI( like X-server ), this is recommended for test servers.
48
                  See Selenium::PhantomJS for installation instructions.
49
    'firefox',    launches a Firefox-instance to run the automated tests.
50
                  See Selenium::Firefox for installation instructions.
51
    'mojolicious' is the Test::Mojo-test userAgent used to test Mojolicious framework routes.
52
                  Is installed with the Mojolicius framework.
53
                  No accepted configuration parameters at this time.
54
                  You can give the 'version', but we default to the only version we currently have, 'V1'.
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
@RETURNS List of, the requested Selenium::Remote::Driver-implementations
68
69
@THROWS Koha::Exception::UnknownProgramState, see _getTestMojoDriver()
70
@THROWS Koha::Exception::VersionMismatch, see _getTestMojoDriver()
71
=cut
72
73
sub getUserAgentDrivers {
74
    my ($requestedUserAgents) = @_;
75
76
    my $requestedUserAgentNames;
77
    if( ref($requestedUserAgents) eq 'HASH' ) {
78
        $requestedUserAgentNames = [keys(%$requestedUserAgents)];
79
    }
80
    elsif ( ref($requestedUserAgents) eq 'ARRAY' ) {
81
        $requestedUserAgentNames = $requestedUserAgents;
82
    }
83
    else {
84
        $requestedUserAgentNames = [$requestedUserAgents];
85
    }
86
87
    ##Collect the user agents requested for.
88
    #Find out if the $requestedUserAgents-parameters contain a configuration
89
    #HASH for all/some of the requested user agents, and merge that over the
90
    #default configuration values for each user agent.
91
    #For some reason the Selenium constructors want HASHes in List-context?
92
    my @userAgents;
93
    foreach my $reqUAName (@$requestedUserAgentNames) {
94
        require Selenium::PhantomJS;
95
        my $reqUAConf = $requestedUserAgents->{$reqUAName} if ref($requestedUserAgents) eq 'HASH';
96
97
        if ($reqUAName =~ /^phantomjs/) {
98
            my $defaultConf = {
99
                                javascript         => 1,
100
                                accept_ssl_certs   => 1,
101
            };
102
            @$defaultConf{keys %$reqUAConf} = values %$reqUAConf if ref($reqUAConf) eq 'HASH';
103
104
            my @hashInListContext = %$defaultConf;
105
            push @userAgents, Selenium::PhantomJS->new(@hashInListContext);
106
        }
107
        elsif ($reqUAName =~ /^firefox/) {
108
            require Selenium::Firefox;
109
            my $defaultConf = {
110
                                javascript         => 1,
111
                                accept_ssl_certs   => 1,
112
            };
113
            @$defaultConf{keys %$reqUAConf} = values %$reqUAConf if ref($reqUAConf) eq 'HASH';
114
115
            my @hashInListContext = %$defaultConf;
116
            push @userAgents, Selenium::Firefox->new(@hashInListContext);
117
        }
118
        elsif ($reqUAName =~ /^mojolicious/) {
119
            my $defaultConf = {
120
                                version => 'V1',
121
            };
122
            @$defaultConf{keys %$reqUAConf} = values %$reqUAConf if ref($reqUAConf) eq 'HASH';
123
124
            push @userAgents, _getTestMojoDriver($defaultConf);
125
        }
126
    }
127
128
    return @userAgents;
129
}
130
131
=head _getTestMojoDriver
132
133
@THROWS Koha::Exception::UnknownProgramState, if Test::Mojo doesn't die out of failure, but we get no Test Driver.
134
@THROWS Koha::Exception::VersionMismatch, if we try to get an unsupported API version test driver.
135
=cut
136
sub _getTestMojoDriver {
137
    require Test::Mojo;
138
    my ($config) = @_;
139
140
    if ((uc($config->{version}) eq 'V1') || not(exists($config->{version}))) { #Default to V1
141
        $ENV{MOJO_LOGFILES} = undef;
142
        $ENV{MOJO_CONFIG} = undef;
143
        my $mojoDriver = Test::Mojo->new('Koha::REST::V1');
144
        return $mojoDriver if $mojoDriver;
145
        Koha::Exception::UnknownProgramState->throw(error => "WebDriverFactory::_getTestMojoDriver():> Unexpected exception.");
146
    }
147
    else {
148
        Koha::Exception::VersionMismatch->throw(error => "WebDriverFactory::_getTestMojoDriver():> Unknown version, supported version 'V1'");
149
    }
150
}
151
152
1; #Make the compiler happy!
(-)a/t/lib/webDriverFactory.t (-1 / +51 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/env perl
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 Test::More; #Please don't set the test count here. It is nothing but trouble when rebasing against master
23
                #and is of dubious help, especially since we are running dynamic tests here which are triggered
24
                #based on the reported test infrastucture capabilities.
25
use Try::Tiny; #Even Selenium::Remote::Driver uses Try::Tiny :)
26
use Scalar::Util qw(blessed);
27
28
use t::lib::WebDriverFactory;
29
30
31
my $testingModules = {  firefox => {version => '39.0', platform => 'LINUX'},
32
                        phantomjs => {},
33
                        mojolicious => {version => 'V1'},
34
                    };
35
36
foreach my $name (keys %$testingModules) {
37
    try {
38
        my $conf = $testingModules->{$name};
39
        my ($webDriver) = t::lib::WebDriverFactory::getUserAgentDrivers({$name => $conf});
40
        ok(blessed($webDriver), "'$name' WebDriver/UserAgent capability.");
41
    } catch {
42
        if ($_ =~ /is not an executable file/) {
43
            print "$name-driver is not installed. See Selenium::$name for installation instructions.\n";
44
        }
45
        else {
46
            print "$name-driver not operational.\n";
47
        }
48
    };
49
}
50
51
done_testing;

Return to bug 14495