فهرست منبع

find_element(s)_ok functions are covered

Emmanuel Peroumalnaik 11 سال پیش
والد
کامیت
ecbf21a08c

+ 1 - 0
lib/Selenium/Remote/Driver.pm

@@ -36,6 +36,7 @@ use constant FINDERS => {
     xpath             => 'xpath',
 };
 
+
 =head1 SYNOPSIS
 
     use Selenium::Remote::Driver;

+ 2 - 2
lib/Test/Selenium/Remote/Driver.pm

@@ -44,8 +44,8 @@ sub has_args {
     my $self          = shift;
     my $fun_name      = shift;
     my $hash_fun_args = {
-        'find_element'     => 1,
-        'find_elements'     => 1,
+        'find_element'     => 2,
+        'find_elements'     => 2,
         'compare_elements' => 2,
         'get' => 1,
     };

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

@@ -5,6 +5,7 @@ package Test::Selenium::Remote::Role::DoesTesting;
 use Moo::Role;
 use Test::Builder;
 use Try::Tiny;
+use List::MoreUtils qw/any/;
 use namespace::clean;
 
 requires qw(func_list has_args);
@@ -47,6 +48,24 @@ sub _check_ok {
     try {
         $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    
+            if (scalar(@r_args) == 1) { 
+                push @r_args, $self->default_finder; 
+            }
+            else { 
+                if (scalar(@r_args) == 2) { 
+                    # case find_element was called with no finder but
+                    # a test description
+                    my $finder = $r_args[1]; 
+                    my @FINDERS = keys (%{$self->FINDERS});
+                    unless ( any { $finder eq $_ } @FINDERS) { 
+                        $r_args[1] = $self->default_finder; 
+                        push @args, $finder; 
+                    }
+                }
+            }
+        }
         $rv = $self->$method(@r_args);
     }
     catch {

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

@@ -8,15 +8,21 @@ use Selenium::Remote::Mock::RemoteConnection;
 
 my $spec = {
     findElement => sub {
-        my (undef,$searched_item) = @_;
-        return { status => 'OK', return => { ELEMENT => '123456' } }
-          if ( $searched_item->{value} eq 'q' );
+        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' };
-    },
+      },
     getPageSource => sub { return 'this output matches regex'},
     findElements => sub { 
         my (undef,$searched_expr) = @_;
-        if ($searched_expr) { 
+        if ($searched_expr->{value} eq 'abc' && $searched_expr->{using} eq 'xpath') { 
             return { status => 'OK', return => [ { ELEMENT => '123456' }, { ELEMENT => '12341234' } ] }
         }
     },
@@ -29,6 +35,7 @@ my $successful_driver =
     commands => $mock_commands,
 );
 $successful_driver->find_element_ok('q','find_element_ok 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_no_element_ok('notq','find_no_element_ok works');
 $successful_driver->content_like( qr/matches/, 'content_like works');