blib/lib/Test/WebDriver.pm

filter: Statementstmt 34.6% 43 / 124 Branchbran 2.3% 1 / 42 Conditioncond 10.0% 4 / 40 MC/DCmcdc 0.0% 0 / 24 Subroutinesub 50.0% 11 / 22 Podpod 75.0% 6 / 8 totaltotal 25.0% 65 / 260 CC 37 SCAR 36.8
CC
37
Cov
23%
__ANON__
132.0
AUTOLOAD
43.3
new
30.0
CRAP 39.7
↑ Summary
1 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Test::WebDriver;
4 2 ▶use base 'Selenium::Remote::Driver';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 # ABSTRACT: Useful testing subclass for Selenium WebDriver!
6  
7 2 ▶use Test::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Test::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use IO::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 our $AUTOLOAD;
12  
13 2 my $Test = Test::Builder->new;
14 2 $Test->exported_to(__PACKAGE__);
15  
16 2 my %comparator = (
17 is => 'is_eq',
18 isnt => 'isnt_eq',
19 like => 'like',
20 unlike => 'unlike',
21 );
22 2 my $comparator_keys = join '|', keys %comparator;
23  
24 # These commands don't require a locator
25 2 my %no_locator = map { $_ => 1 }
26 qw( alert_text current_window_handle current_url
27 title page_source body location path);
28
29 0 ▶sub no_locator {
Subroutine no_locator: not called CC=1 SCAR=6.9
Pod undocumented
30 0 my $self = shift;
31 0 my $method = shift;
32 0 return $no_locator{$method};
33 }
34  
35 0 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=7 SCAR=37.7
36 0 my $name = $AUTOLOAD;
37 0 $name =~ s/.*:://;
38 0 ▶ return if $name eq 'DESTROY';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "DESTROY" 0 0 0
39 0 my $self = $_[0];
40  
41 0 my $sub;
42 0 ▶ if ($name =~ /(\w+)_($comparator_keys)$/i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($name =~ /(\w+)_($comparator_keys)$/i) { } 0 0 0
elsif ($name =~ /(\w+?)_?ok$/i) { } 0 0 0
43 0 my $getter = "get_$1";
44 0 my $comparator = $comparator{lc $2};
45  
46 # make a subroutine that will call Test::Builder's test methods
47 # with driver data from the getter
48 0 ▶ if ($self->no_locator($1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->no_locator($1)) { } 0 0 0
49 0 ▶ $sub = sub {
Subroutine __ANON__: not called CC=6 SCAR=33.9
50 0 my( $self, $str, $name ) = @_;
51 diag "Test::WebDriver running no_locator $getter (@_[1..$#_])"
52 0 ▶ if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
53 $name = "$getter, '$str'"
54 0 ▶ if $self->{default_names} and !defined $name;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"default_names"} and not defined $name 0 0 0
Condition0%0/3
$self->{"default_names"} and not defined $name
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"default_names"} and not defined $name
$self->{"default_names"} not defined $name
Truth table0%0/3
A and not(B)
A= $self->{"default_names"}B= not defined $name
ABresult
0X0
100
111
55 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
56 0 my $rc = $Test->$comparator( $self->$getter, $str, $name );
57 0 ▶ if (!$rc && $self->error_callback) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $rc and $self->error_callback) 0 0 0
Condition0%0/3
not $rc and $self->error_callback
!ll&&!rl&&r
000
MC/DC0%0/2
not $rc and $self->error_callback
not $rc $self->error_callback
Truth table0%0/3
not(A) and B
A= not $rcB= $self->error_callback
ABresult
0X0
100
111
58 0 &{$self->error_callback}($name);
59 }
60 0 return $rc;
61 0 };
62 }
63 else {
64 0 ▶ $sub = sub {
Subroutine __ANON__: not called CC=6 SCAR=31.1
65 0 my( $self, $locator, $str, $name ) = @_;
66 diag "Test::WebDriver running with locator $getter (@_[1..$#_])"
67 0 ▶ if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
68 $name = "$getter, $locator, '$str'"
69 0 ▶ if $self->{default_names} and !defined $name;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"default_names"} and not defined $name 0 0 0
Condition0%0/3
$self->{"default_names"} and not defined $name
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"default_names"} and not defined $name
$self->{"default_names"} not defined $name
Truth table0%0/3
A and not(B)
A= $self->{"default_names"}B= not defined $name
ABresult
0X0
100
111
70 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
71 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
72 0 my $rc = $Test->$comparator( $self->$getter($locator), $str, $name );
73 0 ▶ if (!$rc && $self->error_callback) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $rc and $self->error_callback) 0 0 0
Condition0%0/3
not $rc and $self->error_callback
!ll&&!rl&&r
000
MC/DC0%0/2
not $rc and $self->error_callback
not $rc $self->error_callback
Truth table0%0/3
not(A) and B
A= not $rcB= $self->error_callback
ABresult
0X0
100
111
74 0 &{$self->error_callback}($name);
75 }
76 0 return $rc;
77 0 };
78 }
79 }
80 elsif ($name =~ /(\w+?)_?ok$/i) {
81 0 my $cmd = $1;
82  
83 # make a subroutine for ok() around the selenium command
84 0 ▶ $sub = sub {
Subroutine __ANON__: not called CC=11 SCAR=48.8
85 0 my( $self, $arg1, $arg2, $name ) = @_;
86 0 ▶ if ($self->{default_names} and !defined $name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"default_names"} and not defined $name) 0 0 0
Condition0%0/3
$self->{"default_names"} and not defined $name
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"default_names"} and not defined $name
$self->{"default_names"} not defined $name
Truth table0%0/3
A and not(B)
A= $self->{"default_names"}B= not defined $name
ABresult
0X0
100
111
87 0 $name = $cmd;
88 0 ▶ $name .= ", $arg1" if defined $arg1;
Branch: 1 branch
BranchTrueFalseTotal
if defined $arg1 0 0 0
89 0 ▶ $name .= ", $arg2" if defined $arg2;
Branch: 1 branch
BranchTrueFalseTotal
if defined $arg2 0 0 0
90 }
91 diag "Test::WebDriver running _ok $cmd (@_[1..$#_])"
92 0 ▶ if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
93  
94 0 local $Test::Builder::Level = $Test::Builder::Level + 1;
95 0 my $rc = '';
96 0 eval { $rc = $self->$cmd( $arg1, $arg2 ) };
97 0 ▶ die $@ if $@ and $@ =~ /Can't locate object method/;
Branch: 1 branch
BranchTrueFalseTotal
if $@ and $@ =~ /Can't locate object method/ 0 0 0
Condition0%0/3
$@ and $@ =~ /Can't locate object method/
!ll&&!rl&&r
000
MC/DC0%0/2
$@ and $@ =~ /Can't locate object method/
$@ $@ =~ /Can't locate object method/
Truth table0%0/3
A and B
A= $@B= $@ =~ /Can't locate object method/
ABresult
0X0
100
111
98 0 ▶ diag($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
99 0 $rc = ok( $rc, $name );
100 0 ▶ if (!$rc && $self->error_callback) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $rc and $self->error_callback) 0 0 0
Condition0%0/3
not $rc and $self->error_callback
!ll&&!rl&&r
000
MC/DC0%0/2
not $rc and $self->error_callback
not $rc $self->error_callback
Truth table0%0/3
not(A) and B
A= not $rcB= $self->error_callback
ABresult
0X0
100
111
101 0 &{$self->error_callback}($name);
102 }
103 0 return $rc;
104 0 };
105 }
106  
107 # jump directly to the new subroutine, avoiding an extra frame stack
108 0 ▶ if ($sub) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sub) { } 0 0 0
109 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
110 0 *{$AUTOLOAD} = $sub;
111 0 goto &$AUTOLOAD;
112 }
113 else {
114 # try to pass through to Selenium::Remote::Driver
115 0 my $sel = 'Selenium::Remote::Driver';
116 0 my $sub = "${sel}::${name}";
117 0 ▶ goto &$sub if exists &$sub;
Branch: 1 branch
BranchTrueFalseTotal
if exists &$sub 0 0 0
118 0 my ($package, $filename, $line) = caller;
119 0 die qq(Can't locate object method "$name" via package ")
120 . __PACKAGE__
121 . qq(" (also tried "$sel") at $filename line $line\n);
122 }
123 }
124  
125 0 ▶sub error_callback {
Subroutine error_callback: not called CC=2 SCAR=17.9
Pod undocumented
126 0 my ($self, $cb) = @_;
127 0 ▶ if (defined($cb)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cb) 0 0 0
128 0 $self->{error_callback} = $cb;
129 }
130 0 return $self->{error_callback};
131 }
132  
133 =head2 new ( %opts )
134  
135 This will create a new Test::WebDriver object, which subclasses
136 L<Selenium::Remote::Driver>. This subclass provides useful testing
137 functions. It is modeled on L<Test::WWW::Selenium>.
138  
139 Environment vars can be used to specify options to pass to
140 L<Selenium::Remote::Driver>. ENV vars are prefixed with C<TWD_>.
141  
142 Set the Selenium server address with C<$TWD_HOST> and C<$TWD_PORT>.
143  
144 Pick which browser is used using the C<$TWD_BROWSER>, C<$TWD_VERSION>,
145 C<$TWD_PLATFORM>, C<$TWD_JAVASCRIPT>, C<$TWD_EXTRA_CAPABILITIES>.
146  
147 See L<Selenium::Driver::Remote> for the meanings of these options.
148  
149 =cut
150  
151 0 ▶sub new {
Subroutine new: not called CC=5 SCAR=34.0
Pod documented
152 0 my ($class, %p) = @_;
153  
154 0 for my $opt (qw/remote_server_addr port browser_name version platform
155 javascript auto_close extra_capabilities/) {
156 0 ▶ $p{$opt} ||= $ENV{ 'TWD_' . uc($opt) };
Condition0%0/3
$p{$opt} ||= $ENV{'TWD_' . uc($opt)}
l!l&&r!l&&!r
000
MC/DC0%0/2
$p{$opt} ||= $ENV{'TWD_' . uc($opt)}
$p{$opt} $ENV{'TWD_' . uc($opt)}
Truth table0%0/3
A ||= B
A= $p{$opt}B= $ENV{'TWD_' . uc($opt)}
ABresult
1X1
011
000
157 }
158 0 ▶ $p{browser_name} ||= $ENV{TWD_BROWSER}; # ykwim
Condition0%0/3
$p{"browser_name"} ||= $ENV{"TWD_BROWSER"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$p{"browser_name"} ||= $ENV{"TWD_BROWSER"}
$p{"browser_name"} $ENV{"TWD_BROWSER"}
Truth table0%0/3
A ||= B
A= $p{"browser_name"}B= $ENV{"TWD_BROWSER"}
ABresult
1X1
011
000
159 0 ▶ $p{remote_server_addr} ||= $ENV{TWD_HOST}; # ykwim
Condition0%0/3
$p{"remote_server_addr"} ||= $ENV{"TWD_HOST"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$p{"remote_server_addr"} ||= $ENV{"TWD_HOST"}
$p{"remote_server_addr"} $ENV{"TWD_HOST"}
Truth table0%0/3
A ||= B
A= $p{"remote_server_addr"}B= $ENV{"TWD_HOST"}
ABresult
1X1
011
000
160  
161 0 my $self = $class->SUPER::new(%p);
162 0 $self->{verbose} = $p{verbose};
163 0 return $self;
164 }
165  
166 =head2 server_is_running( $host, $port )
167  
168 Returns true if a Selenium server is running. The host and port
169 parameters are optional, and default to C<localhost:4444>.
170  
171 Environment vars C<TWD_HOST> and C<TWD_PORT> can also be used to
172 determine the server to check.
173  
174 =cut
175  
176 1 ▶sub server_is_running {
Subroutine server_is_running: called CC=6 SCAR=21.4
Pod documented
177 1 my $class_or_self = shift;
178 1 ▶ my $host = $ENV{TWD_HOST} || shift || 'localhost';
Condition40%2/5
$ENV{'TWD_HOST'} || shift() || 'localhost'
l!l
01
$ENV{"TWD_HOST"} or shift()
l!l&&r!l&&!r
001
MC/DC0%0/2
$ENV{'TWD_HOST'} || shift() || 'localhost'
$ENV{"TWD_HOST"} shift()
Truth table33%1/3
A or B
A= $ENV{"TWD_HOST"}B= shift()
ABresult
1X1
011
000
179 1 ▶ my $port = $ENV{TWD_PORT} || shift || 4444;
Condition40%2/5
$ENV{'TWD_PORT'} || shift() || 4444
l!l
01
$ENV{"TWD_PORT"} or shift()
l!l&&r!l&&!r
001
MC/DC0%0/2
$ENV{'TWD_PORT'} || shift() || 4444
$ENV{"TWD_PORT"} shift()
Truth table33%1/3
A or B
A= $ENV{"TWD_PORT"}B= shift()
ABresult
1X1
011
000
180  
181 1 ▶ return ($host, $port) if IO::Socket::INET->new(
Branch: 1 branch
BranchTrueFalseTotal
if "IO::Socket::INET"->new("PeerAddr", $host, "PeerPort", $port) 0 1 1
182 PeerAddr => $host,
183 PeerPort => $port,
184 );
185 1 return;
186  
187 }
188  
189  
190 =head2 Glue Code
191  
192 Below here are some methods that make things less easier or less wordy.
193  
194 =head3 get_text
195  
196 Get the text of a particular element. Wrapper around find_element()
197  
198 =cut
199  
200 0 ▶sub get_text {
Subroutine get_text: not called CC=1 SCAR=6.9
Pod documented
201 0 my $self = shift;
202 0 return $self->find_element(@_)->get_text();
203 }
204  
205 =head3 get_body
206  
207 Get the current text for the whole body.
208  
209 =cut
210  
211 0 ▶sub get_body {
Subroutine get_body: not called CC=1 SCAR=6.9
Pod documented
212 0 my $self = shift;
213 0 return $self->get_text('//body');
214 }
215  
216 =head3 get_location
217  
218 Get the current URL.
219  
220 =cut
221  
222 0 ▶sub get_location {
Subroutine get_location: not called CC=1 SCAR=6.9
Pod documented
223 0 return shift->get_current_url();
224 }
225  
226 =head3 get_location
227  
228 Get the path part of the current browser location.
229  
230 =cut
231  
232 0 ▶sub get_path {
Subroutine get_path: not called CC=1 SCAR=6.9
Pod documented
233 0 my $self = shift;
234 0 my $location = $self->get_location;
235 0 $location =~ s/\?.*//; # strip of query params
236 0 $location =~ s/#.*//; # strip of anchors
237 0 $location =~ s#^https?://[^/]+##; # strip off host
238 0 return $location;
239 }
240  
241 2 1;
242  
243 __END__
↑ Summary