DoesTesting.pm 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package Test::Selenium::Remote::Role::DoesTesting;
  2. # ABSTRACT: Role to cope with everything that is related to testing (could
  3. # be reused in both testing classes)
  4. use Moo::Role;
  5. use Test::Builder;
  6. use Try::Tiny;
  7. use List::MoreUtils qw/any/;
  8. use namespace::clean;
  9. requires qw(func_list has_args);
  10. has _builder => (
  11. is => 'lazy',
  12. builder => sub { return Test::Builder->new() },
  13. handles => [qw/is_eq isnt_eq like unlike ok croak/],
  14. );
  15. # main method for non ok tests
  16. sub _check_method {
  17. my $self = shift;
  18. my $method = shift;
  19. my $method_to_test = shift;
  20. $method = "get_$method";
  21. my @args = @_;
  22. my $rv;
  23. try {
  24. my $num_of_args = $self->has_args($method);
  25. my @r_args = splice( @args, 0, $num_of_args );
  26. $rv = $self->$method(@r_args);
  27. }
  28. catch {
  29. $self->croak($_);
  30. };
  31. return $self->$method_to_test( $rv, @args );
  32. }
  33. # main method for _ok tests
  34. # a bit hacked so that find_no_element_ok can also be processed
  35. # TODO: maybe simplify a bit the logic
  36. sub _check_ok {
  37. my $self = shift;
  38. my $method = shift;
  39. my $real_method = '';
  40. my @args = @_;
  41. my ($rv, $num_of_args, @r_args);
  42. try {
  43. $num_of_args = $self->has_args($method);
  44. @r_args = splice( @args, 0, $num_of_args );
  45. if ($method =~ m/^find(_no|_child)?_element/) {
  46. # case find_element_ok was called with no arguments
  47. if (scalar(@r_args) - $num_of_args == 1) {
  48. push @r_args, $self->default_finder;
  49. }
  50. else {
  51. if (scalar(@r_args) == $num_of_args) {
  52. # case find_element was called with no finder but
  53. # a test description
  54. my $finder = $r_args[$num_of_args - 1];
  55. my @FINDERS = keys (%{$self->FINDERS});
  56. unless ( any { $finder eq $_ } @FINDERS) {
  57. $r_args[$num_of_args - 1] = $self->default_finder;
  58. push @args, $finder;
  59. }
  60. }
  61. }
  62. }
  63. if ($method eq 'find_no_element') {
  64. $real_method = $method;
  65. $method = 'find_element';
  66. }
  67. $rv = $self->$method(@r_args);
  68. }
  69. catch {
  70. if ($real_method) {
  71. $method = $real_method;
  72. $rv = 1;
  73. }
  74. else {
  75. $self->croak($_);
  76. }
  77. };
  78. my $default_test_name = $method;
  79. $default_test_name .= "'" . join("' ", @r_args) . "'"
  80. if $num_of_args > 0;
  81. my $test_name = pop @args // $default_test_name;
  82. return $self->ok( $rv, $test_name);
  83. }
  84. # build the subs with the correct arg set
  85. sub _build_sub {
  86. my $self = shift;
  87. my $meth_name = shift;
  88. my @func_args;
  89. my $comparators = {
  90. is => 'is_eq',
  91. isnt => 'isnt_eq',
  92. like => 'like',
  93. unlike => 'unlike',
  94. };
  95. my @meth_elements = split( '_', $meth_name );
  96. my $meth = '_check_ok';
  97. my $meth_comp = pop @meth_elements;
  98. if ( $meth_comp eq 'ok' ) {
  99. push @func_args, join( '_', @meth_elements );
  100. }
  101. else {
  102. if ( defined( $comparators->{$meth_comp} ) ) {
  103. $meth = '_check_method';
  104. push @func_args, join( '_', @meth_elements ),
  105. $comparators->{$meth_comp};
  106. }
  107. else {
  108. return sub {
  109. my $self = shift;
  110. $self->croak("Sub $meth_name could not be defined");
  111. }
  112. }
  113. }
  114. return sub {
  115. my $self = shift;
  116. local $Test::Builder::Level = $Test::Builder::Level + 2;
  117. $self->$meth( @func_args, @_ );
  118. };
  119. }
  120. 1;
  121. =head1 NAME
  122. Selenium::Remote::Role::DoesTesting - Role implementing the common logic used for testing
  123. =cut