Răsfoiți Sursa

Made changes to be compatible with the recent JSON wire protocol changes & streamlined how the response is processed & returned to the end user. From now on each method of Driver will always return a hash with cmd_status & cmd_return as keys which will tell whether the command executed OK or not (NOTOK). cmd_result will either contain the value returned from the server or the error explanation & stack trace if included.

Aditya Ivaturi 15 ani în urmă
părinte
comite
dfc09ea822

+ 1 - 1
lib/Selenium/Remote/Commands.pm

@@ -265,7 +265,7 @@ Perl Bindings for Remote Driver by Aditya Ivaturi <ivaturi@gmail.com>
 
 =head1 LICENSE
 
-Copyright (c) 2010 Juniper Networks, Inc
+Copyright (c) 2010 Aditya Ivaturi
 
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.

+ 2 - 1
lib/Selenium/Remote/Driver.pm

@@ -135,8 +135,9 @@ sub _execute_command {
     $res->{'session_id'} = $self->{'session_id'};
     my $resource = $self->{commands}->get_params($res);
     if ($resource) {
-        return $self->{remote_conn}
+        my $resp = $self->{remote_conn}
           ->request( $resource->{'method'}, $resource->{'url'}, $params );
+        return $resp;
     }
     else {
         croak "Couldn't retrieve command settings properly\n";

+ 30 - 43
lib/Selenium/Remote/RemoteConnection.pm

@@ -66,65 +66,52 @@ sub request {
     return $self->_process_response($response);
 }
 
-# Even though there are multiple error codes returned, at this point we care
-# mainly about 404. We should add code to handle specific HTTP Response codes.
 sub _process_response {
     my ($self, $response) = @_;
-    my $data;    #returned data from server
-     my $json = new JSON;
+    my $data; # server response 'value' that'll be returned to the user
+    my $json = new JSON;
 
     if ($response->is_redirect) {
         return $self->request('GET', $response->header('location'));
     }
-    elsif ($response->code == 404) {
-        return "Command not implemented on the RC server.";
-    }
-    elsif ($response->code > 199) {
-        my $decoded_json; 
+    else {
+        my $decoded_json = undef; 
         if ($response->message ne 'No Content') {
-            $decoded_json = $json->allow_nonref->utf8->decode($response->content);
+            $decoded_json = $json->allow_nonref(1)->utf8(1)->decode($response->content);
+            $data->{'sessionId'} = $decoded_json->{'sessionId'};
         }
-        return $self->_get_command_result($decoded_json);
-    }
-    else {
-        return "Unrecognized server status = " . $response->code;
-    }
-}
-
-# When a command is processed by the remote server & a result is sent back, it
-# also includes other relevant info. We strip those & just return the value we're
-# interested in. And if there is an error, ErrorHandler will handle it.
-sub _get_command_result {
-    my ($self, $resp) = @_;
-    my $error_handler = new Selenium::Remote::ErrorHandler;
-    
-    if (defined $resp) {
-        if (ref $resp eq 'HASH') {
-            if (defined $resp->{'status'} && $resp->{'status'} != 0) {
-                return $error_handler->process_error($resp);
+        
+        if ($response->is_error) {
+            my $error_handler = new Selenium::Remote::ErrorHandler;
+            $data->{'cmd_status'} = 'NOTOK';
+            if (defined $decoded_json) {
+                $data->{'cmd_return'} = $error_handler->process_error($decoded_json);
             }
             else {
-                # For new session we need to grab the session id.
-                if ((ref $resp->{'value'} eq 'HASH') &&
-                    ($resp->{'value'}->{'class'} eq 'org.openqa.selenium.remote.DesiredCapabilities')) {
-                        return $resp;
-                }
-                else {
-                    return $resp->{'value'};
-                }
+                $data->{'cmd_return'} = 'Server returned error code '.$response->code.' and no data';          
             }
+            return $data;
         }
-        else
-        {
-            return $resp;
+        elsif ($response->is_success) {
+            $data->{'cmd_status'} = 'OK';
+            if (defined $decoded_json) {
+                $data->{'cmd_return'} = $decoded_json->{'value'};
+            }
+            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;
         }
-    }
-    else {
-        # If there is no value or status assume success
-        return 1;
     }
 }
 
+
 1;
 
 __END__