| 1 | package Selenium::Remote::Mock::RemoteConnection; | ||||||||||||||||||||||||||||
| 2 | 11 | $Selenium::Remote::Mock::RemoteConnection::VERSION = '1.50'; | |||||||||||||||||||||||||||
| 3 | # ABSTRACT: utility class to mock the responses from Selenium server | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 11 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 11 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 11 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 11 | ▶ | use JSON; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 11 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 11 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 11 | ▶ | use HTTP::Response; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 11 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 11 | extends 'Selenium::Remote::RemoteConnection'; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | has 'spec' => ( | ||||||||||||||||||||||||||||
| 18 | is => 'ro', | ||||||||||||||||||||||||||||
| 19 | 8 | ▶ | default => sub { {} }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 11 | ); | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 11 | has 'mock_cmds' => ( is => 'ro', ); | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | has 'fake_session_id' => ( | ||||||||||||||||||||||||||||
| 25 | is => 'lazy', | ||||||||||||||||||||||||||||
| 26 | 1 | ▶ | builder => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
_build_fake_session_id: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 1 | my $id = join '', | |||||||||||||||||||||||||||
| 28 | map +( 0 .. 9, 'a' .. 'z', 'A' .. 'Z' )[ rand( 10 + 26 * 2 ) ], | ||||||||||||||||||||||||||||
| 29 | 1 .. 50; | ||||||||||||||||||||||||||||
| 30 | 1 | return $id; | |||||||||||||||||||||||||||
| 31 | }, | ||||||||||||||||||||||||||||
| 32 | 11 | ); | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | has 'record' => ( | ||||||||||||||||||||||||||||
| 35 | is => 'ro', | ||||||||||||||||||||||||||||
| 36 | 16 | ▶ | default => sub { 0 } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 11 | ); | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 11 | has 'replay' => ( is => 'ro', ); | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 11 | has 'replay_file' => ( is => 'ro', ); | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | has 'session_store' => ( | ||||||||||||||||||||||||||||
| 44 | is => 'rw', | ||||||||||||||||||||||||||||
| 45 | 16 | ▶ | default => sub { {} } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 46 | 11 | ); | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | has 'session_id' => ( | ||||||||||||||||||||||||||||
| 49 | is => 'rw', | ||||||||||||||||||||||||||||
| 50 | 16 | ▶ | default => sub { undef }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 51 | 11 | ); | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | has 'remote_server_addr' => ( | ||||||||||||||||||||||||||||
| 54 | is => 'lazy', | ||||||||||||||||||||||||||||
| 55 | 0 | ▶ | default => sub { 'localhost' } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 56 | 11 | ); | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 16 | ▶ | sub BUILD { | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=8 SCAR=22.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 60 | 16 | my $self = shift; | |||||||||||||||||||||||||||
| 61 | 16 | ▶ | croak 'Cannot define replay and record attributes at the same time' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->replay and $self->record
MC/DC0%0/2
$self->replay and $self->record
$self->replay
$self->record
Truth table66%2/3
A and B
A= $self->replayB= $self->record
| |||||||||||||||||||||||||||||
| 62 | if ( ( $self->replay ) && ( $self->record ) ); | ||||||||||||||||||||||||||||
| 63 | 16 | ▶ | croak 'replay_file attribute needs to be defined' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->replay and not $self->replay_file
MC/DC0%0/2
$self->replay and not $self->replay_file
$self->replay
not $self->replay_file
Truth table66%2/3
A and not(B)
A= $self->replayB= not $self->replay_file
| |||||||||||||||||||||||||||||
| 64 | if ( ( $self->replay ) && !( $self->replay_file ) ); | ||||||||||||||||||||||||||||
| 65 | 16 | ▶ | croak 'replay attribute needs to be defined' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $self->replay and $self->replay_file
MC/DC0%0/2
not $self->replay and $self->replay_file
not $self->replay
$self->replay_file
Truth table66%2/3
not(A) and B
A= not $self->replayB= $self->replay_file
| |||||||||||||||||||||||||||||
| 66 | if ( !( $self->replay ) && ( $self->replay_file ) ); | ||||||||||||||||||||||||||||
| 67 | 16 | $self->port('4444'); | |||||||||||||||||||||||||||
| 68 | 16 | ▶ | if ( $self->replay ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 8 | $self->load_session_store( $self->replay_file ); | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 18 | ▶ | sub check_status { | ||||||||||||||||||||||||||
|
Subroutine
check_status: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 74 | 18 | return; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 8 | ▶ | sub load_session_store { | ||||||||||||||||||||||||||
|
Subroutine
load_session_store: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 78 | 8 | my $self = shift; | |||||||||||||||||||||||||||
| 79 | 8 | my $file = shift; | |||||||||||||||||||||||||||
| 80 | 8 | ▶ | croak "'$file' is not a valid file" unless ( -f $file ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 8 | ▶ | open( my $fh, '<', $file ) or croak "Opening '$file' failed"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | # here we use a fake session id since we have no way of figuring out | ||||||||||||||||||||||||||||
| 84 | # which session is good or not | ||||||||||||||||||||||||||||
| 85 | 8 | local $/ = undef; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 8 | my $json = JSON->new; | |||||||||||||||||||||||||||
| 88 | 8 | $json->allow_blessed; | |||||||||||||||||||||||||||
| 89 | 8 | my $decoded_json = $json->allow_nonref(1)->utf8(1)->decode(<$fh>); | |||||||||||||||||||||||||||
| 90 | 8 | close($fh); | |||||||||||||||||||||||||||
| 91 | 8 | $self->session_store($decoded_json); | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 0 | ▶ | sub dump_session_store { | ||||||||||||||||||||||||||
|
Subroutine
dump_session_store: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 95 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 96 | 0 | my ($file) = @_; | |||||||||||||||||||||||||||
| 97 | 0 | ▶ | open( my $fh, '>', $file ) or croak "Opening '$file' failed"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 0 | my $session_store = $self->session_store; | |||||||||||||||||||||||||||
| 99 | 0 | my $dump = {}; | |||||||||||||||||||||||||||
| 100 | 0 | foreach my $path ( keys %{$session_store} ) { | |||||||||||||||||||||||||||
| 101 | 0 | $dump->{$path} = $session_store->{$path}; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | 0 | my $json = JSON->new; | |||||||||||||||||||||||||||
| 104 | 0 | $json->allow_blessed; | |||||||||||||||||||||||||||
| 105 | 0 | my $json_session = $json->allow_nonref->utf8->pretty->encode($dump); | |||||||||||||||||||||||||||
| 106 | 0 | print $fh $json_session; | |||||||||||||||||||||||||||
| 107 | 0 | close($fh); | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 299 | ▶ | sub request { | ||||||||||||||||||||||||||
|
Subroutine
request: called CC=12 SCAR=25.0
Pod
documented
| |||||||||||||||||||||||||||||
| 111 | 299 | my $self = shift; | |||||||||||||||||||||||||||
| 112 | 299 | my ( $resource, $params ) = @_; | |||||||||||||||||||||||||||
| 113 | 299 | my $method = $resource->{method}; | |||||||||||||||||||||||||||
| 114 | 299 | my $url = $resource->{url}; | |||||||||||||||||||||||||||
| 115 | 299 | ▶ | my $no_content_success = $resource->{no_content_success} // 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
$resource->{"no_content_success"} // 0
MC/DC0%0/1
$resource->{"no_content_success"} // 0
$resource->{"no_content_success"}
Truth table50%1/2
A
A= $resource->{"no_content_success"}
| |||||||||||||||||||||||||||||
| 116 | 299 | my $content = ''; | |||||||||||||||||||||||||||
| 117 | 299 | my $json = JSON->new; | |||||||||||||||||||||||||||
| 118 | 299 | $json->allow_blessed; | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 299 | ▶ | if ($params) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 299 | $content = $json->allow_nonref->utf8->canonical(1)->encode($params); | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | 299 | my $url_params = $resource->{url_params}; | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 299 | ▶ | print "REQ: $method, $url, $content\n" if $self->debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 299 | ▶ | if ( $self->record ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 0 | my $response = $self->SUPER::request( $resource, $params, 1 ); | |||||||||||||||||||||||||||
| 129 | 0 | push @{ $self->session_store->{"$method $url $content"} }, | |||||||||||||||||||||||||||
| 130 | $response->as_string; | ||||||||||||||||||||||||||||
| 131 | 0 | return $self->_process_response( $response, $no_content_success ); | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | 299 | ▶ | if ( $self->replay ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 201 | my $resp; | |||||||||||||||||||||||||||
| 135 | 201 | ▶ | my $arr_of_resps = $self->session_store->{"$method $url $content"} | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->session_store->{"$method $url $content"} // []
MC/DC100%1/1
$self->session_store->{"$method $url $content"} // []
$self->session_store->{"$method $url $content"}
Truth table100%2/2
A
A= $self->session_store->{"$method $url $content"}
| |||||||||||||||||||||||||||||
| 136 | // []; | ||||||||||||||||||||||||||||
| 137 | 201 | ▶ | if ( scalar(@$arr_of_resps) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 200 | $resp = shift @$arr_of_resps; | |||||||||||||||||||||||||||
| 139 | 200 | $resp = HTTP::Response->parse($resp); | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | else { | ||||||||||||||||||||||||||||
| 142 | 1 | $resp = HTTP::Response->new( '501', "Failed to find a response" ); | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | 201 | return $self->_process_response( $resp, $no_content_success ); | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | 98 | my $mock_cmds = $self->mock_cmds; | |||||||||||||||||||||||||||
| 147 | 98 | my $spec = $self->spec; | |||||||||||||||||||||||||||
| 148 | 98 | my $cmd = $mock_cmds->get_method_name_from_parameters( | |||||||||||||||||||||||||||
| 149 | { method => $method, url => $url } ); | ||||||||||||||||||||||||||||
| 150 | 98 | my $ret = { cmd_status => 'OK', cmd_return => 1 }; | |||||||||||||||||||||||||||
| 151 | 98 | ▶ | if ( defined( $spec->{$cmd} ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | 88 | my $return_sub = $spec->{$cmd}; | |||||||||||||||||||||||||||
| 153 | 88 | my $mock_return = $return_sub->( $url_params, $params ); | |||||||||||||||||||||||||||
| 154 | 88 | ▶ | if ( ref($mock_return) eq 'HASH' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 59 | $ret->{cmd_status} = $mock_return->{status}; | |||||||||||||||||||||||||||
| 156 | 59 | $ret->{cmd_return} = $mock_return->{return}; | |||||||||||||||||||||||||||
| 157 | 59 | ▶ | $ret->{cmd_error} = $mock_return->{error} // ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
$mock_return->{"error"} // ""
MC/DC100%1/1
$mock_return->{"error"} // ""
$mock_return->{"error"}
Truth table100%2/2
A
A= $mock_return->{"error"}
| |||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | else { | ||||||||||||||||||||||||||||
| 160 | 29 | $ret = $mock_return; | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | 88 | ▶ | $ret->{session_id} = $self->fake_session_id if ( ref($ret) eq 'HASH' ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | else { | ||||||||||||||||||||||||||||
| 165 | 10 | $ret->{sessionId} = $self->fake_session_id; | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | 98 | return $ret; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 11 | 1; | |||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | __END__ | ||||||||||||||||||||||||||||