Test/Selenium/Remote/Role/DoesTesting.pm

filter: Statementstmt 92.7% 90 / 97 Branchbran 89.2% 25 / 28 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 90.0% 18 / 20 Podpod n/a totaltotal 90.6% 136 / 150 CC 17 SCAR 12.4
CC
17
Cov
91%
__ANON__
7.1
_check_ok
6.0
_build_sub
3.0
CRAP 3.5
« Selenium/Firefox/Profile.pm ↑ Summary Selenium/Waiter.pm »
1 package Test::Selenium::Remote::Role::DoesTesting;
2 15 $Test::Selenium::Remote::Role::DoesTesting::VERSION = '1.50';
3 # ABSTRACT: Role to cope with everything that is related to testing (could
4 # be reused in both testing classes)
5  
6 15 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 15 ▶use Test::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 15 ▶use List::Util qw/any/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 15 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 15 requires qw(func_list has_args);
14  
15 has _builder => (
16 is => 'lazy',
17 5 ▶ builder => sub { return Test::Builder->new() },
Subroutine _build__builder: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
18 15 handles => [qw/is_eq isnt_eq like unlike ok croak/],
19 );
20  
21 # get back the key value from an already coerced finder (default finder)
22  
23 9 ▶sub _get_finder_key {
Subroutine _get_finder_key: called CC=3 SCAR=11.0
24 9 my $self = shift;
25 9 my $finder_value = shift;
26  
27 9 foreach my $k ( keys %{ $self->FINDERS } ) {
28 28 ▶ return $k if ( $self->FINDERS->{$k} eq $finder_value );
Branch: 1 branch
BranchTrueFalseTotal
if $self->FINDERS->{$k} eq $finder_value 9 19 28
29 }
30  
31 0 return;
32 }
33  
34 # main method for non ok tests
35  
36 28 ▶sub _check_method {
Subroutine _check_method: called CC=1 SCAR=0.0
37 28 my $self = shift;
38 28 my $method = shift;
39 28 my $method_to_test = shift;
40 28 $method = "get_$method";
41 28 my @args = @_;
42 28 my $rv;
43 28 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
44 28 my $num_of_args = $self->has_args($method);
45 28 my @r_args = splice( @args, 0, $num_of_args );
46 28 $rv = $self->$method(@r_args);
47 }
48 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
49 0 $self->croak($_);
50 28 };
51  
52 28 return $self->$method_to_test( $rv, @args );
53 }
54  
55 # main method for _ok tests
56 # a bit hacked so that find_no_element_ok can also be processed
57  
58 31 ▶sub _check_ok {
Subroutine _check_ok: called CC=6 SCAR=18.0
59 31 my $self = shift;
60 31 my $method = shift;
61  
62 31 my @args = @_;
63 31 my ( $rv, $num_of_args, @r_args );
64 31 ▶ try {
Subroutine __ANON__: called CC=7 SCAR=19.6
65 31 $num_of_args = $self->has_args($method);
66 31 @r_args = splice( @args, 0, $num_of_args );
67 31 ▶ if ( $method =~ m/^find(_no|_child)?_element/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method =~ /^find(_no|_child)?_element/) 10 21 31
68  
69 # case find_element_ok was called with no arguments
70 10 ▶ if ( scalar(@r_args) - $num_of_args == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar(@r_args) - $num_of_args == 1) { } 0 10 10
71 0 push @r_args, $self->_get_finder_key( $self->default_finder );
72 }
73 else {
74 10 ▶ if ( scalar(@r_args) == $num_of_args ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @r_args == $num_of_args) 8 2 10
75  
76 # case find_element was called with no finder but
77 # a test description
78 8 my $finder = $r_args[ $num_of_args - 1 ];
79 8 my @FINDERS = keys( %{ $self->FINDERS } );
80 8 ▶ unless ( any { $finder eq $_ } @FINDERS ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless (&any(sub { $finder eq $_; } , @FINDERS)) 4 4 8
81 4 $r_args[ $num_of_args - 1 ] =
82 $self->_get_finder_key( $self->default_finder );
83 4 push @args, $finder;
84 }
85 }
86 }
87 }
88  
89 # quick hack to fit 'find_no_element' into check_ok logic
90 31 ▶ if ( $method eq 'find_no_element' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq 'find_no_element') { } 3 28 31
91 # If we use `find_element` and find nothing, the error
92 # handler is incorrectly invoked. Doing a `find_elements`
93 # and checking that it returns an empty array does not
94 # invoke the error_handler. See
95 # https://github.com/gempesaw/Selenium-Remote-Driver/issues/253
96 3 my $elements = $self->find_elements(@r_args);
97 1 ▶ if ( @{$elements} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$elements;}) { } 1 0 1
98 1 $rv = $elements->[0];
99 }
100 else {
101 0 $rv = 1; # empty list means success
102 }
103 }
104 else {
105 28 $rv = $self->$method(@r_args); # a true $rv means success
106 }
107 }
108 4 ▶ catch {
Subroutine __ANON__: called CC=2 SCAR=6.9
109 4 ▶ if ($method eq 'find_no_element') {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq 'find_no_element') { } 2 2 4
110 2 $rv = 1; # an exception from find_elements() means success
111 }
112 else {
113 2 $self->croak($_);
114 }
115 31 };
116  
117 # test description might have been explicitly passed
118 29 my $test_name = pop @args;
119  
120 # generic test description when no explicit test description was passed
121 29 ▶ if ( ! defined $test_name ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $test_name) 15 14 29
122 $test_name = $num_of_args > 0 ?
123 15 ▶ join( ' ', $method, map { q{'$_'} } @r_args )
Branch: 1 branch
BranchTrueFalseTotal
$num_of_args > 0 ? : 9 6 15
124 :
125 $method;
126 }
127  
128 # case when find_no_element found an element, we should croak
129 29 ▶ if ( $method eq 'find_no_element' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "find_no_element") 3 26 29
130 3 ▶ if ( blessed($rv) && $rv->isa('Selenium::Remote::WebElement') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($rv) and $rv->isa("Selenium::Remote::WebElement")) 1 2 3
Condition66%2/3
builtin::blessed($rv) and $rv->isa("Selenium::Remote::WebElement")
!ll&&!rl&&r
201
MC/DC50%1/2
builtin::blessed($rv) and $rv->isa("Selenium::Remote::WebElement")
builtin::blessed($rv) $rv->isa("Selenium::Remote::WebElement")
Truth table66%2/3
A and B
A= builtin::blessed($rv)B= $rv->isa("Selenium::Remote::WebElement")
ABresult
0X0
100
111
131 1 $self->croak($test_name);
132 }
133 }
134  
135 28 return $self->ok( $rv, $test_name );
136 }
137  
138 # build the subs with the correct arg set
139  
140 381 ▶sub _build_sub {
Subroutine _build_sub: called CC=3 SCAR=11.1
141 381 my $self = shift;
142 381 my $meth_name = shift;
143  
144 # e.g. for $meth_name = 'find_no_element_ok':
145 # $meth_comp = 'ok'
146 # $meth_without_comp = 'find_no_element'
147 381 my @meth_elements = split '_', $meth_name;
148 381 my $meth_comp = pop @meth_elements;
149 381 my $meth_without_comp = join '_', @meth_elements;
150  
151 # handle the ok testing methods
152 381 ▶ if ( $meth_comp eq 'ok' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($meth_comp eq "ok") 135 246 381
153 31 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
154 31 my $self = shift;
155  
156 31 local $Test::Builder::Level = $Test::Builder::Level + 2;
157  
158 31 return $self->_check_ok($meth_without_comp, @_);
159 135 };
160 }
161  
162 # find the Test::More comparator method
163 246 my %comparators = (
164 is => 'is_eq',
165 isnt => 'isnt_eq',
166 like => 'like',
167 unlike => 'unlike',
168 );
169  
170 # croak on unknown comparator methods
171 246 ▶ if ( ! exists $comparators{$meth_comp} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $comparators{$meth_comp}) 0 246 246
172 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
173 0 my $self = shift;
174  
175 0 return $self->croak("Sub $meth_name could not be defined");
176 0 };
177 }
178  
179 # handle check in _check_method()
180 28 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
181 28 my $self = shift;
182  
183 28 local $Test::Builder::Level = $Test::Builder::Level + 2;
184  
185 28 return $self->_check_method( $meth_without_comp, $comparators{$meth_comp}, @_ );
186 246 };
187 }
188  
189 15 1;
190  
191 =head1 NAME
192  
193 Selenium::Remote::Role::DoesTesting - Role implementing the common logic used for testing
194  
195 =cut
« Selenium/Firefox/Profile.pm ↑ Summary Selenium/Waiter.pm »