Browse Source

#179 Covered the functions using find_element + _ok on an S::R::D::Element

Emmanuel Peroumalnaik 11 years ago
parent
commit
a0a26045d8

+ 103 - 81
lib/Test/Selenium/Remote/Driver.pm

@@ -251,19 +251,22 @@ label.
 
 
 =cut
 =cut
 
 
-sub type_element_ok {
-    my $self    = shift;
-    my ($locator,$locator_strategy,$keys,$desc) = @_;
+# function composing a find_element with locator with a webelement test
+
+sub _find_element_with_action { 
+    my $self = shift; 
+    my $method = shift;
+    my ($locator,$locator_strategy,$params,$desc) = @_;
     # case 4 args 
     # case 4 args 
     if ($desc) { 
     if ($desc) { 
         $self->croak('Invalid locator strategy') unless ($self->FINDERS->{$locator_strategy});
         $self->croak('Invalid locator strategy') unless ($self->FINDERS->{$locator_strategy});
     }
     }
     else { 
     else { 
-        if ($keys) { 
+        if ($params) { 
             # means that we called it the 'old way' (no locator strategy)
             # means that we called it the 'old way' (no locator strategy)
             if (!$self->FINDERS->{$locator_strategy}) { 
             if (!$self->FINDERS->{$locator_strategy}) { 
-                $desc = $keys; 
-                $keys = $locator_strategy; 
+                $desc = $params; 
+                $params = $locator_strategy; 
                 $locator_strategy = $self->default_finder;
                 $locator_strategy = $self->default_finder;
             }
             }
         }
         }
@@ -271,7 +274,7 @@ sub type_element_ok {
             # means it was called with no locator strategy and no desc 
             # means it was called with no locator strategy and no desc 
             if ($locator_strategy) { 
             if ($locator_strategy) { 
                 if (!$self->FINDERS->{$locator_strategy}) { 
                 if (!$self->FINDERS->{$locator_strategy}) { 
-                    $keys = $locator_strategy; 
+                    $params = $locator_strategy; 
                     $locator_strategy = $self->default_finder;
                     $locator_strategy = $self->default_finder;
                 }
                 }
             }
             }
@@ -280,7 +283,99 @@ sub type_element_ok {
             }
             }
         }
         }
     }
     }
-    return $self->find_element($locator,$locator_strategy)->send_keys_ok( $keys, $desc );
+    unless ($desc) {
+        $desc = $method;
+        $desc .= "'" . join( " ", ($params // '') ) . "'";
+    }
+    return $self->find_element($locator,$locator_strategy)->$method( $params, $desc );
+}
+
+
+sub type_element_ok {
+    my $self    = shift;
+    my $method = 'send_keys_ok'; 
+    return $self->_find_element_with_action($method,@_);
+}
+
+
+=head2 $twd->element_text_is($search_target[,$finder],$expected_text [,$desc]);
+
+    $twd->element_text_is($search_target[,$finder],$expected_text [,$desc]);
+
+=cut
+
+sub element_text_is {
+    my $self = shift; 
+    my $method = 'text_is';
+    return $self->_find_element_with_action($method,@_);
+}
+
+=head2 $twd->element_value_is($search_target[,$finder],$expected_value [,$desc]);
+
+    $twd->element_value_is($search_target[,$finder],$expected_value [,$desc]);
+
+=cut
+
+sub element_value_is {
+    my $self = shift; 
+    my $method = 'value_is';
+    return $self->_find_element_with_action($method,@_);
+}
+
+=head2 $twd->click_element_ok($search_target [,$desc]);
+
+    $twd->click_element_ok($search_target [,$desc]);
+
+Find an element and then click on it.
+
+=cut
+
+sub click_element_ok {
+    my $self = shift; 
+    my $method = 'click_ok';
+    return $self->_find_element_with_action($method,@_);
+}
+
+=head2 $twd->clear_element_ok($search_target [,$desc]);
+
+    $twd->clear_element_ok($search_target [,$desc]);
+
+Find an element and then clear on it.
+
+=cut
+
+sub clear_element_ok {
+    my $self = shift; 
+    my $method = 'clear_ok';
+    return $self->_find_element_with_action($method,@_);
+}
+
+=head2 $twd->is_element_displayed_ok($search_target [,$desc]);
+
+    $twd->is_element_displayed_ok($search_target [,$desc]);
+
+Find an element and check to confirm that it is displayed. (visible)
+
+=cut
+
+sub is_element_displayed_ok {
+    my $self = shift; 
+    my $method = 'is_displayed_ok';
+    return $self->_find_element_with_action($method,@_);
+}
+
+=head2 $twd->is_element_enabled_ok($search_target [,$desc]);
+
+    $twd->is_element_enabled_ok($search_target [,$desc]);
+
+Find an element and check to confirm that it is enabled.
+
+=cut
+
+sub is_element_enabled_ok {
+    my $self = shift; 
+    my $method = 'is_enabled_ok';
+    return $self->_find_element_with_action($method,@_);
 }
 }
 
 
 
 
@@ -603,79 +698,6 @@ sub body_text_lacks {
     }
     }
 }
 }
 
 
-=head2 $twd->element_text_is($search_target,$expected_text [,$desc]);
-
-    $twd->element_text_is($search_target,$expected_text [,$desc]);
-
-=cut
-
-sub element_text_is {
-    my ( $self, $search_target, $expected, $desc ) = @_;
-    return $self->find_element($search_target)->text_is( $expected, $desc );
-}
-
-=head2 $twd->element_value_is($search_target,$expected_value [,$desc]);
-
-    $twd->element_value_is($search_target,$expected_value [,$desc]);
-
-=cut
-
-sub element_value_is {
-    my ( $self, $search_target, $expected, $desc ) = @_;
-    return $self->find_element($search_target)->value_is( $expected, $desc );
-}
-
-=head2 $twd->click_element_ok($search_target [,$desc]);
-
-    $twd->click_element_ok($search_target [,$desc]);
-
-Find an element and then click on it.
-
-=cut
-
-sub click_element_ok {
-    my ( $self, $search_target, $desc ) = @_;
-    return $self->find_element($search_target)->click_ok($desc);
-}
-
-=head2 $twd->clear_element_ok($search_target [,$desc]);
-
-    $twd->clear_element_ok($search_target [,$desc]);
-
-Find an element and then clear on it.
-
-=cut
-
-sub clear_element_ok {
-    my ( $self, $search_target, $desc ) = @_;
-    return $self->find_element($search_target)->clear_ok($desc);
-}
-
-=head2 $twd->is_element_displayed_ok($search_target [,$desc]);
-
-    $twd->is_element_displayed_ok($search_target [,$desc]);
-
-Find an element and check to confirm that it is displayed. (visible)
-
-=cut
-
-sub is_element_displayed_ok {
-    my ( $self, $search_target, $desc ) = @_;
-    return $self->find_element($search_target)->is_displayed_ok($desc);
-}
-
-=head2 $twd->is_element_enabled_ok($search_target [,$desc]);
-
-    $twd->is_element_enabled_ok($search_target [,$desc]);
-
-Find an element and check to confirm that it is enabled.
-
-=cut
-
-sub is_element_enabled_ok {
-    my ( $self, $search_target, $desc ) = @_;
-    return $self->find_element($search_target)->is_enabled_ok($desc);
-}
 
 
 1;
 1;
 
 

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

@@ -27,6 +27,7 @@ sub _check_method {
     my @args = @_;
     my @args = @_;
     my $rv;
     my $rv;
     try {
     try {
+        $DB::single = 1;
         my $num_of_args = $self->has_args($method);
         my $num_of_args = $self->has_args($method);
         my @r_args = splice( @args, 0, $num_of_args );
         my @r_args = splice( @args, 0, $num_of_args );
         $rv = $self->$method(@r_args);
         $rv = $self->$method(@r_args);

+ 47 - 9
t/Test-Selenium-Remote-Driver.t

@@ -15,7 +15,7 @@ my $find_element = sub {
     if (   $searched_item->{value} eq 'p'
     if (   $searched_item->{value} eq 'p'
         && $searched_item->{using} eq 'class name' )
         && $searched_item->{using} eq 'class name' )
     {
     {
-        return { status => 'OK', return => { ELEMENT => '123456' } };
+        return { status => 'OK', return => { ELEMENT => '123457' } };
     }
     }
     return { status => 'NOK', return => 0, error => 'element not found' };
     return { status => 'NOK', return => 0, error => 'element not found' };
 };
 };
@@ -50,11 +50,23 @@ my $find_elements = sub {
 };
 };
 
 
 my $send_keys = 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 ( $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 $get_text = sub { 
+    my ($session_object) =@_; 
+    return 'abc' if ($session_object->{id} eq '123456');
+    return 'def' if ($session_object->{id} eq '123457');
+    return;
+};
+
+my $get_attr = sub { 
+    my ($session_object) = @_;
+    return 'foo';
+};
 
 
 my $spec = {
 my $spec = {
     findElement => $find_element,
     findElement => $find_element,
@@ -62,7 +74,13 @@ my $spec = {
     getPageSource => sub { return 'this output matches regex'},
     getPageSource => sub { return 'this output matches regex'},
     findElements => $find_elements,
     findElements => $find_elements,
     findChildElements => $find_child_element,
     findChildElements => $find_child_element,
+    getElementText => $get_text,
     sendKeysToElement => $send_keys,
     sendKeysToElement => $send_keys,
+    getElementAttribute => $get_attr, 
+    clickElement => sub { return { status => 'OK', return => 1 }; }, 
+    clearElement =>  sub { return { status => 'OK', return => 1 }; }, 
+    isElementDisplayed =>  sub { return { status => 'OK', return => 1 }; }, 
+    isElementEnabled =>  sub { return { status => 'OK', return => 1 }; }, 
 };
 };
 
 
 my $mock_commands = Selenium::Remote::Mock::Commands->new;
 my $mock_commands = Selenium::Remote::Mock::Commands->new;
@@ -72,17 +90,37 @@ my $successful_driver =
     remote_conn => Selenium::Remote::Mock::RemoteConnection->new( spec => $spec, mock_cmds => $mock_commands ),
     remote_conn => Selenium::Remote::Mock::RemoteConnection->new( spec => $spec, mock_cmds => $mock_commands ),
     commands => $mock_commands,
     commands => $mock_commands,
 );
 );
+
+# find element ok tests 
 $successful_driver->find_element_ok('q','find_element_ok works');
 $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_element_ok('p','class','find_element_ok with a locator works');
+dies_ok { $successful_driver->find_element_ok('notq') } 'find_element_ok dies if element not found';
+$successful_driver->find_elements_ok('abc','find_elements_ok works');
+
+# find child element ok tests 
+$successful_driver->find_child_elements_ok({id => 1},'p','find_child_elements_ok works');
 $successful_driver->find_child_element_ok({id => 1},'p','class','find_child_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_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';
+
+# find no element ok test
+
 $successful_driver->find_no_element_ok('notq','xpath','find_no_element_ok works');
 $successful_driver->find_no_element_ok('notq','xpath','find_no_element_ok works');
+
+
 $successful_driver->content_like( qr/matches/, 'content_like works');
 $successful_driver->content_like( qr/matches/, 'content_like works');
 $successful_driver->content_unlike( qr/nomatch/, 'content_unlike 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('q','abc');
 $successful_driver->type_element_ok('p','class','def','type_element_ok works with a locator');
 $successful_driver->type_element_ok('p','class','def','type_element_ok works with a locator');
 
 
+$successful_driver->element_text_is('q','abc','element has a correct text');
+$successful_driver->element_text_is('p','class','def');
+
+$successful_driver->element_value_is('p','class','foo');
+$successful_driver->click_element_ok('p','class','click_element_ok works');
+$successful_driver->clear_element_ok('q','element is cleared ok');
+$successful_driver->is_element_enabled_ok('p','class','element is enabled');
+$successful_driver->is_element_displayed_ok('q','element is displayed');
+
+
 done_testing();
 done_testing();