Explorar o código

Fixed things up for find_child_elements

Emmanuel Peroumalnaik %!s(int64=11) %!d(string=hai) anos
pai
achega
8c427929d2

+ 3 - 0
lib/Test/Selenium/Remote/Driver.pm

@@ -44,6 +44,9 @@ sub has_args {
     my $self          = shift;
     my $fun_name      = shift;
     my $hash_fun_args = {
+        'find_element'     => 2,
+        'find_child_element'     => 3,
+        'find_child_elements'     => 3,
         'find_element'     => 2,
         'find_elements'     => 2,
         'compare_elements' => 2,

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

@@ -49,7 +49,7 @@ sub _check_ok {
         $num_of_args = $self->has_args($method);
         @r_args = splice( @args, 0, $num_of_args );
         if ($method =~ m/^find_element/) { 
-            # case find_element*_ok was called with no arguments    
+            # case find_element_ok was called with no arguments    
             if (scalar(@r_args) == 1) { 
                 push @r_args, $self->default_finder; 
             }
@@ -66,6 +66,24 @@ sub _check_ok {
                 }
             }
         }
+        if ($method =~ m/^find_child_element/) { 
+            # case find_element_ok was called with no arguments    
+            if (scalar(@r_args) == 2) { 
+                push @r_args, $self->default_finder; 
+            }
+            else { 
+                if (scalar(@r_args) == 3) { 
+                    # case find_element was called with no finder but
+                    # a test description
+                    my $finder = $r_args[2]; 
+                    my @FINDERS = keys (%{$self->FINDERS});
+                    unless ( any { $finder eq $_ } @FINDERS) { 
+                        $r_args[2] = $self->default_finder; 
+                        push @args, $finder; 
+                    }
+                }
+            }
+        }
         $rv = $self->$method(@r_args);
     }
     catch {

+ 49 - 18
t/Test-Selenium-Remote-Driver.t

@@ -5,28 +5,56 @@ use Test::Selenium::Remote::Driver;
 use Selenium::Remote::WebElement;
 use Selenium::Remote::Mock::Commands;
 use Selenium::Remote::Mock::RemoteConnection;
-
-my $spec = {
-    findElement => sub {
-        my ( undef, $searched_item ) = @_;
-        if ( $searched_item->{value} eq 'q' ) {
-            return { status => 'OK', return => { ELEMENT => '123456' } };
+my $find_element = sub {
+    my ( undef, $searched_item ) = @_;
+    if ( $searched_item->{value} eq 'q' ) {
+        return { status => 'OK', return => { ELEMENT => '123456' } };
+    }
+    if (   $searched_item->{value} eq 'p'
+        && $searched_item->{using} eq 'class name' )
+    {
+        return { status => 'OK', return => { ELEMENT => '123456' } };
+    }
+    return { status => 'NOK', return => 0, error => 'element not found' };
+};
+my $find_child_element = sub {
+    my ( $session_object, $searched_item ) = @_;
+    my $elem_id = $session_object->{id};
+    if ( $elem_id == 1 && $searched_item->{value} eq 'p' ) {
+        if ( $searched_item->{using} eq 'class name' ) {
+            return { status => 'OK', return => { ELEMENT => '11223344' } };
         }
-        if (   $searched_item->{value} eq 'p'
-            && $searched_item->{using} eq 'class name' )
-        {
-            return { status => 'OK', return => { ELEMENT => '123456' } };
+
+        if ( $searched_item->{using} eq 'xpath' ) {
+            return { status => 'OK',
+                return => [ { ELEMENT => '112' }, { ELEMENT => '113' } ] };
         }
-        return { status => 'NOK', return => 0, error => 'element not found' };
-      },
+    }
+
+    return {
+        status => 'NOK', return => 0,
+        error  => 'child element not found'
+    };
+};
+
+my $find_elements = sub {
+    my ( undef, $searched_expr ) = @_;
+    if (   $searched_expr->{value} eq 'abc'
+        && $searched_expr->{using} eq 'xpath' )
+    {
+        return { status => 'OK',
+            return => [ { ELEMENT => '123456' }, { ELEMENT => '12341234' } ] };
+    }
+};
+
+my $spec = {
+    findElement => $find_element,
+    findChildElement => $find_child_element,
     getPageSource => sub { return 'this output matches regex'},
-    findElements => sub { 
-        my (undef,$searched_expr) = @_;
-        if ($searched_expr->{value} eq 'abc' && $searched_expr->{using} eq 'xpath') { 
-            return { status => 'OK', return => [ { ELEMENT => '123456' }, { ELEMENT => '12341234' } ] }
-        }
-    },
+    findElements => $find_elements,
+    findChildElements => $find_child_element,
 };
+
 my $mock_commands = Selenium::Remote::Mock::Commands->new;
 
 my $successful_driver =
@@ -36,10 +64,13 @@ my $successful_driver =
 );
 $successful_driver->find_element_ok('q','find_element_ok works');
 $successful_driver->find_element_ok('p','class','find_element_ok with a locator works');
+$successful_driver->find_child_element_ok({id => 1},'p','class','find_child_element_ok with a locator works');
+dies_ok{ $successful_driver->find_child_element_ok({id => 1200}) } 'find_child_element_ok dies if the element is not found';
 dies_ok { $successful_driver->find_element_ok('notq') } 'find_element_ok dies if element not found';
 $successful_driver->find_no_element_ok('notq','find_no_element_ok works');
 $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');
 
 done_testing();