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

(-)a/t/lib/WebDriverFactory.pm (+140 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 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!
(-)a/t/lib/webDriverFactory.t (-1 / +84 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 Selenium::PhantomJS;
29
use Selenium::Firefox;
30
use t::lib::WebDriverFactory;
31
32
use C4::Members;
33
use Koha::Database;
34
use Koha::Borrower;
35
36
37
my $branchcode = Koha::Database->new()->schema()->resultset('Branch')->first()->branchcode();
38
39
###############################################
40
##    Setting up test context Koha-style!    ##
41
#This should be refactored when a proper automagic setUp-tearDown framework for integration tests is deployed.
42
###############################################
43
my $borrower = C4::Members::GetMember(cardnumber => '11A001'); #These tests can crash and leave the context behind,
44
my $borrowernumber = $borrower->{borrowernumber} if $borrower; #so when we rerun them, we get nasty issues with existing DB objects.
45
unless ($borrower) {
46
    $borrowernumber = C4::Members::AddMember(
47
                            firstname    => 'Olli',
48
                            lastname     => 'Kivi',
49
                            categorycode => 'PT',
50
                            userid       => '11Aadmin',
51
                            password     => '1234',
52
                            cardnumber   => '11A001',
53
                            dateofbirth  => DateTime->now(time_zone => C4::Context->tz)->subtract(years => 21), #I am always 21 :)
54
                            flags        => '1', #Giving specific permission flags here is REALLY HARD! Just giving superlibrarian-permission.
55
                            branchcode   => 'CPL',
56
                            );
57
    $borrower = C4::Members::GetMember(borrowernumber => $borrowernumber);
58
}
59
60
my $testingModules = {  firefox => {version => '39.0', platform => 'LINUX'},
61
                        phantomjs => {},
62
                        mojolicious => {version => 'V1'},
63
                    };
64
65
foreach my $name (keys %$testingModules) {
66
    try {
67
        my $conf = $testingModules->{$name};
68
        my ($webDriver) = t::lib::WebDriverFactory::getUserAgentDrivers({$name => $conf});
69
        ok(blessed($webDriver), "'$name' WebDriver/UserAgent capability.");
70
    } catch {
71
        if ($_ =~ /is not an executable file/) {
72
            print "$name-driver is not installed. See Selenium::$name for installation instructions.\n";
73
        }
74
        else {
75
            print "$name-driver not operational.\n";
76
        }
77
    };
78
}
79
80
###################################
81
##    TearDown test context..    ##
82
###################################
83
Koha::Database->new()->schema()->resultset('Borrower')->search({borrowernumber => $borrower->{borrowernumber}})->delete_all();
84
done_testing;

Return to bug 14495