Răsfoiți Sursa

Fix #253: stop find_no_element_ok invoking error handler

`find_no_element_ok` should not call the error handler if no element is
found. The previous implementation used `find_element`, which _does_
invoke the error handler when no element is found. Now, we use
`find_elements` and assert a 0-length response array. `find_elements`
does not invoke the error handler.
Daniel Gempesaw 9 ani în urmă
părinte
comite
1cf47c40fe

+ 16 - 1
lib/Test/Selenium/Remote/Role/DoesTesting.pm

@@ -83,10 +83,25 @@ sub _check_ok {
         # quick hack to fit 'find_no_element' into check_ok logic
         if ($method eq 'find_no_element') { 
             $real_method = $method;
-            $method = 'find_element'; 
+
+            # If we use `find_element` and find nothing, the error
+            # handler is incorrectly invoked. Doing a `find_elements`
+            # and checking that it returns an empty array does not
+            # invoke the error_handler. See
+            # https://github.com/gempesaw/Selenium-Remote-Driver/issues/253
+            $method = 'find_elements';
+            my $elements = $self->$method(@r_args);
+            if (scalar(@$elements)) {
+                $rv = $elements->[0];
+            }
+            else {
+                $rv = 1;
+        }
         }
+        else {
         $rv = $self->$method(@r_args);
     }
+    }
     catch {
         if ($real_method) {
             $method = $real_method;

+ 7 - 2
t/Test-Selenium-Remote-Driver.t

@@ -109,9 +109,14 @@ $successful_driver->find_child_element_ok({id => 1},'p','class','find_child_elem
 ok( exception { $successful_driver->find_child_element_ok({id => 1200}) }, 'find_child_element_ok dies if the element is not found' );
 
 # find no element ok test
-
 $successful_driver->find_no_element_ok('notq','xpath','find_no_element_ok works');
-ok(exception { $successful_driver->find_no_element_ok('q','xpath','find_no_element_ok works') }, 'find no element dies when an element is found');
+ok(exception { $successful_driver->find_no_element_ok('abc','xpath','find_no_element_ok works') }, 'find no element dies when an element is found');
+
+my $called = 0;
+$successful_driver->error_handler(sub { my ($self,$msg) = @_; $called++; croak});
+$successful_driver->find_no_element_ok('notq','xpath');
+is($called, 0, 'find_no_element_ok does not call error handler when finding no element');
+$successful_driver->clear_error_handler;
 
 # body and content function family
 $successful_driver->content_like( qr/matches/, 'content_like works');