DoesTesting.pm 4.0 KB

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