浏览代码

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 年之前
父节点
当前提交
0ee6f2b3bd
共有 2 个文件被更改,包括 10 次插入18 次删除
  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
 Selenium::Remote::Driver - Perl Client for Selenium Remote Driver
 
 
-=cut
-
 =head1 SYNOPSIS
 =head1 SYNOPSIS
 
 
     use Selenium::Remote::Driver;
     use Selenium::Remote::Driver;
@@ -38,8 +36,6 @@ Selenium::Remote::Driver - Perl Client for Selenium Remote Driver
     print $driver->get_title();
     print $driver->get_title();
     $driver->quit();
     $driver->quit();
 
 
-=cut
-
 =head1 DESCRIPTION
 =head1 DESCRIPTION
 
 
 Selenium is a test tool that allows you to write
 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
 browser. To use this module, you need to have already downloaded and started
 the Selenium Server (Selenium Server is a Java application).
 the Selenium Server (Selenium Server is a Java application).
 
 
-=cut
-
 =head1 USAGE (read this first)
 =head1 USAGE (read this first)
 
 
 =head2 Remote Driver Response
 =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
 don't have to instantiate WebElement objects at all - they will be automatically
 created when you use the find_* methods.
 created when you use the find_* methods.
 
 
-=cut
-
 =head1 FUNCTIONS
 =head1 FUNCTIONS
 
 
-=cut
-
 =head2 new
 =head2 new
 
 
  Description:
  Description:
@@ -154,7 +144,7 @@ created when you use the find_* methods.
                                               );
                                               );
     or
     or
     my $driver = Selenium::Remote::Driver->new('proxy' => {'proxyType' => 'manual', 'httpProxy' => 'myproxy.com:1234'});
     my $driver = Selenium::Remote::Driver->new('proxy' => {'proxyType' => 'manual', 'httpProxy' => 'myproxy.com:1234'});
-    
+
 =cut
 =cut
 
 
 sub new {
 sub new {
@@ -244,13 +234,13 @@ sub _execute_command {
         my $resp = $self->{remote_conn}
         my $resp = $self->{remote_conn}
           ->request( $resource->{'method'}, $resource->{'url'}, $params );
           ->request( $resource->{'method'}, $resource->{'url'}, $params );
         if(ref($resp) eq 'HASH') {
         if(ref($resp) eq 'HASH') {
-            if($resp->{cmd_status} eq 'OK') {
+            if($resp->{cmd_status} && $resp->{cmd_status} eq 'OK') {
                return $resp->{cmd_return};
                return $resp->{cmd_return};
             } else {
             } else {
                my $msg = "Error while executing command";
                my $msg = "Error while executing command";
                if($resp->{cmd_error}) {
                if($resp->{cmd_error}) {
                  $msg .= ": $resp->{cmd_error}" if $resp->{cmd_error};
                  $msg .= ": $resp->{cmd_error}" if $resp->{cmd_error};
-               } else {
+               } elsif ($resp->{cmd_return}) {
                    if(ref($resp->{cmd_return}) eq 'HASH') {
                    if(ref($resp->{cmd_return}) eq 'HASH') {
                      $msg .= ": $resp->{cmd_return}->{error}->{msg}"
                      $msg .= ": $resp->{cmd_return}->{error}->{msg}"
                        if $resp->{cmd_return}->{error}->{msg};
                        if $resp->{cmd_return}->{error}->{msg};
@@ -294,7 +284,9 @@ sub new_session {
         $self->{session_id} = $resp->{'sessionId'};
         $self->{session_id} = $resp->{'sessionId'};
     }
     }
     else {
     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;
         my $decoded_json = undef;
         print "RES: ".$response->decoded_content."\n\n" if $self->{debug};
         print "RES: ".$response->decoded_content."\n\n" if $self->{debug};
         if (($response->message ne 'No Content') && ($response->content ne '')) {
         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);
             $decoded_json = $json->allow_nonref(1)->utf8(1)->decode($response->content);
             $data->{'sessionId'} = $decoded_json->{'sessionId'};
             $data->{'sessionId'} = $decoded_json->{'sessionId'};
         }
         }