MockRemoteConnection.pm 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package MockRemoteConnection;
  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 lib 't/lib';
  8. # use MockCommands;
  9. extends 'Selenium::Remote::RemoteConnection';
  10. has 'spec' => (
  11. is => 'ro',
  12. required => 1,
  13. );
  14. has 'mock_cmds' => (
  15. is => 'ro',
  16. # default => sub { return MockCommands->new }
  17. );
  18. has 'fake_session_id' => (
  19. is => 'lazy',
  20. builder => sub {
  21. my $id = join '',
  22. map +( 0 .. 9, 'a' .. 'z', 'A' .. 'Z' )[ rand( 10 + 26 * 2 ) ], 1 .. 50;
  23. return $id;
  24. },
  25. );
  26. has 'record' => (
  27. is => 'ro',
  28. default => sub { 0 }
  29. );
  30. has 'session_store' => (
  31. is => 'ro',
  32. default => sub { {} }
  33. );
  34. has 'session_id' => (
  35. is => 'rw',
  36. default => sub { undef },
  37. );
  38. sub BUILD {
  39. my $self = shift;
  40. $self->remote_server_addr('localhost');
  41. $self->port('4444');
  42. }
  43. sub check_status {
  44. return;
  45. }
  46. sub dump_session_store {
  47. my $self = shift;
  48. my ($file,$session_id) = @_;
  49. open (my $fh, '>', $file) or croak "Opening '$file' failed";
  50. my $session_store = $self->session_store;
  51. my $dump = {};
  52. foreach my $path (keys %{$session_store->{$session_id}}) {
  53. $dump->{$path} = $session_store->{$session_id}->{$path};
  54. }
  55. my $json = JSON->new;
  56. $json->allow_blessed;
  57. my $json_session = $json->allow_nonref->utf8->encode($dump);
  58. print $fh $json_session;
  59. close ($fh);
  60. }
  61. sub request {
  62. my $self = shift;
  63. my ($resource, $params) = @_;
  64. my $method = $resource->{method};
  65. my $url = $resource->{url};
  66. my $no_content_success = $resource->{no_content_success} // 0;
  67. my $url_params = $resource->{url_params};
  68. if ( $self->record ) {
  69. my $json = JSON->new;
  70. $json->allow_blessed;
  71. my $response = $self->SUPER::request( $resource, $params, 1 );
  72. if ( ( $response->message ne 'No Content' )
  73. && ( $response->content ne '' ) )
  74. {
  75. if ( $response->content_type =~ m/json/i ) {
  76. my $decoded_json =
  77. $json->allow_nonref(1)->utf8(1)->decode( $response->content );
  78. $self->session_id( $decoded_json->{'sessionId'} )
  79. unless $self->session_id;
  80. }
  81. }
  82. my $content = '';
  83. if (($params) && ($params ne '')) {
  84. $content = $json->allow_nonref->utf8->encode($params);
  85. }
  86. $self->session_store->{$self->session_id}->{"$method $url $content"} = $response if ($self->session_id);
  87. return $self->_process_response($response,$no_content_success);
  88. }
  89. my $mock_cmds = $self->mock_cmds;
  90. my $spec = $self->spec;
  91. my $cmd = $mock_cmds->get_method_name_from_parameters({method => $method,url => $url});
  92. my $ret = {cmd_status => 'OK', cmd_return => 1};
  93. if (defined($spec->{$cmd})) {
  94. my $return_sub = $spec->{$cmd};
  95. if ($no_content_success) {
  96. $ret->{cmd_return} = 1;
  97. }
  98. else {
  99. my $mock_return = $return_sub->($url_params,$params);
  100. if (ref($mock_return) eq 'HASH') {
  101. $ret->{cmd_status} = $mock_return->{status};
  102. $ret->{cmd_return} = $mock_return->{return};
  103. $ret->{cmd_error} = $mock_return->{error} // ''
  104. }
  105. else {
  106. $ret = $mock_return;
  107. }
  108. }
  109. $ret->{session_id} = $self->fake_session_id if (ref($ret) eq 'HASH');
  110. }
  111. else {
  112. $ret->{sessionId} = $self->fake_session_id;
  113. }
  114. return $ret;
  115. }
  116. 1;