DoesTesting.pm 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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)?_element/) {
  46. # case find_element_ok was called with no arguments
  47. if (scalar(@r_args) == 1) {
  48. push @r_args, $self->default_finder;
  49. }
  50. else {
  51. if (scalar(@r_args) == 2) {
  52. # case find_element was called with no finder but
  53. # a test description
  54. my $finder = $r_args[1];
  55. my @FINDERS = keys (%{$self->FINDERS});
  56. unless ( any { $finder eq $_ } @FINDERS) {
  57. $r_args[1] = $self->default_finder;
  58. push @args, $finder;
  59. }
  60. }
  61. }
  62. }
  63. if ($method =~ m/^find_child_element/) {
  64. # case find_element_ok was called with no arguments
  65. if (scalar(@r_args) == 2) {
  66. push @r_args, $self->default_finder;
  67. }
  68. else {
  69. if (scalar(@r_args) == 3) {
  70. # case find_element was called with no finder but
  71. # a test description
  72. my $finder = $r_args[2];
  73. my @FINDERS = keys (%{$self->FINDERS});
  74. unless ( any { $finder eq $_ } @FINDERS) {
  75. $r_args[2] = $self->default_finder;
  76. push @args, $finder;
  77. }
  78. }
  79. }
  80. }
  81. if ($method eq 'find_no_element_ok') {
  82. $real_method = $method;
  83. $method = 'find_element';
  84. }
  85. $rv = $self->$method(@r_args);
  86. }
  87. catch {
  88. if ($real_method) {
  89. $method = $real_method;
  90. $rv = 1;
  91. }
  92. else {
  93. $self->croak($_);
  94. }
  95. };
  96. my $default_test_name = $method;
  97. $default_test_name .= "'" . join("' ", @r_args) . "'"
  98. if $num_of_args > 0;
  99. my $test_name = pop @args // $default_test_name;
  100. return $self->ok( $rv, $test_name);
  101. }
  102. # build the subs with the correct arg set
  103. sub _build_sub {
  104. my $self = shift;
  105. my $meth_name = shift;
  106. my @func_args;
  107. my $comparators = {
  108. is => 'is_eq',
  109. isnt => 'isnt_eq',
  110. like => 'like',
  111. unlike => 'unlike',
  112. };
  113. my @meth_elements = split( '_', $meth_name );
  114. my $meth = '_check_ok';
  115. my $meth_comp = pop @meth_elements;
  116. if ( $meth_comp eq 'ok' ) {
  117. push @func_args, join( '_', @meth_elements );
  118. }
  119. else {
  120. if ( defined( $comparators->{$meth_comp} ) ) {
  121. $meth = '_check_method';
  122. push @func_args, join( '_', @meth_elements ),
  123. $comparators->{$meth_comp};
  124. }
  125. else {
  126. return sub {
  127. my $self = shift;
  128. $self->croak("Sub $meth_name could not be defined");
  129. }
  130. }
  131. }
  132. return sub {
  133. my $self = shift;
  134. local $Test::Builder::Level = $Test::Builder::Level + 2;
  135. $self->$meth( @func_args, @_ );
  136. };
  137. }
  138. 1;
  139. =head1 NAME
  140. Selenium::Remote::Role::DoesTesting - Role implementing the common logic used for testing
  141. =cut