Ver código fonte

#179 totally fixed, took care of 'type_elements_ok'

Emmanuel Peroumalnaik 11 anos atrás
pai
commit
a7a8bc8bbc

+ 31 - 8
lib/Test/Selenium/Remote/Driver.pm

@@ -240,24 +240,47 @@ more documentation, see the related test methods in L<Selenium::Remote::Driver>
     click_ok
     double_click_ok
 
-=head2 $twd->type_element_ok($search_target, $keys, [, $desc ]);
+=head2 $twd->type_element_ok($search_target [,$locator], $keys, [, $desc ]);
 
-   $twd->type_element_ok( $search_target, $keys [, $desc ] );
+   $twd->type_element_ok( $search_target [,$locator], $keys [, $desc ] );
 
 Use L<Selenium::Remote::Driver/find_element> to resolve the C<$search_target>
-to a web element, and then type C<$keys> into it, providing an optional test
+to a web element and an optional locator, and then type C<$keys> into it, providing an optional test
 label.
 
-Currently, other finders besides the default are not supported for C<type_ok()>.
 
 =cut
 
 sub type_element_ok {
     my $self    = shift;
-    my $locator = shift;
-    my $keys    = shift;
-    my $desc    = shift;
-    return $self->find_element($locator)->send_keys_ok( $keys, $desc );
+    my ($locator,$locator_strategy,$keys,$desc) = @_;
+    # case 4 args 
+    if ($desc) { 
+        $self->croak('Invalid locator strategy') unless ($self->FINDERS->{$locator_strategy});
+    }
+    else { 
+        if ($keys) { 
+            # means that we called it the 'old way' (no locator strategy)
+            if (!$self->FINDERS->{$locator_strategy}) { 
+                $desc = $keys; 
+                $keys = $locator_strategy; 
+                $locator_strategy = $self->default_finder;
+            }
+        }
+        else { 
+            # means it was called with no locator strategy and no desc 
+            if ($locator_strategy) { 
+                if (!$self->FINDERS->{$locator_strategy}) { 
+                    $keys = $locator_strategy; 
+                    $locator_strategy = $self->default_finder;
+                }
+            }
+            else { 
+                $self->croak('Not enough arguments');
+            }
+        }
+    }
+    return $self->find_element($locator,$locator_strategy)->send_keys_ok( $keys, $desc );
 }
 
 

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

@@ -40,7 +40,6 @@ sub _check_method {
 
 # main method for _ok tests
 # a bit hacked so that find_no_element_ok can also be processed
-# TODO: maybe simplify a bit the logic 
 
 sub _check_ok {
     my $self   = shift;
@@ -69,6 +68,7 @@ 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'; 

+ 12 - 0
t/Test-Selenium-Remote-Driver.t

@@ -5,6 +5,8 @@ use Test::Selenium::Remote::Driver;
 use Selenium::Remote::WebElement;
 use Selenium::Remote::Mock::Commands;
 use Selenium::Remote::Mock::RemoteConnection;
+use DDP; 
+
 my $find_element = sub {
     my ( undef, $searched_item ) = @_;
     if ( $searched_item->{value} eq 'q' ) {
@@ -47,12 +49,20 @@ my $find_elements = sub {
     }
 };
 
+my $send_keys = sub {
+        my ( $session_object, $val ) = @_;
+        my $keys = shift @{ $val->{value} };
+        return { status => 'OK', return => 1 } if ( $keys =~ /abc|def/ );
+        return { status => 'NOK', return => 0, error => 'cannot send keys' };
+      };
+
 my $spec = {
     findElement => $find_element,
     findChildElement => $find_child_element,
     getPageSource => sub { return 'this output matches regex'},
     findElements => $find_elements,
     findChildElements => $find_child_element,
+    sendKeysToElement => $send_keys,
 };
 
 my $mock_commands = Selenium::Remote::Mock::Commands->new;
@@ -72,5 +82,7 @@ $successful_driver->content_like( qr/matches/, 'content_like works');
 $successful_driver->content_unlike( qr/nomatch/, 'content_unlike works');
 $successful_driver->find_elements_ok('abc','find_elements_ok works');
 $successful_driver->find_child_elements_ok({id => 1},'p','find_child_elements_ok works');
+$successful_driver->type_element_ok('q','abc');
+$successful_driver->type_element_ok('p','class','def','type_element_ok works with a locator');
 
 done_testing();