| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Selenium::Remote::Mock::RemoteConnection; |
|
2
|
|
|
|
|
|
|
$Selenium::Remote::Mock::RemoteConnection::VERSION = '1.50'; |
|
3
|
|
|
|
|
|
|
# ABSTRACT: utility class to mock the responses from Selenium server |
|
4
|
|
|
|
|
|
|
|
|
5
|
11
|
|
|
11
|
|
482164
|
use strict; |
|
|
11
|
|
|
|
|
25
|
|
|
|
11
|
|
|
|
|
447
|
|
|
6
|
11
|
|
|
11
|
|
57
|
use warnings; |
|
|
11
|
|
|
|
|
24
|
|
|
|
11
|
|
|
|
|
491
|
|
|
7
|
|
|
|
|
|
|
|
|
8
|
11
|
|
|
11
|
|
693
|
use Moo; |
|
|
11
|
|
|
|
|
11234
|
|
|
|
11
|
|
|
|
|
85
|
|
|
9
|
11
|
|
|
11
|
|
6865
|
use JSON; |
|
|
11
|
|
|
|
|
23007
|
|
|
|
11
|
|
|
|
|
95
|
|
|
10
|
11
|
|
|
11
|
|
1846
|
use Carp; |
|
|
11
|
|
|
|
|
28
|
|
|
|
11
|
|
|
|
|
1805
|
|
|
11
|
11
|
|
|
11
|
|
997
|
use Try::Tiny; |
|
|
11
|
|
|
|
|
2530
|
|
|
|
11
|
|
|
|
|
693
|
|
|
12
|
11
|
|
|
11
|
|
942
|
use HTTP::Response; |
|
|
11
|
|
|
|
|
55488
|
|
|
|
11
|
|
|
|
|
402
|
|
|
13
|
11
|
|
|
11
|
|
1345
|
use Data::Dumper; |
|
|
11
|
|
|
|
|
16721
|
|
|
|
11
|
|
|
|
|
14162
|
|
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
extends 'Selenium::Remote::RemoteConnection'; |
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
has 'spec' => ( |
|
18
|
|
|
|
|
|
|
is => 'ro', |
|
19
|
|
|
|
|
|
|
default => sub { {} }, |
|
20
|
|
|
|
|
|
|
); |
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
has 'mock_cmds' => ( is => 'ro', ); |
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
has 'fake_session_id' => ( |
|
25
|
|
|
|
|
|
|
is => 'lazy', |
|
26
|
|
|
|
|
|
|
builder => sub { |
|
27
|
8
|
|
|
8
|
|
423
|
my $id = join '', |
|
28
|
|
|
|
|
|
|
map +( 0 .. 9, 'a' .. 'z', 'A' .. 'Z' )[ rand( 10 + 26 * 2 ) ], |
|
29
|
|
|
|
|
|
|
1 .. 50; |
|
30
|
8
|
|
|
|
|
55
|
return $id; |
|
31
|
|
|
|
|
|
|
}, |
|
32
|
|
|
|
|
|
|
); |
|
33
|
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
has 'record' => ( |
|
35
|
|
|
|
|
|
|
is => 'ro', |
|
36
|
|
|
|
|
|
|
default => sub { 0 } |
|
37
|
|
|
|
|
|
|
); |
|
38
|
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
has 'replay' => ( is => 'ro', ); |
|
40
|
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
has 'replay_file' => ( is => 'ro', ); |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
has 'session_store' => ( |
|
44
|
|
|
|
|
|
|
is => 'rw', |
|
45
|
|
|
|
|
|
|
default => sub { {} } |
|
46
|
|
|
|
|
|
|
); |
|
47
|
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
has 'session_id' => ( |
|
49
|
|
|
|
|
|
|
is => 'rw', |
|
50
|
|
|
|
|
|
|
default => sub { undef }, |
|
51
|
|
|
|
|
|
|
); |
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
has 'remote_server_addr' => ( |
|
54
|
|
|
|
|
|
|
is => 'lazy', |
|
55
|
|
|
|
|
|
|
default => sub { 'localhost' } |
|
56
|
|
|
|
|
|
|
); |
|
57
|
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub BUILD { |
|
60
|
16
|
|
|
16
|
0
|
108
|
my $self = shift; |
|
61
|
16
|
50
|
66
|
|
|
169
|
croak 'Cannot define replay and record attributes at the same time' |
|
62
|
|
|
|
|
|
|
if ( ( $self->replay ) && ( $self->record ) ); |
|
63
|
16
|
50
|
66
|
|
|
97
|
croak 'replay_file attribute needs to be defined' |
|
64
|
|
|
|
|
|
|
if ( ( $self->replay ) && !( $self->replay_file ) ); |
|
65
|
16
|
50
|
66
|
|
|
100
|
croak 'replay attribute needs to be defined' |
|
66
|
|
|
|
|
|
|
if ( !( $self->replay ) && ( $self->replay_file ) ); |
|
67
|
16
|
|
|
|
|
117
|
$self->port('4444'); |
|
68
|
16
|
100
|
|
|
|
166
|
if ( $self->replay ) { |
|
69
|
8
|
|
|
|
|
39
|
$self->load_session_store( $self->replay_file ); |
|
70
|
|
|
|
|
|
|
} |
|
71
|
|
|
|
|
|
|
} |
|
72
|
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub check_status { |
|
74
|
18
|
|
|
18
|
1
|
194
|
return; |
|
75
|
|
|
|
|
|
|
} |
|
76
|
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub load_session_store { |
|
78
|
8
|
|
|
8
|
0
|
21
|
my $self = shift; |
|
79
|
8
|
|
|
|
|
17
|
my $file = shift; |
|
80
|
8
|
50
|
|
|
|
228
|
croak "'$file' is not a valid file" unless ( -f $file ); |
|
81
|
8
|
50
|
|
|
|
326
|
open( my $fh, '<', $file ) or croak "Opening '$file' failed"; |
|
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
|
|
|
|
|
105
|
local $/ = undef; |
|
86
|
|
|
|
|
|
|
|
|
87
|
8
|
|
|
|
|
171
|
my $json = JSON->new; |
|
88
|
8
|
|
|
|
|
59
|
$json->allow_blessed; |
|
89
|
8
|
|
|
|
|
47751
|
my $decoded_json = $json->allow_nonref(1)->utf8(1)->decode(<$fh>); |
|
90
|
8
|
|
|
|
|
756
|
close($fh); |
|
91
|
8
|
|
|
|
|
460
|
$self->session_store($decoded_json); |
|
92
|
|
|
|
|
|
|
} |
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
sub dump_session_store { |
|
95
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
|
96
|
0
|
|
|
|
|
0
|
my ($file) = @_; |
|
97
|
0
|
0
|
|
|
|
0
|
open( my $fh, '>', $file ) or croak "Opening '$file' failed"; |
|
98
|
0
|
|
|
|
|
0
|
my $session_store = $self->session_store; |
|
99
|
0
|
|
|
|
|
0
|
my $dump = {}; |
|
100
|
0
|
|
|
|
|
0
|
foreach my $path ( keys %{$session_store} ) { |
|
|
0
|
|
|
|
|
0
|
|
|
101
|
0
|
|
|
|
|
0
|
$dump->{$path} = $session_store->{$path}; |
|
102
|
|
|
|
|
|
|
} |
|
103
|
0
|
|
|
|
|
0
|
my $json = JSON->new; |
|
104
|
0
|
|
|
|
|
0
|
$json->allow_blessed; |
|
105
|
0
|
|
|
|
|
0
|
my $json_session = $json->allow_nonref->utf8->pretty->encode($dump); |
|
106
|
0
|
|
|
|
|
0
|
print $fh $json_session; |
|
107
|
0
|
|
|
|
|
0
|
close($fh); |
|
108
|
|
|
|
|
|
|
} |
|
109
|
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
sub request { |
|
111
|
299
|
|
|
299
|
1
|
2564
|
my $self = shift; |
|
112
|
299
|
|
|
|
|
572
|
my ( $resource, $params ) = @_; |
|
113
|
299
|
|
|
|
|
571
|
my $method = $resource->{method}; |
|
114
|
299
|
|
|
|
|
483
|
my $url = $resource->{url}; |
|
115
|
299
|
|
50
|
|
|
747
|
my $no_content_success = $resource->{no_content_success} // 0; |
|
116
|
299
|
|
|
|
|
507
|
my $content = ''; |
|
117
|
299
|
|
|
|
|
1413
|
my $json = JSON->new; |
|
118
|
299
|
|
|
|
|
1267
|
$json->allow_blessed; |
|
119
|
|
|
|
|
|
|
|
|
120
|
299
|
50
|
|
|
|
776
|
if ($params) { |
|
121
|
299
|
|
|
|
|
3400
|
$content = $json->allow_nonref->utf8->canonical(1)->encode($params); |
|
122
|
|
|
|
|
|
|
} |
|
123
|
299
|
|
|
|
|
581
|
my $url_params = $resource->{url_params}; |
|
124
|
|
|
|
|
|
|
|
|
125
|
299
|
100
|
|
|
|
1697
|
print "REQ: $method, $url, $content\n" if $self->debug; |
|
126
|
|
|
|
|
|
|
|
|
127
|
299
|
50
|
|
|
|
1000
|
if ( $self->record ) { |
|
128
|
0
|
|
|
|
|
0
|
my $response = $self->SUPER::request( $resource, $params, 1 ); |
|
129
|
0
|
|
|
|
|
0
|
push @{ $self->session_store->{"$method $url $content"} }, |
|
|
0
|
|
|
|
|
0
|
|
|
130
|
|
|
|
|
|
|
$response->as_string; |
|
131
|
0
|
|
|
|
|
0
|
return $self->_process_response( $response, $no_content_success ); |
|
132
|
|
|
|
|
|
|
} |
|
133
|
299
|
100
|
|
|
|
778
|
if ( $self->replay ) { |
|
134
|
201
|
|
|
|
|
288
|
my $resp; |
|
135
|
201
|
|
100
|
|
|
1174
|
my $arr_of_resps = $self->session_store->{"$method $url $content"} |
|
136
|
|
|
|
|
|
|
// []; |
|
137
|
201
|
100
|
|
|
|
491
|
if ( scalar(@$arr_of_resps) ) { |
|
138
|
200
|
|
|
|
|
402
|
$resp = shift @$arr_of_resps; |
|
139
|
200
|
|
|
|
|
984
|
$resp = HTTP::Response->parse($resp); |
|
140
|
|
|
|
|
|
|
} |
|
141
|
|
|
|
|
|
|
else { |
|
142
|
1
|
|
|
|
|
11
|
$resp = HTTP::Response->new( '501', "Failed to find a response" ); |
|
143
|
|
|
|
|
|
|
} |
|
144
|
201
|
|
|
|
|
123578
|
return $self->_process_response( $resp, $no_content_success ); |
|
145
|
|
|
|
|
|
|
} |
|
146
|
98
|
|
|
|
|
185
|
my $mock_cmds = $self->mock_cmds; |
|
147
|
98
|
|
|
|
|
202
|
my $spec = $self->spec; |
|
148
|
98
|
|
|
|
|
472
|
my $cmd = $mock_cmds->get_method_name_from_parameters( |
|
149
|
|
|
|
|
|
|
{ method => $method, url => $url } ); |
|
150
|
98
|
|
|
|
|
618
|
my $ret = { cmd_status => 'OK', cmd_return => 1 }; |
|
151
|
98
|
100
|
|
|
|
271
|
if ( defined( $spec->{$cmd} ) ) { |
|
152
|
88
|
|
|
|
|
158
|
my $return_sub = $spec->{$cmd}; |
|
153
|
88
|
|
|
|
|
296
|
my $mock_return = $return_sub->( $url_params, $params ); |
|
154
|
88
|
100
|
|
|
|
929
|
if ( ref($mock_return) eq 'HASH' ) { |
|
155
|
59
|
|
|
|
|
118
|
$ret->{cmd_status} = $mock_return->{status}; |
|
156
|
59
|
|
|
|
|
104
|
$ret->{cmd_return} = $mock_return->{return}; |
|
157
|
59
|
|
100
|
|
|
240
|
$ret->{cmd_error} = $mock_return->{error} // ''; |
|
158
|
|
|
|
|
|
|
} |
|
159
|
|
|
|
|
|
|
else { |
|
160
|
29
|
|
|
|
|
58
|
$ret = $mock_return; |
|
161
|
|
|
|
|
|
|
} |
|
162
|
88
|
100
|
|
|
|
1778
|
$ret->{session_id} = $self->fake_session_id if ( ref($ret) eq 'HASH' ); |
|
163
|
|
|
|
|
|
|
} |
|
164
|
|
|
|
|
|
|
else { |
|
165
|
10
|
|
|
|
|
206
|
$ret->{sessionId} = $self->fake_session_id; |
|
166
|
|
|
|
|
|
|
} |
|
167
|
98
|
|
|
|
|
1227
|
return $ret; |
|
168
|
|
|
|
|
|
|
} |
|
169
|
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
1; |
|
171
|
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
__END__ |