lib/Test/WWW/Selenium.pm

filter: Statementstmt 89.0% 98 / 110 Branchbran 79.5% 35 / 44 Conditioncond 76.1% 16 / 21 MC/DCmcdc 50.0% 7 / 14 Subroutinesub 93.7% 15 / 16 Podpod 75.0% 3 / 4 totaltotal 83.2% 174 / 209 CC 31 SCAR 18.2
CC
31
Cov
85%
__ANON__
11.2
AUTOLOAD
7.1
__ANON__
6.6
CRAP 6.2
↑ Summary lib/WWW/Selenium/Util.pm »
1 package Test::WWW::Selenium;
2 {
3 7 $Test::WWW::Selenium::VERSION = '1.36';
4 }
5 # ABSTRACT: Test applications using Selenium Remote Control
6 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use base qw(WWW::Selenium);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11 7 ▶use Test::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use Test::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 7 our $AUTOLOAD;
15  
16 7 my $Test = Test::Builder->new;
17 7 $Test->exported_to(__PACKAGE__);
18  
19 7 my %comparator = (
20 is => 'is_eq',
21 isnt => 'isnt_eq',
22 like => 'like',
23 unlike => 'unlike',
24 );
25  
26 # These commands don't require a locator
27 # grep item lib/WWW/Selenium.pm | grep sel | grep \(\) | grep get
28 7 my %no_locator = map { $_ => 1 }
29 qw( speed alert confirmation prompt location title
30 body_text all_buttons all_links all_fields
31 mouse_speed all_window_ids all_window_names
32 all_window_titles html_source cookie absolute_location );
33  
34 8 ▶sub no_locator {
Subroutine no_locator: called CC=1 SCAR=0.0
Pod undocumented
35 8 my $self = shift;
36 8 my $method = shift;
37 8 return $no_locator{$method};
38 }
39  
40 11 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=7 SCAR=19.6
41 11 my $name = $AUTOLOAD;
42 11 $name =~ s/.*:://;
43 11 ▶ return if $name eq 'DESTROY';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "DESTROY" 0 11 11
44 11 my $self = $_[0];
45  
46 11 my $sub;
47 11 ▶ if ($name =~ /(\w+)_(is|isnt|like|unlike)$/i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($name =~ /(\w+)_(is|isnt|like|unlike)$/i) { } 8 3 11
elsif ($name =~ /(\w+?)_?ok$/i) { } 2 1 3
48 8 my $getter = "get_$1";
49 8 my $comparator = $comparator{lc $2};
50  
51 # make a subroutine that will call Test::Builder's test methods
52 # with selenium data from the getter
53 8 ▶ if ($self->no_locator($1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->no_locator($1)) { } 4 4 8
54 11 ▶ $sub = sub {
Subroutine __ANON__: called CC=6 SCAR=18.9
55 11 my( $self, $str, $name ) = @_;
56 diag "Test::WWW::Selenium running $getter (@_[1..$#_])"
57 11 ▶ if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 11 11
58 $name = "$getter, '$str'"
59 11 ▶ if $self->{default_names} and !defined $name;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"default_names"} and not defined $name 1 10 11
Condition66%2/3
$self->{"default_names"} and not defined $name
!ll&&!rl&&r
0101
MC/DC50%1/2
$self->{"default_names"} and not defined $name
$self->{"default_names"} not defined $name
Truth table66%2/3
A and not(B)
A= $self->{"default_names"}B= not defined $name
ABresult
0X0
100
111
60 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
61 11 my $rc = $Test->$comparator( $self->$getter, $str, $name );
62 11 ▶ if (!$rc && $self->error_callback) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $rc and $self->error_callback) 0 11 11
Condition66%2/3
not $rc and $self->error_callback
!ll&&!rl&&r
650
MC/DC0%0/2
not $rc and $self->error_callback
not $rc $self->error_callback
Truth table66%2/3
not(A) and B
A= not $rcB= $self->error_callback
ABresult
0X0
100
111
63 0 &{$self->error_callback}( $name, $self );
64 }
65 11 return $rc;
66 4 };
67 }
68 else {
69 24 ▶ $sub = sub {
Subroutine __ANON__: called CC=6 SCAR=18.2
70 24 my( $self, $locator, $str, $name ) = @_;
71 diag "Test::WWW::Selenium running $getter (@_[1..$#_])"
72 24 ▶ if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 24 24
73 $name = "$getter, $locator, '$str'"
74 24 ▶ if $self->{default_names} and !defined $name;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"default_names"} and not defined $name 0 24 24
Condition33%1/3
$self->{"default_names"} and not defined $name
!ll&&!rl&&r
0240
MC/DC0%0/2
$self->{"default_names"} and not defined $name
$self->{"default_names"} not defined $name
Truth table33%1/3
A and not(B)
A= $self->{"default_names"}B= not defined $name
ABresult
0X0
100
111
75 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
76 24 my $rc = $Test->$comparator( $self->$getter($locator), $str, $name );
77 24 ▶ if (!$rc && $self->error_callback) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $rc and $self->error_callback) 4 20 24
Condition100%3/3
not $rc and $self->error_callback
!ll&&!rl&&r
1284
MC/DC100%2/2
not $rc and $self->error_callback
not $rc $self->error_callback
Truth table100%3/3
not(A) and B
A= not $rcB= $self->error_callback
ABresult
0X0
100
111
78 4 &{$self->error_callback}( $name, $self );
79 }
80 24 return $rc;
81 4 };
82 }
83 }
84 elsif ($name =~ /(\w+?)_?ok$/i) {
85 2 my $cmd = $1;
86  
87 # make a subroutine for ok() around the selenium command
88 6 ▶ $sub = sub {
Subroutine __ANON__: called CC=11 SCAR=24.2
89 6 my( $self, $arg1, $arg2, $name ) = @_;
90 6 ▶ if ($self->{default_names} and !defined $name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"default_names"} and not defined $name) 4 2 6
Condition100%3/3
$self->{"default_names"} and not defined $name
!ll&&!rl&&r
114
MC/DC100%2/2
$self->{"default_names"} and not defined $name
$self->{"default_names"} not defined $name
Truth table100%3/3
A and not(B)
A= $self->{"default_names"}B= not defined $name
ABresult
0X0
100
111
91 4 $name = $cmd;
92 4 ▶ $name .= ", $arg1" if defined $arg1;
Branch: 1 branch
BranchTrueFalseTotal
if defined $arg1 3 1 4
93 4 ▶ $name .= ", $arg2" if defined $arg2;
Branch: 1 branch
BranchTrueFalseTotal
if defined $arg2 3 1 4
94 }
95 diag "Test::WWW::Selenium running $cmd (@_[1..$#_])"
96 6 ▶ if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 6 6
97  
98 6 local $Test::Builder::Level = $Test::Builder::Level + 1;
99 6 my $rc = '';
100 6 eval { $rc = $self->$cmd( $arg1, $arg2 ) };
101 6 ▶ die $@ if $@ and $@ =~ /Can't locate object method/;
Branch: 1 branch
BranchTrueFalseTotal
if $@ and $@ =~ /Can't locate object method/ 1 5 6
Condition100%3/3
$@ and $@ =~ /Can't locate object method/
!ll&&!rl&&r
411
MC/DC100%2/2
$@ and $@ =~ /Can't locate object method/
$@ $@ =~ /Can't locate object method/
Truth table100%3/3
A and B
A= $@B= $@ =~ /Can't locate object method/
ABresult
0X0
100
111
102 5 ▶ diag($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 4 5
103 5 $rc = ok( $rc, $name );
104 5 ▶ if (!$rc && $self->error_callback) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $rc and $self->error_callback) 0 5 5
Condition66%2/3
not $rc and $self->error_callback
!ll&&!rl&&r
410
MC/DC0%0/2
not $rc and $self->error_callback
not $rc $self->error_callback
Truth table66%2/3
not(A) and B
A= not $rcB= $self->error_callback
ABresult
0X0
100
111
105 0 &{$self->error_callback}( $name, $self );
106 }
107 5 return $rc;
108 2 };
109 }
110  
111 # jump directly to the new subroutine, avoiding an extra frame stack
112 11 ▶ if ($sub) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sub) { } 10 1 11
113 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
114 10 *{$AUTOLOAD} = $sub;
115 10 goto &$AUTOLOAD;
116 }
117 else {
118 # try to pass through to WWW::Selenium
119 1 my $sel = 'WWW::Selenium';
120 1 my $sub = "${sel}::${name}";
121 1 ▶ goto &$sub if exists &$sub;
Branch: 1 branch
BranchTrueFalseTotal
if exists &$sub 0 1 1
122 1 my ($package, $filename, $line) = caller;
123 1 die qq(Can't locate object method "$name" via package ")
124 . __PACKAGE__
125 . qq(" (also tried "$sel") at $filename line $line\n);
126 }
127 }
128  
129 15 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
130 15 my ($class, %opts) = @_;
131 my $default_names = defined $opts{default_names} ?
132 15 ▶ delete $opts{default_names} : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{'default_names'} ? : 2 13 15
133 my $error_callback = defined $opts{error_callback} ?
134 15 ▶ delete $opts{error_callback} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{'error_callback'} ? : 1 14 15
135 15 my $self = $class->SUPER::new(%opts);
136 15 $self->{default_names} = $default_names;
137 15 $self->{error_callback} = $error_callback;
138 15 $self->start;
139 15 return $self;
140 }
141  
142 22 ▶sub error_callback {
Subroutine error_callback: called CC=2 SCAR=7.6
Pod documented
143 22 my ($self, $cb) = @_;
144 22 ▶ if (defined($cb)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cb) 0 22 22
145 0 $self->{error_callback} = $cb;
146 }
147 22 return $self->{error_callback};
148 }
149  
150  
151 0 ▶sub debug {
Subroutine debug: not called CC=2 SCAR=17.9
Pod documented
152 0 my $self = shift;
153 0 require Devel::REPL;
154 0 my $repl = Devel::REPL->new(prompt => 'Selenium$ ');
155 0 $repl->load_plugin($_) for qw/History LexEnv Colors Selenium Interrupt/;
156 0 $repl->selenium($self);
157 0 $repl->lexical_environment->do($repl->selenium_lex_env);
158 0 $repl->run;
159 }
160  
161 7 1;
162  
163 __END__
↑ Summary lib/WWW/Selenium/Util.pm »