Sfoglia il codice sorgente

- moved _get_command_result to RemoteConnection as it belongs there and the request operation is self contained. i.e. when a request method is called now, it'll process the data & return the appropriate result back.
- process_error will return a hash, which is more descriptive.
- Removed a bunch of croaks, which were place holders. From now on a Driver method will return an appropriate error & not die, unless it is fatal.

Aditya Ivaturi 15 anni fa
parent
commit
644d446f5a

+ 40 - 28
lib/Selenium/Remote/Driver.pm

@@ -79,24 +79,6 @@ sub new {
     return $self;
 }
 
-# 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, @args) = @_;
-    my $resp = $self->{remote_conn}->request(@args);
-    if (defined $resp->{'status'} && $resp->{'status'} != 0) {
-        $self->{error_handler}->process_error($resp);
-    }
-    elsif (defined $resp->{'value'}) {
-        return $resp->{'value'};
-    }
-    else {
-        # If there is no value or status assume success
-        return 1;
-    }
-}
-
 sub new_session {
     my $self = shift;
     my $args = { 'desiredCapabilities' => {
@@ -127,7 +109,7 @@ sub get_capabilities {
     my $data = $self->{commands}->getParams($command, $args);
     
     if ($data) {
-        return $self->_get_command_result($data->{'method'}, $data->{'url'});
+        return $self->{remote_conn}->request($data->{'method'}, $data->{'url'});
     }
     else {
         croak "Couldn't retrieve command $command settings\n";
@@ -141,7 +123,7 @@ sub quit {
     my $data = $self->{commands}->getParams('quit', $args);
 
     if ($data) {
-        $self->{remote_conn}->request($data->{'method'}, $data->{'url'});
+        return $self->{remote_conn}->request($data->{'method'}, $data->{'url'});
     }
     else {
         croak "Couldn't retrieve command settings properly\n";
@@ -155,7 +137,7 @@ sub get_current_window_handle {
     my $data = $self->{commands}->getParams($command, $args);
     
     if ($data) {
-        return $self->_get_command_result($data->{'method'}, $data->{'url'});
+        return $self->{remote_conn}->request($data->{'method'}, $data->{'url'});
     }
     else {
         croak "Couldn't retrieve command $command settings\n";
@@ -169,7 +151,7 @@ sub get_window_handles {
     my $data = $self->{commands}->getParams($command, $args);
     
     if ($data) {
-        return $self->_get_command_result($data->{'method'}, $data->{'url'});
+        return $self->{remote_conn}->request($data->{'method'}, $data->{'url'});
     }
     else {
         croak "Couldn't retrieve command $command settings\n";
@@ -183,7 +165,7 @@ sub get_current_url {
     my $data = $self->{commands}->getParams($command, $args);
     
     if ($data) {
-        return $self->_get_command_result($data->{'method'}, $data->{'url'});
+        return $self->{remote_conn}->request($data->{'method'}, $data->{'url'});
     }
     else {
         croak "Couldn't retrieve command $command settings\n";
@@ -203,7 +185,7 @@ sub get {
     my $params = {'url' => $url};
 
     if ($data) {
-        $self->{remote_conn}->request($data->{'method'}, $data->{'url'}, $params);
+        return $self->{remote_conn}->request($data->{'method'}, $data->{'url'}, $params);
     }
     else {
         croak "Couldn't retrieve command $command settings\n";
@@ -217,7 +199,7 @@ sub get_title {
     my $data    = $self->{commands}->getParams($command, $args);
 
     if ($data) {
-        return $self->_get_command_result($data->{'method'}, $data->{'url'});
+        return $self->{remote_conn}->request($data->{'method'}, $data->{'url'});
     }
     else {
         croak "Couldn't retrieve command $command settings\n";
@@ -231,7 +213,7 @@ sub go_back {
     my $data    = $self->{commands}->getParams($command, $args);
 
     if ($data) {
-        $self->{remote_conn}->request($data->{'method'}, $data->{'url'});
+        return $self->{remote_conn}->request($data->{'method'}, $data->{'url'});
     }
     else {
         croak "Couldn't retrieve command $command settings\n";
@@ -245,7 +227,7 @@ sub go_forward {
     my $data    = $self->{commands}->getParams($command, $args);
 
     if ($data) {
-        $self->{remote_conn}->request($data->{'method'}, $data->{'url'});
+        return $self->{remote_conn}->request($data->{'method'}, $data->{'url'});
     }
     else {
         croak "Couldn't retrieve command $command settings\n";
@@ -259,13 +241,43 @@ sub refresh {
     my $data    = $self->{commands}->getParams($command, $args);
 
     if ($data) {
-        $self->{remote_conn}->request($data->{'method'}, $data->{'url'});
+        return $self->{remote_conn}->request($data->{'method'}, $data->{'url'});
+    }
+    else {
+        croak "Couldn't retrieve command $command settings\n";
+    }
+}
+
+sub execute_script {
+    my ($self, $script, @args)    = @_;
+    if (not defined $script) {
+        return 'No script provided';
+    }
+    my $command = 'executeScript';
+    my $args    = { 'session_id' => $self->{'session_id'}, };
+    my $data    = $self->{commands}->getParams($command, $args);
+
+    if ($data) {
+        return $self->{remote_conn}->request($data->{'method'}, $data->{'url'});
     }
     else {
         croak "Couldn't retrieve command $command settings\n";
     }
 }
 
+sub screenshot {
+    my ($self)    = @_;
+    my $command = 'screenshot';
+    my $args    = { 'session_id' => $self->{'session_id'}, };
+    my $data    = $self->{commands}->getParams($command, $args);
+
+    if ($data) {
+        return $self->{remote_conn}->request($data->{'method'}, $data->{'url'});
+    }
+    else {
+        croak "Couldn't retrieve command $command settings\n";
+    }
+}
 
 
 1;

+ 4 - 3
lib/Selenium/Remote/ErrorHandler.pm

@@ -72,9 +72,10 @@ sub process_error {
     
     # TODO: Handle screen if it sent back with the response.
     
-    croak "ERROR: ".$self->STATUS_CODE->{$resp->{'status'}}->{'code'}."\n".
-          "DETAIL: ".$self->STATUS_CODE->{$resp->{'status'}}->{'msg'}."\n".
-          "RESPONSE: ".Dumper($resp);
+    #croak "ERROR: ".$self->STATUS_CODE->{$resp->{'status'}}->{'code'}."\n".
+    #      "DETAIL: ".$self->STATUS_CODE->{$resp->{'status'}}->{'msg'}."\n".
+    #      "RESPONSE: ".Dumper($resp);
+    return $self->STATUS_CODE->{$resp->{'status'}};
 }
 
 1;

+ 38 - 13
lib/Selenium/Remote/RemoteConnection.pm

@@ -12,6 +12,8 @@ use JSON;
 use Error;
 use Data::Dumper;
 
+use Selenium::Remote::ErrorHandler;
+
 sub new {
     my ($class, $remote_srvr, $port) = @_;
     
@@ -27,7 +29,7 @@ sub new {
     croak "Selenium RC server is not responding\n"
             unless $p->ping($self->{'remote_server_addr'});
     undef($p);
-
+    
     return $self;
 }
 
@@ -71,27 +73,50 @@ sub request {
 sub _process_response {
     my ($self, $response) = @_;
     my $data;    #returned data from server
+     my $json = new JSON;
 
     if ($response->is_redirect) {
         return $self->request('GET', $response->header('location'));
     }
-    elsif (($response->is_success) && ($response->code == 200)) {
-        $data = from_json($response->content);
-        if ($data->{'status'} != 0) {
-            croak "Error occurred in server while processing request: $data";
+    elsif ($response->code == 404) {
+        return "Command not implemented on the RC server.";
+    }
+    elsif ($response->code > 199) {
+        my $decoded_json; 
+        if ($response->message ne 'No Content') {
+            $decoded_json = $json->allow_nonref->utf8->decode($response->content);
         }
-        return $data;
+        return $self->_get_command_result($decoded_json);
     }
-    elsif (   ($response->is_success)
-           && (($response->code == 200) || ($response->code == 204))) {
-
-        # Nothing to do.
+    else {
+        return "Unrecognized server status = " . $response->code;
     }
-    elsif ($response->code == 404) {
-        croak "No such command.";
+}
+
+# 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);
+            }
+            else {
+                return $resp;
+            }
+        }
+        else
+        {
+            return $resp;
+        }
     }
     else {
-        croak "Remote server error with status = " . $response->code;
+        # If there is no value or status assume success
+        return 1;
     }
 }