| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | # case find_element_ok was called with no arguments | ||||||||||||||||||||||||||||
| 70 | 10 | ▶ | if ( scalar(@r_args) - $num_of_args == 1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 122 | $test_name = $num_of_args > 0 ? | ||||||||||||||||||||||||||||
| 123 | 15 | ▶ | join( ' ', $method, map { q{'$_'} } @r_args ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 130 | 3 | ▶ | if ( blessed($rv) && $rv->isa('Selenium::Remote::WebElement') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($rv) and $rv->isa("Selenium::Remote::WebElement")
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")
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||