| 1 | package Selenium::Remote::Mock::RemoteConnection; | ||||||||||||||||||||||||||||
| 2 | 11 | $Selenium::Remote::Mock::RemoteConnection::VERSION = '1.49'; | |||||||||||||||||||||||||||
| 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 | builder => sub { | ||||||||||||||||||||||||||||
| 27 | 7 | ▶ | my $id = join '', | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
_build_fake_session_id: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | map +( 0 .. 9, 'a' .. 'z', 'A' .. 'Z' )[ rand( 10 + 26 * 2 ) ], | ||||||||||||||||||||||||||||
| 29 | 1 .. 50; | ||||||||||||||||||||||||||||
| 30 | 7 | 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 | sub BUILD { | ||||||||||||||||||||||||||||
| 60 | 16 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=8 SCAR=22.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 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 | sub check_status { | ||||||||||||||||||||||||||||
| 74 | 18 | ▶ | return; | ||||||||||||||||||||||||||
|
Subroutine
check_status: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | sub load_session_store { | ||||||||||||||||||||||||||||
| 78 | 8 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
load_session_store: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 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 | sub dump_session_store { | ||||||||||||||||||||||||||||
| 95 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
dump_session_store: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 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 | sub request { | ||||||||||||||||||||||||||||
| 111 | 299 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
request: called CC=12 SCAR=25.0
Pod
documented
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | =pod | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | =head1 NAME | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | Selenium::Remote::Mock::RemoteConnection - utility class to mock the responses from Selenium server | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | =head1 VERSION | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | version 1.49 | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | =head2 Record interactions | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | use strict; | ||||||||||||||||||||||||||||
| 191 | use warnings; | ||||||||||||||||||||||||||||
| 192 | use Selenium::Remote::Driver; | ||||||||||||||||||||||||||||
| 193 | use Selenium::Remote::Mock::RemoteConnection; | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | # create a new Mock object to record the interactions with Selenium | ||||||||||||||||||||||||||||
| 196 | # Server | ||||||||||||||||||||||||||||
| 197 | my $mock_connection = Selenium::Remote::Mock::RemoteConnection->new( record => 1 ); | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | # the Mock object is passed to the driver in place of what would be | ||||||||||||||||||||||||||||
| 200 | # a regular Selenium::Remote::RemoteConnection object | ||||||||||||||||||||||||||||
| 201 | my $driver = Selenium::Remote::Driver->new( remote_conn => $mock_connection ); | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | # always store the session id, as it will become undef once | ||||||||||||||||||||||||||||
| 204 | # $driver->quit is called | ||||||||||||||||||||||||||||
| 205 | my $session_id = $driver->session_id; | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | # do all the selenium things and quit | ||||||||||||||||||||||||||||
| 208 | $driver->get('http://www.google.com'); | ||||||||||||||||||||||||||||
| 209 | $driver->get('http://www.wikipedia.com'); | ||||||||||||||||||||||||||||
| 210 | $driver->quit; | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | # dump the session to a file | ||||||||||||||||||||||||||||
| 213 | $mock_connection->dump_session_store( 'my_record.json' ); | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | This code, above doing some basic Selenium interactions, will end up generating a JSON file containing all the requests and their responses for your Selenium session. | ||||||||||||||||||||||||||||
| 216 | The JSON file looks like this : | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | '{ | ||||||||||||||||||||||||||||
| 219 | "HTTP_REQUEST_URL {request_parameters}":[response1,response2,...], | ||||||||||||||||||||||||||||
| 220 | ... | ||||||||||||||||||||||||||||
| 221 | }' | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | The reason why we store array of responses is that the exact same request can be made more than once during a session, so we have to store every response to the same requests. | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | =head2 Replay interactions | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | #!perl | ||||||||||||||||||||||||||||
| 228 | use strict; | ||||||||||||||||||||||||||||
| 229 | use warnings; | ||||||||||||||||||||||||||||
| 230 | use Test::More; | ||||||||||||||||||||||||||||
| 231 | use Test::Selenium::Remote::Driver; | ||||||||||||||||||||||||||||
| 232 | use Selenium::Remote::Mock::RemoteConnection; | ||||||||||||||||||||||||||||
| 233 | my $mock_connection_2 = | ||||||||||||||||||||||||||||
| 234 | Selenium::Remote::Mock::RemoteConnection->new( replay => 1, | ||||||||||||||||||||||||||||
| 235 | replay_file => 'my_record.json' ); | ||||||||||||||||||||||||||||
| 236 | # javascript + version parameters added or else it will not work | ||||||||||||||||||||||||||||
| 237 | my $driver = | ||||||||||||||||||||||||||||
| 238 | Test::Selenium::Remote::Driver->new( remote_conn => $mock_connection_2, javascript => 1, version => '' ); | ||||||||||||||||||||||||||||
| 239 | $driver->get_ok('http://www.google.com'); | ||||||||||||||||||||||||||||
| 240 | $driver->get_ok('http://www.wikipedia.com'); | ||||||||||||||||||||||||||||
| 241 | $driver->quit; | ||||||||||||||||||||||||||||
| 242 | done_testing; | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | Using the file generated with the recording snippet from the section before, we are able to mock the responses. | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | Note that there is one small limitation (that I hope to remove in future versions), is that a record generated with L<Selenium::Remote::Driver> is not directly useable with L<Test::Selenium::Remote::Driver>. | ||||||||||||||||||||||||||||
| 247 | This is mainly because the way the two instances are created are a bit different, which leads to different requests made, for creating a session for instance. | ||||||||||||||||||||||||||||
| 248 | For now, what works for sure is recording and replaying from the same class. | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | =head2 Mock responses | ||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | #!perl | ||||||||||||||||||||||||||||
| 253 | use Test::More; | ||||||||||||||||||||||||||||
| 254 | use Test::Selenium::Remote::Driver; | ||||||||||||||||||||||||||||
| 255 | use Selenium::Remote::WebElement; | ||||||||||||||||||||||||||||
| 256 | use Selenium::Remote::Mock::Commands; | ||||||||||||||||||||||||||||
| 257 | use Selenium::Remote::Mock::RemoteConnection; | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | my $spec = { | ||||||||||||||||||||||||||||
| 260 | findElement => sub { | ||||||||||||||||||||||||||||
| 261 | my (undef,$searched_item) = @_; | ||||||||||||||||||||||||||||
| 262 | return { status => 'OK', return => { ELEMENT => '123456' } } | ||||||||||||||||||||||||||||
| 263 | if ( $searched_item->{value} eq 'q' ); | ||||||||||||||||||||||||||||
| 264 | return { status => 'NOK', return => 0, error => 'element not found' }; | ||||||||||||||||||||||||||||
| 265 | }, | ||||||||||||||||||||||||||||
| 266 | getPageSource => sub { return 'this output matches regex'}, | ||||||||||||||||||||||||||||
| 267 | }; | ||||||||||||||||||||||||||||
| 268 | my $mock_commands = Selenium::Remote::Mock::Commands->new; | ||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | my $successful_driver = | ||||||||||||||||||||||||||||
| 271 | Test::Selenium::Remote::Driver->new( | ||||||||||||||||||||||||||||
| 272 | remote_conn => Selenium::Remote::Mock::RemoteConnection->new( spec => $spec, mock_cmds => $mock_commands ), | ||||||||||||||||||||||||||||
| 273 | commands => $mock_commands, | ||||||||||||||||||||||||||||
| 274 | ); | ||||||||||||||||||||||||||||
| 275 | $successful_driver->find_element_ok('q','find_element_ok works'); | ||||||||||||||||||||||||||||
| 276 | dies_ok { $successful_driver->find_element_ok('notq') } 'find_element_ok dies if element not found'; | ||||||||||||||||||||||||||||
| 277 | $successful_driver->find_no_element_ok('notq','find_no_element_ok works'); | ||||||||||||||||||||||||||||
| 278 | $successful_driver->content_like( qr/matches/, 'content_like works'); | ||||||||||||||||||||||||||||
| 279 | $successful_driver->content_unlike( qr/nomatch/, 'content_unlike works'); | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | done_testing(); | ||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | Mocking responses by hand requires a more advanced knowledge of the underlying implementation of L<Selenium::Remote::Driver>. | ||||||||||||||||||||||||||||
| 284 | What we mock here is the processed response that will be returned by L<Selenium::Remote::RemoteConnection> to '_execute_command' call. | ||||||||||||||||||||||||||||
| 285 | To accomplish this we need : | ||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | =over | ||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | =item * | ||||||||||||||||||||||||||||
| 290 | a spec: a HASHREF which keys are the name of the methods we want to mock. Note that those keys should also be valid keys from the _cmds attribute in L<Selenium::Remote::Commands>. | ||||||||||||||||||||||||||||
| 291 | The value of each key is a sub which will be given two parameters: | ||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | =over | ||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | =item * | ||||||||||||||||||||||||||||
| 296 | $url_params : the values that should have been replaced in the URL | ||||||||||||||||||||||||||||
| 297 | For instance, on the example above, it would have been: | ||||||||||||||||||||||||||||
| 298 | { session_id => 'some_session_id'} | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | =item * | ||||||||||||||||||||||||||||
| 301 | $params : the original parameters of the request. | ||||||||||||||||||||||||||||
| 302 | On the example above it would have been: | ||||||||||||||||||||||||||||
| 303 | { value => 'q', using => 'xpath'} | ||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | =back | ||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | The sub used as a value in the spec is not expected to return anything, so you have to craft very carefully what you return so that it will produce the expected result. | ||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | =item * | ||||||||||||||||||||||||||||
| 310 | a mock_cmd: a L<Selenium::Remote::Mock::Commands> object. This is used mainly to hijack the normal commands so that placeholders do not get replaced in the URLs. | ||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | =back | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | Selenium::Remote::Mock::RemoteConnection is a class to act as a short-circuit or a pass through to the connection to a Selenium Server. | ||||||||||||||||||||||||||||
| 317 | Using this class in place of L<Selenium::Remote::RemoteConnection> allows to: | ||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||
| 319 | =over | ||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | =item * | ||||||||||||||||||||||||||||
| 322 | record interactions with the Selenium Server into a JSON file | ||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | =item * | ||||||||||||||||||||||||||||
| 325 | replay recorded interactions from a JSON file to mock answers from the Selenium Server | ||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | =item * | ||||||||||||||||||||||||||||
| 328 | mock responses to specific functions | ||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | =back | ||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | =for Pod::Coverage *EVERYTHING* | ||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | =head1 BUGS | ||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | This code is really early alpha, so its API might change. Use with caution ! | ||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | =head1 AUTHORS | ||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | Current Maintainers: | ||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | =over 4 | ||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | =item * | ||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | George S. Baugh <george@troglodyne.net> | ||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | =back | ||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | Previous maintainers: | ||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||
| 352 | =over 4 | ||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | =item * | ||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | Daniel Gempesaw <gempesaw@gmail.com> | ||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | =item * | ||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | Emmanuel Peroumalnaïk <peroumalnaik.emmanuel@gmail.com> | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | =item * | ||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||
| 364 | Luke Closs <cpan@5thplane.com> | ||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | =item * | ||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | Mark Stosberg <mark@stosberg.com> | ||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | =back | ||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | Original authors: | ||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | =over 4 | ||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | =item * | ||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | Aditya Ivaturi <ivaturi@gmail.com> | ||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | =back | ||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||
| 384 | Copyright (c) 2010-2011 Aditya Ivaturi, Gordon Child | ||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||
| 386 | Copyright (c) 2014-2017 Daniel Gempesaw | ||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||
| 388 | Copyright (c) 2018-2021 George S. Baugh | ||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | Licensed under the Apache License, Version 2.0 (the "License"); | ||||||||||||||||||||||||||||
| 391 | you may not use this file except in compliance with the License. | ||||||||||||||||||||||||||||
| 392 | You may obtain a copy of the License at | ||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||
| 394 | http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||
| 396 | Unless required by applicable law or agreed to in writing, software | ||||||||||||||||||||||||||||
| 397 | distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||||||||||||||||||||
| 398 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||||||||||||||||||||
| 399 | See the License for the specific language governing permissions and | ||||||||||||||||||||||||||||
| 400 | limitations under the License. | ||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||
| 402 | =cut | ||||||||||||||||||||||||||||