1
0

WebElement.pm 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. package Test::Selenium::Remote::WebElement;
  2. # ABSTRACT: A sub-class of L<Selenium::Remote::WebElement>, with several test-specific method additions.
  3. use parent 'Selenium::Remote::WebElement';
  4. use Moo;
  5. use Test::Builder;
  6. use Try::Tiny;
  7. has _builder => (
  8. is => 'lazy',
  9. builder => sub { return Test::Builder->new() },
  10. handles => [qw/is_eq isnt_eq like unlike ok croak/],
  11. );
  12. sub has_args {
  13. my $self = shift;
  14. my $fun_name = shift;
  15. my $hash_fun_args = {
  16. 'get_attribute' => 1,
  17. };
  18. return ($hash_fun_args->{$fun_name} // 0);
  19. }
  20. sub _check_method {
  21. my $self = shift;
  22. my $method = shift;
  23. my $method_to_test = shift;
  24. $method = "get_$method";
  25. my @args = @_;
  26. my $rv;
  27. try {
  28. my $num_of_args = $self->has_args($method);
  29. my @r_args = splice (@args,0,$num_of_args);
  30. $rv = $self->$method(@r_args);
  31. }
  32. catch {
  33. $self->croak($_);
  34. };
  35. # +2 because of the delegation on _builder
  36. local $Test::Builder::Level = $Test::Builder::Level + 2;
  37. return $self->$method_to_test( $rv, @args );
  38. }
  39. sub _check_ok {
  40. my $self = shift;
  41. my $meth = shift;
  42. my $test_name = pop // $meth;
  43. my $rv;
  44. try {
  45. $rv = $self->$meth(@_);
  46. }
  47. catch {
  48. $self->croak($_);
  49. };
  50. # +2 because of the delegation on _builder
  51. local $Test::Builder::Level = $Test::Builder::Level + 2;
  52. return $self->ok($rv,$test_name,@_);
  53. }
  54. sub clear_ok {
  55. my $self = shift;
  56. return $self->_check_ok('clear',@_);
  57. }
  58. sub click_ok {
  59. my $self = shift;
  60. return $self->_check_ok('click',@_);
  61. }
  62. sub submit_ok {
  63. my $self = shift;
  64. return $self->_check_ok('submit',@_);
  65. }
  66. sub is_selected_ok {
  67. my $self = shift;
  68. return $self->_check_ok('is_selected',@_);
  69. }
  70. sub is_enabled_ok {
  71. my $self = shift;
  72. return $self->_check_ok('is_enabled',@_);
  73. }
  74. sub is_displayed_ok {
  75. my $self = shift;
  76. return $self->_check_ok('is_displayed',@_);
  77. }
  78. sub send_keys_ok {
  79. my $self = shift;
  80. return $self->_check_ok('send_keys',@_);
  81. }
  82. sub text_is {
  83. my $self = shift;
  84. return $self->_check_method( 'text', 'is_eq', @_ );
  85. }
  86. sub text_isnt {
  87. my $self = shift;
  88. return $self->_check_method( 'text', 'isnt_eq', @_ );
  89. }
  90. sub text_like {
  91. my $self = shift;
  92. return $self->_check_method( 'text', 'like', @_ );
  93. }
  94. sub text_unlike {
  95. my $self = shift;
  96. return $self->_check_method( 'text', 'unlike', @_ );
  97. }
  98. sub tag_name_is {
  99. my $self = shift;
  100. return $self->_check_method( 'tag_name', 'is_eq', @_ );
  101. }
  102. sub tag_name_isnt {
  103. my $self = shift;
  104. return $self->_check_method( 'tag_name', 'isnt_eq', @_ );
  105. }
  106. sub tag_name_like {
  107. my $self = shift;
  108. return $self->_check_method( 'tag_name', 'like', @_ );
  109. }
  110. sub tag_name_unlike {
  111. my $self = shift;
  112. return $self->_check_method( 'tag_name', 'unlike', @_ );
  113. }
  114. sub value_is {
  115. my $self = shift;
  116. return $self->_check_method( 'value', 'is_eq', @_ );
  117. }
  118. sub value_isnt {
  119. my $self = shift;
  120. return $self->_check_method( 'value', 'isnt_eq', @_ );
  121. }
  122. sub value_like {
  123. my $self = shift;
  124. return $self->_check_method( 'value', 'like', @_ );
  125. }
  126. sub value_unlike {
  127. my $self = shift;
  128. return $self->_check_method( 'value', 'unlike', @_ );
  129. }
  130. sub attribute_is {
  131. my $self = shift;
  132. return $self->_check_method( 'attribute', 'is_eq', @_ );
  133. }
  134. sub attribute_isnt {
  135. my $self = shift;
  136. return $self->_check_method( 'attribute', 'isnt_eq', @_ );
  137. }
  138. sub attribute_like {
  139. my $self = shift;
  140. return $self->_check_method( 'attribute', 'like', @_ );
  141. }
  142. sub attribute_unlike {
  143. my $self = shift;
  144. return $self->_check_method( 'attribute', 'unlike', @_ );
  145. }
  146. 1;
  147. __END__
  148. =head1 DESCRIPTION
  149. This is an I<experimental> addition to the Selenium::Remote::Driver
  150. distribution, and some interfaces may change.
  151. =head1 METHODS
  152. All methods from L<Selenium::Remote::WebElement> are available through this
  153. module, as well as the following test-specific methods. All test names are optional.
  154. text_is($match_str,$test_name);
  155. text_isnt($match_str,$test_name);
  156. text_like($match_re,$test_name);
  157. text_unlike($match_re,$test_name);
  158. tag_name_is($match_str,$test_name);
  159. tag_name_isnt($match_str,$test_name);
  160. tag_name_like($match_re,$test_name);
  161. tag_name_unlike($match_re,$test_name);
  162. value_is($match_str,$test_name);
  163. value_isnt($match_str,$test_name);
  164. value_like($match_re,$test_name);
  165. value_unlike($match_re,$test_name);
  166. clear_ok($test_name);
  167. click_ok($test_name);
  168. submit_ok($test_name);
  169. is_selected_ok($test_name);
  170. is_enabled_ok($test_name);
  171. is_displayed_ok($test_name);
  172. send_keys_ok($str)
  173. send_keys_ok($str,$test_name)
  174. attribute_is($attr_name,$match_str,$test_name); # TODO
  175. attribute_isnt($attr_name,$match_str,$test_name); # TODO
  176. attribute_like($attr_name,$match_re,$test_name); # TODO
  177. attribute_unlike($attr_name,$match_re,$test_name); # TODO
  178. css_attribute_is($attr_name,$match_str,$test_name); # TODO
  179. css_attribute_isnt($attr_name,$match_str,$test_name); # TODO
  180. css_attribute_like($attr_name,$match_re,$test_name); # TODO
  181. css_attribute_unlike($attr_name,$match_re,$test_name); # TODO
  182. element_location_is([x,y]) # TODO
  183. element_location_in_view_is([x,y]) # TODO
  184. =head1 AUTHORS
  185. =over 4
  186. =item *
  187. Created by: Mark Stosberg <mark@stosberg.org>, but inspired by
  188. L<Test::WWW::Selenium> and its authors.
  189. =back
  190. =head1 COPYRIGHT AND LICENSE
  191. Copyright (c) 2013 Mark Stosberg
  192. This program is free software; you can redistribute it and/or
  193. modify it under the same terms as Perl itself.