WebElement.pm 5.5 KB

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