Selaa lähdekoodia

Merge pull request #55 from autarch/autarch/handle-non-json-response

mostly changes to make the code a little more robust when handling errors.
Mark Stosberg 12 vuotta sitten
vanhempi
sitoutus
0ee6f2b3bd
2 muutettua tiedostoa jossa 10 lisäystä ja 18 poistoa
  1. 6 14
      lib/Selenium/Remote/Driver.pm
  2. 4 4
      lib/Selenium/Remote/RemoteConnection.pm

+ 6 - 14
lib/Selenium/Remote/Driver.pm

@@ -27,8 +27,6 @@ use constant FINDERS => {
 
 Selenium::Remote::Driver - Perl Client for Selenium Remote Driver
 
-=cut
-
 =head1 SYNOPSIS
 
     use Selenium::Remote::Driver;
@@ -38,8 +36,6 @@ Selenium::Remote::Driver - Perl Client for Selenium Remote Driver
     print $driver->get_title();
     $driver->quit();
 
-=cut
-
 =head1 DESCRIPTION
 
 Selenium is a test tool that allows you to write
@@ -55,8 +51,6 @@ together with the Selenium Server, you can automatically control any supported
 browser. To use this module, you need to have already downloaded and started
 the Selenium Server (Selenium Server is a Java application).
 
-=cut
-
 =head1 USAGE (read this first)
 
 =head2 Remote Driver Response
@@ -87,12 +81,8 @@ your further actions will fail for that element. Finally, just remember that you
 don't have to instantiate WebElement objects at all - they will be automatically
 created when you use the find_* methods.
 
-=cut
-
 =head1 FUNCTIONS
 
-=cut
-
 =head2 new
 
  Description:
@@ -154,7 +144,7 @@ created when you use the find_* methods.
                                               );
     or
     my $driver = Selenium::Remote::Driver->new('proxy' => {'proxyType' => 'manual', 'httpProxy' => 'myproxy.com:1234'});
-    
+
 =cut
 
 sub new {
@@ -244,13 +234,13 @@ sub _execute_command {
         my $resp = $self->{remote_conn}
           ->request( $resource->{'method'}, $resource->{'url'}, $params );
         if(ref($resp) eq 'HASH') {
-            if($resp->{cmd_status} eq 'OK') {
+            if($resp->{cmd_status} && $resp->{cmd_status} eq 'OK') {
                return $resp->{cmd_return};
             } else {
                my $msg = "Error while executing command";
                if($resp->{cmd_error}) {
                  $msg .= ": $resp->{cmd_error}" if $resp->{cmd_error};
-               } else {
+               } elsif ($resp->{cmd_return}) {
                    if(ref($resp->{cmd_return}) eq 'HASH') {
                      $msg .= ": $resp->{cmd_return}->{error}->{msg}"
                        if $resp->{cmd_return}->{error}->{msg};
@@ -294,7 +284,9 @@ sub new_session {
         $self->{session_id} = $resp->{'sessionId'};
     }
     else {
-        croak "Could not create new session";
+        my $error = 'Could not create new session';
+        $error .= ": $resp->{cmd_return}" if defined $resp->{cmd_return};
+        croak $error;
     }
 }
 

+ 4 - 4
lib/Selenium/Remote/RemoteConnection.pm

@@ -92,10 +92,10 @@ sub _process_response {
         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 =~ m/^<html>/i) {
-		$data->{'cmd_return'} = 'Server returned error message '.$response->content.' instead of data';
-		return $data;
-	    }
+            if ($response->content_type !~ m/json/i) {
+                $data->{'cmd_return'} = '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'};
         }