WebElement.pm 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package Test::Selenium::Remote::WebElement;
  2. # ABSTRACT: A sub-class of L<Selenium::Remote::WebElement>, with several test-specific method additions.
  3. use Moo;
  4. use Sub::Install;
  5. extends 'Selenium::Remote::WebElement';
  6. # list of test functions to be built
  7. has func_list => (
  8. is => 'lazy',
  9. builder => sub {
  10. return [
  11. 'clear_ok', 'click_ok',
  12. 'send_keys_ok', 'is_displayed_ok',
  13. 'is_enabled_ok', 'is_selected_ok', 'submit_ok',
  14. 'text_is', 'text_isnt', 'text_like', 'text_unlike',
  15. 'attribute_is', 'attribute_isnt', 'attribute_like',
  16. 'attribute_unlike', 'value_is', 'value_isnt', 'value_like',
  17. 'value_unlike', 'tag_name_is', 'tag_name_isnt', 'tag_name_like',
  18. 'tag_name_unlike'
  19. ];
  20. }
  21. );
  22. with 'Test::Selenium::Remote::Role::DoesTesting';
  23. # helper so we could specify the num of args a method takes (if any)
  24. sub has_args {
  25. my $self = shift;
  26. my $fun_name = shift;
  27. my $hash_fun_args = {
  28. 'get_attribute' => 1,
  29. send_keys => 1,
  30. };
  31. return ( $hash_fun_args->{$fun_name} // 0 );
  32. }
  33. # install the test methods into the class namespace
  34. sub BUILD {
  35. my $self = shift;
  36. foreach my $method_name ( @{ $self->func_list } ) {
  37. unless ( defined( __PACKAGE__->can($method_name) ) ) {
  38. my $sub = $self->_build_sub($method_name);
  39. Sub::Install::install_sub(
  40. { code => $sub,
  41. into => __PACKAGE__,
  42. as => $method_name
  43. }
  44. );
  45. }
  46. }
  47. }
  48. 1;
  49. __END__
  50. =head1 DESCRIPTION
  51. This is an I<experimental> addition to the Selenium::Remote::Driver
  52. distribution, and some interfaces may change.
  53. =head1 METHODS
  54. All methods from L<Selenium::Remote::WebElement> are available through this
  55. module, as well as the following test-specific methods. All test names are optional.
  56. text_is($match_str,$test_name);
  57. text_isnt($match_str,$test_name);
  58. text_like($match_re,$test_name);
  59. text_unlike($match_re,$test_name);
  60. tag_name_is($match_str,$test_name);
  61. tag_name_isnt($match_str,$test_name);
  62. tag_name_like($match_re,$test_name);
  63. tag_name_unlike($match_re,$test_name);
  64. value_is($match_str,$test_name);
  65. value_isnt($match_str,$test_name);
  66. value_like($match_re,$test_name);
  67. value_unlike($match_re,$test_name);
  68. clear_ok($test_name);
  69. click_ok($test_name);
  70. submit_ok($test_name);
  71. is_selected_ok($test_name);
  72. is_enabled_ok($test_name);
  73. is_displayed_ok($test_name);
  74. send_keys_ok($str)
  75. send_keys_ok($str,$test_name)
  76. attribute_is($attr_name,$match_str,$test_name);
  77. attribute_isnt($attr_name,$match_str,$test_name);
  78. attribute_like($attr_name,$match_re,$test_name);
  79. attribute_unlike($attr_name,$match_re,$test_name);
  80. css_attribute_is($attr_name,$match_str,$test_name); # TODO
  81. css_attribute_isnt($attr_name,$match_str,$test_name); # TODO
  82. css_attribute_like($attr_name,$match_re,$test_name); # TODO
  83. css_attribute_unlike($attr_name,$match_re,$test_name); # TODO
  84. element_location_is([x,y]) # TODO
  85. element_location_in_view_is([x,y]) # TODO
  86. =head1 AUTHORS
  87. =over 4
  88. =item *
  89. Created by: Mark Stosberg <mark@stosberg.org>, but inspired by
  90. L<Test::WWW::Selenium> and its authors.
  91. =back
  92. =head1 COPYRIGHT AND LICENSE
  93. Copyright (c) 2013 Mark Stosberg
  94. This program is free software; you can redistribute it and/or
  95. modify it under the same terms as Perl itself.