File: RemoteConnection.pm

package info (click to toggle)
otrs2 6.0.32-6
  • links: PTS
  • area: non-free
  • in suites: bullseye
  • size: 197,336 kB
  • sloc: perl: 1,003,018; javascript: 75,060; xml: 70,883; php: 51,819; sql: 22,361; sh: 379; makefile: 51
file content (362 lines) | stat: -rw-r--r-- 9,414 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package Selenium::Remote::RemoteConnection;
$Selenium::Remote::RemoteConnection::VERSION = '1.38';
use strict;
use warnings;

#ABSTRACT: Connect to a selenium server

use Moo;
use Try::Tiny;
use LWP::UserAgent;
use HTTP::Headers;
use HTTP::Request;
use Carp qw(croak);
use JSON;
use Data::Dumper;
use Selenium::Remote::ErrorHandler;
use Scalar::Util qw{looks_like_number};

has 'remote_server_addr' => ( is => 'rw', );

has 'port' => ( is => 'rw', );

has 'debug' => (
    is      => 'rw',
    default => sub { 0 }
);

has 'ua' => (
    is      => 'lazy',
    builder => sub { return LWP::UserAgent->new; }
);

has 'error_handler' => (
    is      => 'lazy',
    builder => sub { return Selenium::Remote::ErrorHandler->new; }
);

with 'Selenium::Remote::Driver::CanSetWebdriverContext';


sub check_status {
    my $self = shift;
    my $status;

    try {
        $status = $self->request( { method => 'GET', url => 'status' } );
    }
    catch {
        croak "Could not connect to SeleniumWebDriver: $_";
    };

    my $cmdOut = $status->{cmd_status} || '';
    if ( $cmdOut ne 'OK' ) {

        # Could be grid, see if we can talk to it
        $status = undef;
        $status =
          $self->request( { method => 'GET', url => 'grid/api/hub/status' } );
    }

    unless ( $cmdOut eq 'OK' ) {
        croak "Selenium server did not return proper status";
    }
}


sub request {
    my ( $self, $resource, $params, $dont_process_response ) = @_;
    my $method             = $resource->{method};
    my $url                = $resource->{url};
    my $no_content_success = $resource->{no_content_success} // 0;

    my $content = '';
    my $fullurl = '';

    # Construct full url.
    if ( $url =~ m/^http/g ) {
        $fullurl = $url;
    }
    elsif ( $url =~ m/^\// ) {

        # This is used when we get a 302 Redirect with a Location header.
        $fullurl =
          "http://" . $self->remote_server_addr . ":" . $self->port . $url;
    }
    elsif ( $url =~ m/grid/g ) {
        $fullurl =
          "http://" . $self->remote_server_addr . ":" . $self->port . "/$url";
    }
    else {
        $fullurl =
            "http://"
          . $self->remote_server_addr . ":"
          . $self->port
          . $self->wd_context_prefix . "/$url";
    }

    if ( ( defined $params ) && $params ne '' ) {

        #WebDriver 3 shims
        if ( $resource->{payload} ) {
            foreach my $key ( keys( %{ $resource->{payload} } ) ) {
                $params->{$key} = $resource->{payload}->{$key};
            }
        }

        my $json = JSON->new;
        $json->allow_blessed;
        $content = $json->allow_nonref->utf8->encode($params);
    }

    print "REQ: $method, $fullurl, $content\n" if $self->debug;

    # HTTP request
    my $header =
      HTTP::Headers->new( Content_Type => 'application/json; charset=utf-8' );
    $header->header( 'Accept' => 'application/json' );
    my $request = HTTP::Request->new( $method, $fullurl, $header, $content );
    my $response = $self->ua->request($request);
    if ($dont_process_response) {
        return $response;
    }
    return $self->_process_response( $response, $no_content_success );
}

sub _process_response {
    my ( $self, $response, $no_content_success ) = @_;
    my $data;    # server response 'value' that'll be returned to the user
    my $json = JSON->new;

    if ( $response->is_redirect ) {
        my $redirect = {
            method => 'GET',
            url    => $response->header('location')
        };
        return $self->request($redirect);
    }
    else {
        my $decoded_json = undef;
        print "RES: " . $response->decoded_content . "\n\n" if $self->debug;

        if (   ( $response->message ne 'No Content' )
            && ( $response->content ne '' ) )
        {
            if ( $response->content_type !~ m/json/i ) {
                $data->{'cmd_status'} = 'NOTOK';
                $data->{'cmd_return'}->{message} =
                    'Server returned error message '
                  . $response->content
                  . ' instead of data';
                return $data;
            }
            $decoded_json =
              $json->allow_nonref(1)->utf8(1)->decode( $response->content );
            $data->{'sessionId'} = $decoded_json->{'sessionId'};
        }

        if ( $response->is_error ) {
            $data->{'cmd_status'} = 'NOTOK';
            if ( defined $decoded_json ) {
                $data->{'cmd_return'} =
                  $self->error_handler->process_error($decoded_json);
            }
            else {
                $data->{'cmd_return'} =
                    'Server returned error code '
                  . $response->code
                  . ' and no data';
            }
            return $data;
        }
        elsif ( $response->is_success ) {
            $data->{'cmd_status'} = 'OK';
            if ( defined $decoded_json ) {

                #XXX MS edge doesn't follow spec here either
                if (   looks_like_number( $decoded_json->{status} )
                    && $decoded_json->{status} > 0
                    && $decoded_json->{value}{message} )
                {
                    $data->{cmd_status} = 'NOT OK';
                    $data->{cmd_return} = $decoded_json->{value};
                    return $data;
                }

                #XXX shockingly, neither does InternetExplorerDriver
                if ( ref $decoded_json eq 'HASH' && $decoded_json->{error} ) {
                    $data->{cmd_status} = 'NOT OK';
                    $data->{cmd_return} = $decoded_json;
                    return $data;
                }

                if ($no_content_success) {
                    $data->{'cmd_return'} = 1;
                }
                else {
                    $data->{'cmd_return'} = $decoded_json->{'value'};
                    if ( ref( $data->{cmd_return} ) eq 'HASH'
                        && exists $data->{cmd_return}->{sessionId} )
                    {
                        $data->{sessionId} = $data->{cmd_return}->{sessionId};
                    }
                }
            }
            else {
                $data->{'cmd_return'} =
                    'Server returned status code '
                  . $response->code
                  . ' but no data';
            }
            return $data;
        }
        else {
            # No idea what the server is telling me, must be high
            $data->{'cmd_status'} = 'NOTOK';
            $data->{'cmd_return'} =
                'Server returned status code '
              . $response->code
              . ' which I don\'t understand';
            return $data;
        }
    }
}

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

Selenium::Remote::RemoteConnection - Connect to a selenium server

=head1 VERSION

version 1.38

=head1 SYNOPSIS

    my $driver = Selenium::Remote::Driver->new();
    eval { $driver->remote_conn->check_status() };
    die "do something to kick the server" if $@;

=head1 DESCRIPTION

You shouldn't really need to use this module unless debugging or checking connections when testing dangerous things.

=head1 CONSTRUCTOR

=head2 new(%parameters)

Accepts 5 parameters:

=over 4

=item B<remote_server_addr> - address of selenium server

=item B<port> - port of selenium server

=item B<ua> - Useful to override with Test::LWP::UserAgent in unit tests

=item B<debug> - Should be self-explanatory

=item B<error_handler> - Defaults to Selenium::Remote::ErrorHandler.

=back

These can be set any time later by getter/setters with the same name.

=head1 METHODS

=head2 check_status

Croaks unless the selenium server is responsive.  Sometimes is useful to call in-between tests (the server CAN die on you...)

=head2 request

Make a request of the Selenium server.  Mostly useful for debugging things going wrong with Selenium::Remote::Driver when not in normal operation.

=head1 SEE ALSO

Please see those modules/websites for more information related to this module.

=over 4

=item *

L<Selenium::Remote::Driver|Selenium::Remote::Driver>

=back

=head1 BUGS

Please report any bugs or feature requests on the bugtracker website
L<https://github.com/teodesian/Selenium-Remote-Driver/issues>

When submitting a bug or request, please include a test-file or a
patch to an existing test-file that illustrates the bug or desired
feature.

=head1 AUTHORS

Current Maintainers:

=over 4

=item *

Daniel Gempesaw <gempesaw@gmail.com>

=item *

Emmanuel Peroumalnaïk <peroumalnaik.emmanuel@gmail.com>

=back

Previous maintainers:

=over 4

=item *

Luke Closs <cpan@5thplane.com>

=item *

Mark Stosberg <mark@stosberg.com>

=back

Original authors:

=over 4

=item *

Aditya Ivaturi <ivaturi@gmail.com>

=back

=head1 COPYRIGHT AND LICENSE

Copyright (c) 2010-2011 Aditya Ivaturi, Gordon Child

Copyright (c) 2014-2017 Daniel Gempesaw

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

=cut