| 1 | package Selenium::Server; | ||||||||||||
| 2 | |||||||||||||
| 3 | 1 | ▶ | use 5.008001; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 1 | ▶ | use Carp qw(croak); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 1 | ▶ | use LWP::UserAgent; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 1 | ▶ | use Test::TCP; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 1 | ▶ | use File::Slurp qw(write_file); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 1 | ▶ | use File::Temp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 1 | ▶ | use File::Spec; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | |||||||||||||
| 13 | 1 | our $VERSION = '0.02'; | |||||||||||
| 14 | 1 | $VERSION = eval $VERSION; | |||||||||||
| 15 | |||||||||||||
| 16 | 1 | my $ua = LWP::UserAgent->new; | |||||||||||
| 17 | |||||||||||||
| 18 | 1 | our $LATEST_VERSION; | |||||||||||
| 19 | 0 | ▶ | sub latest_version { | ||||||||||
|
Subroutine
latest_version: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 20 | 0 | my $class = shift; | |||||||||||
| 21 | |||||||||||||
| 22 | 0 | ▶ | unless ($LATEST_VERSION) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 23 | 0 | my $body = $ua->get('http://code.google.com/p/selenium/downloads/list')->content; | |||||||||||
| 24 | 0 | my ($version) = $body =~ /selenium-server-standalone-(\d+.\d+.\d+).jar/i; | |||||||||||
| 25 | 0 | $LATEST_VERSION = $version; | |||||||||||
| 26 | } | ||||||||||||
| 27 | |||||||||||||
| 28 | 0 | $LATEST_VERSION; | |||||||||||
| 29 | } | ||||||||||||
| 30 | |||||||||||||
| 31 | 0 | ▶ | sub download { | ||||||||||
|
Subroutine
download: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 32 | 0 | my ($class, $version, $path) = @_; | |||||||||||
| 33 | |||||||||||||
| 34 | 0 | my $name = "selenium-server-standalone-${version}.jar"; | |||||||||||
| 35 | 0 | my $url = "http://selenium.googlecode.com/files/${name}"; | |||||||||||
| 36 | |||||||||||||
| 37 | 0 | my $res = $ua->get($url); | |||||||||||
| 38 | 0 | write_file($path, { binmode => ':raw' }, $res->content); | |||||||||||
| 39 | } | ||||||||||||
| 40 | |||||||||||||
| 41 | 0 | ▶ | sub new { | ||||||||||
|
Subroutine
new: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||
| 42 | 0 | my ($class, %args) = @_; | |||||||||||
| 43 | |||||||||||||
| 44 | 0 | ▶ | my $jar = exists $args{jar} ? $args{jar} : do { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 45 | 0 | ▶ | my $version = exists $args{version} ? $args{version} : $class->latest_version; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 46 | |||||||||||||
| 47 | 0 | my $dir = File::Spec->tmpdir; | |||||||||||
| 48 | 0 | my $name = "selenium-server-standalone-${version}.jar"; | |||||||||||
| 49 | 0 | my $path = File::Spec->catfile($dir, $name); | |||||||||||
| 50 | |||||||||||||
| 51 | 0 | ▶ | $class->download($version, $path) unless -e $path; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 52 | 0 | $path; | |||||||||||
| 53 | }; | ||||||||||||
| 54 | |||||||||||||
| 55 | 0 | my $self = bless { jar => $jar }, $class; | |||||||||||
| 56 | |||||||||||||
| 57 | 0 | ▶ | if ($args{auto_start}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 58 | 0 | $self->start; | |||||||||||
| 59 | } | ||||||||||||
| 60 | |||||||||||||
| 61 | 0 | return $self; | |||||||||||
| 62 | } | ||||||||||||
| 63 | |||||||||||||
| 64 | 0 | ▶ | sub jar { | ||||||||||
|
Subroutine
jar: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 65 | 0 | my $self = shift; | |||||||||||
| 66 | 0 | ▶ | return $self->{jar} || ''; | ||||||||||
|
Condition0%0/2
$self->{'jar'} || ''
MC/DC0%0/1
$self->{'jar'} || ''
$self->{'jar'}
Truth table0%0/2
A
A= $self->{'jar'}
| |||||||||||||
| 67 | } | ||||||||||||
| 68 | |||||||||||||
| 69 | 0 | ▶ | sub host { | ||||||||||
|
Subroutine
host: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 70 | 0 | my $self = shift; | |||||||||||
| 71 | 0 | ▶ | return $self->{server} ? '127.0.0.1' : ''; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 72 | } | ||||||||||||
| 73 | |||||||||||||
| 74 | 0 | ▶ | sub port { | ||||||||||
|
Subroutine
port: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 75 | 0 | my $self = shift; | |||||||||||
| 76 | 0 | ▶ | return $self->{server} ? $self->{server}->port : ''; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 77 | } | ||||||||||||
| 78 | |||||||||||||
| 79 | 0 | ▶ | sub start { | ||||||||||
|
Subroutine
start: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 80 | 0 | my ($self, $args) = @_; | |||||||||||
| 81 | |||||||||||||
| 82 | my $server = Test::TCP->new( | ||||||||||||
| 83 | 0 | ▶ | code => sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||
| 84 | 0 | my $port = shift; | |||||||||||
| 85 | |||||||||||||
| 86 | 0 | my $fh = File::Temp->new(UNLINK => 1); | |||||||||||
| 87 | 0 | ▶ | open STDOUT, '>&', $fh or croak "dup(2) failed:$!"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 88 | 0 | ▶ | open STDERR, '>&', $fh or croak "dup(2) failed:$!"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 89 | |||||||||||||
| 90 | 0 | ▶ | my $cmd = sprintf 'java -jar "%s" %s -port %s', $self->jar, ($args || ''), $port; | ||||||||||
|
Condition0%0/2
$args || ''
MC/DC0%0/1
$args || ''
$args
Truth table0%0/2
A
A= $args
| |||||||||||||
| 91 | 0 | system $cmd; | |||||||||||
| 92 | }, | ||||||||||||
| 93 | 0 | ); | |||||||||||
| 94 | 0 | $server->start; | |||||||||||
| 95 | |||||||||||||
| 96 | 0 | $self->{server} = $server; | |||||||||||
| 97 | } | ||||||||||||
| 98 | |||||||||||||
| 99 | 0 | ▶ | sub stop { | ||||||||||
|
Subroutine
stop: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 100 | 0 | my $self = shift; | |||||||||||
| 101 | |||||||||||||
| 102 | 0 | ▶ | if ($self->{server}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 103 | 0 | my ($host, $port) = ($self->host, $self->port); | |||||||||||
| 104 | 0 | my $url = "http://${host}:${port}/selenium-server/driver/?cmd=shutDownSeleniumServer"; | |||||||||||
| 105 | 0 | $ua->get($url); | |||||||||||
| 106 | |||||||||||||
| 107 | 0 | delete $self->{server}; | |||||||||||
| 108 | } | ||||||||||||
| 109 | } | ||||||||||||
| 110 | |||||||||||||
| 111 | 0 | ▶ | sub DESTROY { | ||||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=6.9
| |||||||||||||
| 112 | 0 | my $self = shift; | |||||||||||
| 113 | 0 | $self->stop; | |||||||||||
| 114 | } | ||||||||||||
| 115 | |||||||||||||
| 116 | 1 | 1; | |||||||||||
| 117 | |||||||||||||
| 118 | =head1 NAME | ||||||||||||
| 119 | |||||||||||||
| 120 | Selenium::Server - A wrapper of selenium-server-standalone.jar | ||||||||||||
| 121 | |||||||||||||
| 122 | =head1 SYNOPSIS | ||||||||||||
| 123 | |||||||||||||
| 124 | use Selenium::Server; | ||||||||||||
| 125 | |||||||||||||
| 126 | # (default) download and use latest version jar | ||||||||||||
| 127 | my $server = Selenium::Server->new; | ||||||||||||
| 128 | # specify jar | ||||||||||||
| 129 | my $server = Selenium::Server->new(jar => '/path/to/selenium-server.jar'); | ||||||||||||
| 130 | # specify version | ||||||||||||
| 131 | my $server = Selenium::Server->new(version => '2.11.0'); | ||||||||||||
| 132 | |||||||||||||
| 133 | $server->start; | ||||||||||||
| 134 | # with arguments | ||||||||||||
| 135 | $server->start('-timeout 60 -trustAllSSLCertificates'); | ||||||||||||
| 136 | |||||||||||||
| 137 | my $host = $server->host; # '127.0.0.1' | ||||||||||||
| 138 | my $port = $server->port; | ||||||||||||
| 139 | |||||||||||||
| 140 | $server->stop; | ||||||||||||
| 141 | |||||||||||||
| 142 | =head1 DESCRIPTION | ||||||||||||
| 143 | |||||||||||||
| 144 | Selenium::Server is a wrapper of Selenium RemoteWebDriver Server; | ||||||||||||
| 145 | selenium-server-standalone-{version}.jar file. | ||||||||||||
| 146 | |||||||||||||
| 147 | =head1 METHODS | ||||||||||||
| 148 | |||||||||||||
| 149 | =over 4 | ||||||||||||
| 150 | |||||||||||||
| 151 | =item * new() | ||||||||||||
| 152 | |||||||||||||
| 153 | Creates a selenium-server wrapper instance. | ||||||||||||
| 154 | |||||||||||||
| 155 | =item * start($args) | ||||||||||||
| 156 | |||||||||||||
| 157 | Starts selenium-server, with $args if specified. | ||||||||||||
| 158 | |||||||||||||
| 159 | =item * stop() | ||||||||||||
| 160 | |||||||||||||
| 161 | Stops selenium-server. | ||||||||||||
| 162 | |||||||||||||
| 163 | =item * host() | ||||||||||||
| 164 | |||||||||||||
| 165 | Returns selenium-server host. (127.0.0.1) | ||||||||||||
| 166 | |||||||||||||
| 167 | =item * port() | ||||||||||||
| 168 | |||||||||||||
| 169 | Returns selenium-server port. | ||||||||||||
| 170 | |||||||||||||
| 171 | =item * jar() | ||||||||||||
| 172 | |||||||||||||
| 173 | Returns selenium-server JAR file path. | ||||||||||||
| 174 | |||||||||||||
| 175 | =item * latest_version() | ||||||||||||
| 176 | |||||||||||||
| 177 | Returns selenium-server latest version string. | ||||||||||||
| 178 | |||||||||||||
| 179 | =item * download($version, $path) | ||||||||||||
| 180 | |||||||||||||
| 181 | Downloads selenium-server jar file specified by $version to $path. | ||||||||||||
| 182 | |||||||||||||
| 183 | =back | ||||||||||||
| 184 | |||||||||||||
| 185 | =head1 AUTHOR | ||||||||||||
| 186 | |||||||||||||
| 187 | NAKAGAWA Masaki E<lt>masaki@cpan.orgE<gt> | ||||||||||||
| 188 | |||||||||||||
| 189 | =head1 LICENSE | ||||||||||||
| 190 | |||||||||||||
| 191 | This library is free software; you can redistribute it and/or modify | ||||||||||||
| 192 | it under the same terms as Perl itself. | ||||||||||||
| 193 | |||||||||||||
| 194 | =head1 SEE ALSO | ||||||||||||
| 195 | |||||||||||||
| 196 | L<Alien::SeleniumRC> | ||||||||||||
| 197 | |||||||||||||
| 198 | L<http://selenium.googlecode.com/svn/trunk/rb/lib/selenium/server.rb> | ||||||||||||
| 199 | |||||||||||||
| 200 | =cut | ||||||||||||