RemoteConnection.pm 5.2 KB

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