Quellcode durchsuchen

Merge branch 'find_element_nice_error'

Gordon Child vor 14 Jahren
Ursprung
Commit
f8c7c0e6e7
2 geänderte Dateien mit 60 neuen und 8 gelöschten Zeilen
  1. 51 6
      lib/Selenium/Remote/Driver.pm
  2. 9 2
      t/01-driver-live.t

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

@@ -4,7 +4,8 @@ use strict;
 use warnings;
 use Data::Dumper;
 
-use Carp qw(croak);
+use Carp;
+our @CARP_NOT;
 
 use Selenium::Remote::RemoteConnection;
 use Selenium::Remote::Commands;
@@ -215,7 +216,7 @@ sub _execute_command {
                  $msg .= ": $resp->{cmd_error}" if $resp->{cmd_error};
                } else {
                    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};
                    } else {
                      $msg .= ": $resp->{cmd_return}";
@@ -1019,7 +1020,18 @@ sub find_element {
     if (defined $using) {
         my $res = { 'command' => 'findElement' };
         my $params = { 'using' => $using, 'value' => $query };
-        my $ret_data = $self->_execute_command( $res, $params );
+        my $ret_data = eval { $self->_execute_command( $res, $params ); };
+        if($@) {
+          if($@ =~ /(An element could not be located on the page using the given search parameters)/) {
+            # give details on what element wasn't found
+            $@ = "$1: $query,$using";
+            local @CARP_NOT = ("Selenium::Remote::Driver",@CARP_NOT);
+            croak $@;
+          } else {
+            # re throw if the exception wasn't what we expected
+            die $@;
+          }
+        }
         return new Selenium::Remote::WebElement($ret_data->{ELEMENT}, $self);
     }
     else {
@@ -1060,7 +1072,18 @@ sub find_elements {
     if (defined $using) {
         my $res = { 'command' => 'findElements' };
         my $params = { 'using' => $using, 'value' => $query };
-        my $ret_data = $self->_execute_command( $res, $params );
+        my $ret_data = eval {$self->_execute_command( $res, $params );};
+         if($@) {
+          if($@ =~ /(An element could not be located on the page using the given search parameters)/) {
+            # give details on what element wasn't found
+            $@ = "$1: $query,$using";
+            local @CARP_NOT = ("Selenium::Remote::Driver",@CARP_NOT);
+            croak $@;
+          } else {
+            # re throw if the exception wasn't what we expected
+            die $@;
+          }
+        }
         my $elem_obj_arr;
         my $i = 0;
         foreach (@$ret_data) {
@@ -1110,7 +1133,18 @@ sub find_child_element {
     if (exists FINDERS->{$using}) {
         my $res = { 'command' => 'findChildElement', 'id' => $elem->{id} };
         my $params = { 'using' => $using, 'value' => $query };
-        my $ret_data = $self->_execute_command( $res, $params );
+        my $ret_data = eval {$self->_execute_command( $res, $params );};
+        if($@) {
+          if($@ =~ /(An element could not be located on the page using the given search parameters)/) {
+            # give details on what element wasn't found
+            $@ = "$1: $query,$using";
+            local @CARP_NOT = ("Selenium::Remote::Driver",@CARP_NOT);
+            croak $@;
+          } else {
+            # re throw if the exception wasn't what we expected
+            die $@;
+          }
+        }
         return new Selenium::Remote::WebElement($ret_data->{ELEMENT}, $self);
     }
     else {
@@ -1155,7 +1189,18 @@ sub find_child_elements {
     if (exists FINDERS->{$using}) {
         my $res = { 'command' => 'findChildElements', 'id' => $elem->{id} };
         my $params = { 'using' => $using, 'value' => $query };
-        my $ret_data = $self->_execute_command( $res, $params );
+        my $ret_data = eval {$self->_execute_command( $res, $params );};
+        if($@) {
+          if($@ =~ /(An element could not be located on the page using the given search parameters)/) {
+            # give details on what element wasn't found
+            $@ = "$1: $query,$using";
+            local @CARP_NOT = ("Selenium::Remote::Driver",@CARP_NOT);
+            croak $@;
+          } else {
+            # re throw if the exception wasn't what we expected
+            die $@;
+          }
+        }
         my $elem_obj_arr;
         my $i = 0;
         foreach (@$ret_data) {

+ 9 - 2
t/01-driver-live.t

@@ -120,7 +120,7 @@ FIND: {
         ok($elem->isa('Selenium::Remote::WebElement'), 'Got WebElement via Id');
         $elem = $driver->find_element('checky', 'name');
         ok($elem->isa('Selenium::Remote::WebElement'), 'Got WebElement via Name');
-        
+
         $elem = $driver->find_element('multi', 'id');
         $elem = $driver->find_child_element($elem, "option");
         ok($elem->isa('Selenium::Remote::WebElement'), 'Got child WebElement...');
@@ -128,6 +128,14 @@ FIND: {
         is($ret, 'Eggs', '...right child WebElement');
         $ret = $driver->find_child_elements($elem, "//option[\@selected='selected']");
         is(@{$ret}, 4, 'Got 4 WebElements');
+        my $expected_err = "An element could not be located on the page using the "
+         . "given search parameters: "
+         . "element_that_doesnt_exist,id"
+        # the following needs to always be right before the eval
+         . " at " . __FILE__ . " line " . (__LINE__+1);
+        eval { $driver->find_element("element_that_doesnt_exist","id"); };
+        chomp $@;
+        is($@,$expected_err,"find_element croaks properly");
       }
 
 EXECUTE: {
@@ -151,7 +159,6 @@ EXECUTE: {
         is($elem->get_attribute('id'),'multi','Async found proper element');
 }
 
-
 QUIT: {
         $ret = $driver->quit();
         ok((not defined $driver->{'session_id'}), 'Killed the remote session');