Browse Source

Rewrote the tests of Test::Selenium::Remote::WebElement

Emmanuel Peroumalnaik 11 years ago
parent
commit
67242aba60

+ 18 - 18
lib/Selenium/Remote/WebElement.pm

@@ -47,7 +47,7 @@ has 'driver' => (
 
 
 sub click {
 sub click {
     my ($self) = @_;
     my ($self) = @_;
-    my $res = { 'command' => 'clickElement', 'id' => $self->{id} };
+    my $res = { 'command' => 'clickElement', 'id' => $self->id };
     return $self->_execute_command($res);
     return $self->_execute_command($res);
 }
 }
 
 
@@ -64,7 +64,7 @@ sub click {
 
 
 sub submit {
 sub submit {
     my ($self) = @_;
     my ($self) = @_;
-    my $res = { 'command' => 'submitElement', 'id' => $self->{id} };
+    my $res = { 'command' => 'submitElement', 'id' => $self->id };
     return $self->_execute_command($res);
     return $self->_execute_command($res);
 }
 }
 
 
@@ -95,7 +95,7 @@ sub submit {
 
 
 sub send_keys {
 sub send_keys {
     my ( $self, @strings ) = @_;
     my ( $self, @strings ) = @_;
-    my $res = { 'command' => 'sendKeysToElement', 'id' => $self->{id} };
+    my $res = { 'command' => 'sendKeysToElement', 'id' => $self->id };
     map { $_ .= "" } @strings;
     map { $_ .= "" } @strings;
     my $params = {
     my $params = {
         'value' => \@strings,
         'value' => \@strings,
@@ -119,7 +119,7 @@ sub send_keys {
 
 
 sub is_selected {
 sub is_selected {
     my ($self) = @_;
     my ($self) = @_;
-    my $res = { 'command' => 'isElementSelected', 'id' => $self->{id} };
+    my $res = { 'command' => 'isElementSelected', 'id' => $self->id };
     return $self->_execute_command($res);
     return $self->_execute_command($res);
 }
 }
 
 
@@ -137,7 +137,7 @@ sub is_selected {
 
 
 sub set_selected {
 sub set_selected {
     my ($self) = @_;
     my ($self) = @_;
-    my $res = { 'command' => 'setElementSelected', 'id' => $self->{id} };
+    my $res = { 'command' => 'setElementSelected', 'id' => $self->id };
     return $self->_execute_command($res);
     return $self->_execute_command($res);
 }
 }
 
 
@@ -159,7 +159,7 @@ sub set_selected {
 
 
 sub toggle {
 sub toggle {
     my ($self) = @_;
     my ($self) = @_;
-    my $res = { 'command' => 'toggleElement', 'id' => $self->{id} };
+    my $res = { 'command' => 'toggleElement', 'id' => $self->id };
     return $self->_execute_command($res);
     return $self->_execute_command($res);
 }
 }
 
 
@@ -178,7 +178,7 @@ sub toggle {
 
 
 sub is_enabled {
 sub is_enabled {
     my ($self) = @_;
     my ($self) = @_;
-    my $res = { 'command' => 'isElementEnabled', 'id' => $self->{id} };
+    my $res = { 'command' => 'isElementEnabled', 'id' => $self->id };
     return $self->_execute_command($res);
     return $self->_execute_command($res);
 }
 }
 
 
@@ -198,7 +198,7 @@ sub is_enabled {
 
 
 sub get_element_location {
 sub get_element_location {
     my ($self) = @_;
     my ($self) = @_;
-    my $res = { 'command' => 'getElementLocation', 'id' => $self->{id} };
+    my $res = { 'command' => 'getElementLocation', 'id' => $self->id };
     return $self->_execute_command($res);
     return $self->_execute_command($res);
 }
 }
 
 
@@ -221,7 +221,7 @@ sub get_element_location {
 
 
 sub get_element_location_in_view {
 sub get_element_location_in_view {
     my ($self) = @_;
     my ($self) = @_;
-    my $res = { 'command' => 'getElementLocationInView', 'id' => $self->{id} };
+    my $res = { 'command' => 'getElementLocationInView', 'id' => $self->id };
     return $self->_execute_command($res);
     return $self->_execute_command($res);
 }
 }
 
 
@@ -240,7 +240,7 @@ sub get_element_location_in_view {
 
 
 sub get_tag_name {
 sub get_tag_name {
     my ($self) = @_;
     my ($self) = @_;
-    my $res = { 'command' => 'getElementTagName', 'id' => $self->{id} };
+    my $res = { 'command' => 'getElementTagName', 'id' => $self->id };
     return $self->_execute_command($res);
     return $self->_execute_command($res);
 }
 }
 
 
@@ -256,7 +256,7 @@ sub get_tag_name {
 
 
 sub clear {
 sub clear {
     my ($self) = @_;
     my ($self) = @_;
-    my $res = { 'command' => 'clearElement', 'id' => $self->{id} };
+    my $res = { 'command' => 'clearElement', 'id' => $self->id };
     return $self->_execute_command($res);
     return $self->_execute_command($res);
 }
 }
 
 
@@ -284,7 +284,7 @@ sub get_attribute {
     }
     }
     my $res = {
     my $res = {
         'command' => 'getElementAttribute',
         'command' => 'getElementAttribute',
-        'id'      => $self->{id},
+        'id'      => $self->id,
         'name'    => $attr_name,
         'name'    => $attr_name,
     };
     };
     return $self->_execute_command($res);
     return $self->_execute_command($res);
@@ -323,7 +323,7 @@ sub get_value {
 
 
 sub is_displayed {
 sub is_displayed {
     my ($self) = @_;
     my ($self) = @_;
-    my $res = { 'command' => 'isElementDisplayed', 'id' => $self->{id} };
+    my $res = { 'command' => 'isElementDisplayed', 'id' => $self->id };
     return $self->_execute_command($res);
     return $self->_execute_command($res);
 }
 }
 
 
@@ -348,7 +348,7 @@ sub drag {
     if ( ( not defined $x ) || ( not defined $y ) ) {
     if ( ( not defined $x ) || ( not defined $y ) ) {
         croak 'X & Y pixel coordinates not provided';
         croak 'X & Y pixel coordinates not provided';
     }
     }
-    my $res = { 'command' => 'dragElement', 'id' => $self->{id} };
+    my $res = { 'command' => 'dragElement', 'id' => $self->id };
     my $params = {
     my $params = {
         'x' => $x,
         'x' => $x,
         'y' => $y,
         'y' => $y,
@@ -372,7 +372,7 @@ sub drag {
 
 
 sub get_size {
 sub get_size {
     my ($self) = @_;
     my ($self) = @_;
-    my $res = { 'command' => 'getElementSize', 'id' => $self->{id} };
+    my $res = { 'command' => 'getElementSize', 'id' => $self->id };
     return $self->_execute_command($res);
     return $self->_execute_command($res);
 }
 }
 
 
@@ -391,7 +391,7 @@ sub get_size {
 
 
 sub get_text {
 sub get_text {
     my ($self) = @_;
     my ($self) = @_;
-    my $res = { 'command' => 'getElementText', 'id' => $self->{id} };
+    my $res = { 'command' => 'getElementText', 'id' => $self->id };
     return $self->_execute_command($res);
     return $self->_execute_command($res);
 }
 }
 
 
@@ -421,7 +421,7 @@ sub get_css_attribute {
     }
     }
     my $res = {
     my $res = {
         'command'       => 'getElementValueOfCssProperty',
         'command'       => 'getElementValueOfCssProperty',
-        'id'            => $self->{id},
+        'id'            => $self->id,
         'property_name' => $attr_name,
         'property_name' => $attr_name,
     };
     };
     return $self->_execute_command($res);
     return $self->_execute_command($res);
@@ -439,7 +439,7 @@ sub get_css_attribute {
 
 
 sub describe {
 sub describe {
     my ($self) = @_;
     my ($self) = @_;
-    my $res = { 'command' => 'describeElement', 'id' => $self->{id} };
+    my $res = { 'command' => 'describeElement', 'id' => $self->id };
     return $self->_execute_command($res);
     return $self->_execute_command($res);
 }
 }
 
 

+ 107 - 16
lib/Test/Selenium/Remote/WebElement.pm

@@ -2,85 +2,176 @@ package Test::Selenium::Remote::WebElement;
 use parent 'Selenium::Remote::WebElement';
 use parent 'Selenium::Remote::WebElement';
 use Moo;
 use Moo;
 use Test::Builder;
 use Test::Builder;
+use Try::Tiny;
 
 
 has _builder => (
 has _builder => (
     is      => 'lazy',
     is      => 'lazy',
     builder => sub { return Test::Builder->new() },
     builder => sub { return Test::Builder->new() },
-    handles => [qw/is_eq isnt_eq like unlike/],
+    handles => [qw/is_eq isnt_eq like unlike ok croak/],
 );
 );
 
 
+sub has_args { 
+    my $self = shift; 
+    my $fun_name = shift; 
+    my $hash_fun_args = { 
+        'get_attribute' => 1,
+    };
+    return ($hash_fun_args->{$fun_name} // 0);
+}
+
 
 
-sub _check_main_method {
+sub _check_method {
     my $self           = shift;
     my $self           = shift;
     my $method         = shift;
     my $method         = shift;
     my $method_to_test = shift;
     my $method_to_test = shift;
     $method = "get_$method";
     $method = "get_$method";
-    my $rv = $self->$method();
+    my @args = @_;
+    my $rv;
+    try { 
+        my $num_of_args = $self->has_args($method);
+        my @r_args = splice (@args,0,$num_of_args);
+        $rv = $self->$method(@r_args);
+    }
+    catch { 
+        $self->croak($_);
+    };
+    # +2 because of the delegation on _builder
+    local $Test::Builder::Level = $Test::Builder::Level + 2;
+    return $self->$method_to_test( $rv, @args );
+}
+
+sub _check_ok { 
+    my $self = shift; 
+    my $meth = shift; 
+    my $test_name = pop // $meth;
+    my $rv;
+    try { 
+        $rv = $self->$meth(@_);
+    }
+    catch { 
+        $self->croak($_);
+    };
 
 
     # +2 because of the delegation on _builder
     # +2 because of the delegation on _builder
     local $Test::Builder::Level = $Test::Builder::Level + 2;
     local $Test::Builder::Level = $Test::Builder::Level + 2;
-    return $self->$method_to_test( $rv, @_ );
+    return $self->ok($rv,$test_name,@_);
+}
+
+sub clear_ok { 
+    my $self = shift;
+    return $self->_check_ok('clear',@_);
+}
+
+sub click_ok { 
+    my $self = shift;
+    return $self->_check_ok('click',@_);
+}
+
+sub submit_ok { 
+    my $self = shift;
+    return $self->_check_ok('submit',@_);
+}
+
+sub is_selected_ok { 
+    my $self = shift;
+    return $self->_check_ok('is_selected',@_);
+}
+
+sub is_enabled_ok { 
+    my $self = shift;
+    return $self->_check_ok('is_enabled',@_);
+}
+
+sub is_displayed_ok { 
+    my $self = shift;
+    return $self->_check_ok('is_displayed',@_);
+}
+
+sub send_keys_ok { 
+    my $self = shift;
+    return $self->_check_ok('send_keys',@_);
 }
 }
 
 
 
 
+
 sub text_is {
 sub text_is {
     my $self = shift;
     my $self = shift;
-    return $self->_check_main_method( 'text', 'is_eq', @_ );
+    return $self->_check_method( 'text', 'is_eq', @_ );
 }
 }
 
 
 sub text_isnt {
 sub text_isnt {
     my $self = shift;
     my $self = shift;
-    return $self->_check_main_method( 'text', 'isnt_eq', @_ );
+    return $self->_check_method( 'text', 'isnt_eq', @_ );
 }
 }
 
 
 sub text_like {
 sub text_like {
     my $self = shift;
     my $self = shift;
-    return $self->_check_main_method( 'text', 'like', @_ );
+    return $self->_check_method( 'text', 'like', @_ );
 }
 }
 
 
 sub text_unlike {
 sub text_unlike {
     my $self = shift;
     my $self = shift;
-    return $self->_check_main_method( 'text', 'unlike', @_ );
+    return $self->_check_method( 'text', 'unlike', @_ );
 }
 }
 
 
 sub tag_name_is {
 sub tag_name_is {
     my $self = shift;
     my $self = shift;
-    return $self->_check_main_method( 'tag_name', 'is_eq', @_ );
+    return $self->_check_method( 'tag_name', 'is_eq', @_ );
 }
 }
 
 
 sub tag_name_isnt {
 sub tag_name_isnt {
     my $self = shift;
     my $self = shift;
-    return $self->_check_main_method( 'tag_name', 'isnt_eq', @_ );
+    return $self->_check_method( 'tag_name', 'isnt_eq', @_ );
 }
 }
 
 
 sub tag_name_like {
 sub tag_name_like {
     my $self = shift;
     my $self = shift;
-    return $self->_check_main_method( 'tag_name', 'like', @_ );
+    return $self->_check_method( 'tag_name', 'like', @_ );
 }
 }
 
 
 sub tag_name_unlike {
 sub tag_name_unlike {
     my $self = shift;
     my $self = shift;
-    return $self->_check_main_method( 'tag_name', 'unlike', @_ );
+    return $self->_check_method( 'tag_name', 'unlike', @_ );
 }
 }
 
 
 sub value_is {
 sub value_is {
     my $self = shift;
     my $self = shift;
-    return $self->_check_main_method( 'value', 'is_eq', @_ );
+    return $self->_check_method( 'value', 'is_eq', @_ );
 }
 }
 
 
 sub value_isnt {
 sub value_isnt {
     my $self = shift;
     my $self = shift;
-    return $self->_check_main_method( 'value', 'isnt_eq', @_ );
+    return $self->_check_method( 'value', 'isnt_eq', @_ );
 }
 }
 
 
 sub value_like {
 sub value_like {
     my $self = shift;
     my $self = shift;
-    return $self->_check_main_method( 'value', 'like', @_ );
+    return $self->_check_method( 'value', 'like', @_ );
 }
 }
 
 
 sub value_unlike {
 sub value_unlike {
     my $self = shift;
     my $self = shift;
-    return $self->_check_main_method( 'value', 'unlike', @_ );
+    return $self->_check_method( 'value', 'unlike', @_ );
+}
+
+sub attribute_is {
+    my $self = shift;
+    return $self->_check_method( 'attribute', 'is_eq', @_ );
+}
+
+sub attribute_isnt {
+    my $self = shift;
+    return $self->_check_method( 'attribute', 'isnt_eq', @_ );
+}
+
+sub attribute_like {
+    my $self = shift;
+    return $self->_check_method( 'attribute', 'like', @_ );
+}
+
+sub attribute_unlike {
+    my $self = shift;
+    return $self->_check_method( 'attribute', 'unlike', @_ );
 }
 }
 
 
 1;
 1;

+ 107 - 78
t/Test-Selenium-Remote-WebElement.t

@@ -28,77 +28,77 @@ $successful_element->mock( 'get_text',     sub {"my_text\nis fantastic"} );
 # Given input 'foo' to 'get_attribute', return 'my_foo';
 # Given input 'foo' to 'get_attribute', return 'my_foo';
 $successful_element->mock( 'get_attribute', sub { 'my_' . $_[1] } );
 $successful_element->mock( 'get_attribute', sub { 'my_' . $_[1] } );
 
 
-# check_test(
-#   sub { $successful_element->clear_ok },
-#   {
-#     ok => 1,
-#     name => "clear... no return value",
-#     diag => "",
-#   }
-# );
-
-# check_test(
-#   sub { $successful_element->clear_ok('test_name') },
-#   {
-#     ok => 1,
-#     name => "test_name... no return value",
-#     diag => "",
-#   }
-# );
-
-# check_test(
-#   sub { $successful_element->click_ok },
-#   {
-#     ok => 1,
-#     name => "click... no return value",
-#     diag => "",
-#   }
-# );
-
-# check_test(
-#   sub { $successful_element->submit_ok },
-#   {
-#     ok => 1,
-#     name => "submit... no return value",
-#     diag => "",
-#   }
-# );
-
-# check_test(
-#   sub { $successful_element->is_selected_ok },
-#   {
-#     ok => 1,
-#     name => "is_selected",
-#     diag => "",
-#   }
-# );
-
-# check_test(
-#   sub { $successful_element->is_enabled_ok },
-#   {
-#     ok => 1,
-#     name => "is_enabled",
-#     diag => "",
-#   }
-# );
-
-# check_test(
-#   sub { $successful_element->is_displayed_ok },
-#   {
-#     ok => 1,
-#     name => "is_displayed",
-#     diag => "",
-#   }
-# );
-
-# check_test(
-#   sub { $successful_element->send_keys_ok('Hello World', 'I sent keys') },
-#   {
-#     ok => 1,
-#     name => "I sent keys... no return value",
-#     diag => "",
-#   }
-# );
+check_test(
+  sub { $successful_element->clear_ok },
+  {
+    ok => 1,
+    name => "clear",
+    diag => "",
+  }
+);
+
+check_test(
+  sub { $successful_element->clear_ok('test_name') },
+  {
+    ok => 1,
+    name => "test_name",
+    diag => "",
+  }
+);
+
+check_test(
+  sub { $successful_element->click_ok },
+  {
+    ok => 1,
+    name => "click",
+    diag => "",
+  }
+);
+
+check_test(
+  sub { $successful_element->submit_ok },
+  {
+    ok => 1,
+    name => "submit",
+    diag => "",
+  }
+);
+
+check_test(
+  sub { $successful_element->is_selected_ok },
+  {
+    ok => 1,
+    name => "is_selected",
+    diag => "",
+  }
+);
+
+check_test(
+  sub { $successful_element->is_enabled_ok },
+  {
+    ok => 1,
+    name => "is_enabled",
+    diag => "",
+  }
+);
+
+check_test(
+  sub { $successful_element->is_displayed_ok },
+  {
+    ok => 1,
+    name => "is_displayed",
+    diag => "",
+  }
+);
+
+check_test(
+  sub { $successful_element->send_keys_ok('Hello World', 'I sent keys') },
+  {
+    ok => 1,
+    name => "I sent keys",
+    diag => "",
+  }
+);
 
 
 # tag_name_*
 # tag_name_*
 {
 {
@@ -229,14 +229,9 @@ $successful_element->mock( 'get_attribute', sub { 'my_' . $_[1] } );
             diag => "",
             diag => "",
         }
         }
     );
     );
-}
 
 
-#  attribute_is($attr_name,$match_str,$test_name);
-#  attribute_isnt($attr_name,$match_str,$test_name);
-#  attribute_like($attr_name,$match_re,$test_name);
-#  attribute_unlike($attr_name,$match_re,$test_name);
-{
-    local $TODO = 'not implemented yet.';
+}
+{ 
     check_test(
     check_test(
         sub {
         sub {
             $successful_element->attribute_is( 'foo', 'my_foo',
             $successful_element->attribute_is( 'foo', 'my_foo',
@@ -248,8 +243,42 @@ $successful_element->mock( 'get_attribute', sub { 'my_' . $_[1] } );
         }
         }
     );
     );
 
 
+    check_test(
+        sub {
+            $successful_element->attribute_isnt( 'foo', 'not_foo',
+                'attribute_is not_foo' );
+        },
+        {   ok   => 1,
+            name => "attribute_is not_foo",
+            diag => "",
+        }
+    );
+
+    check_test(
+        sub {
+            $successful_element->attribute_like( 'foo',qr/foo/,
+                'Matches my_attribute' );
+        },
+        {   ok   => 1,
+            name => "Matches my_attribute",
+            diag => "",
+        }
+    );
+
+    check_test(
+        sub {
+            $successful_element->attribute_unlike( 'bar',qr/foo/,
+                "Attribute does not match foo" );
+        },
+        {   ok   => 1,
+            name => "Attribute does not match foo",
+            diag => "",
+        }
+    );
 }
 }
 
 
+
+
 #  css_attribute_is($attr_name,$match_str,$test_name);
 #  css_attribute_is($attr_name,$match_str,$test_name);
 #  css_attribute_isnt($attr_name,$match_str,$test_name);
 #  css_attribute_isnt($attr_name,$match_str,$test_name);
 #  css_attribute_like($attr_name,$match_re,$test_name);
 #  css_attribute_like($attr_name,$match_re,$test_name);