1
0

DoesTesting.pm 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. $DB::single = 1;
  25. my $num_of_args = $self->has_args($method);
  26. my @r_args = splice( @args, 0, $num_of_args );
  27. $rv = $self->$method(@r_args);
  28. }
  29. catch {
  30. $self->croak($_);
  31. };
  32. return $self->$method_to_test( $rv, @args );
  33. }
  34. # main method for _ok tests
  35. # a bit hacked so that find_no_element_ok can also be processed
  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. # quick hack to fit 'find_no_element' into check_ok logic
  64. if ($method eq 'find_no_element') {
  65. $real_method = $method;
  66. $method = 'find_element';
  67. }
  68. $rv = $self->$method(@r_args);
  69. }
  70. catch {
  71. if ($real_method) {
  72. $method = $real_method;
  73. $rv = 1;
  74. }
  75. else {
  76. $self->croak($_);
  77. }
  78. };
  79. my $default_test_name = $method;
  80. $default_test_name .= "'" . join("' ", @r_args) . "'"
  81. if $num_of_args > 0;
  82. my $test_name = pop @args // $default_test_name;
  83. return $self->ok( $rv, $test_name);
  84. }
  85. # build the subs with the correct arg set
  86. sub _build_sub {
  87. my $self = shift;
  88. my $meth_name = shift;
  89. my @func_args;
  90. my $comparators = {
  91. is => 'is_eq',
  92. isnt => 'isnt_eq',
  93. like => 'like',
  94. unlike => 'unlike',
  95. };
  96. my @meth_elements = split( '_', $meth_name );
  97. my $meth = '_check_ok';
  98. my $meth_comp = pop @meth_elements;
  99. if ( $meth_comp eq 'ok' ) {
  100. push @func_args, join( '_', @meth_elements );
  101. }
  102. else {
  103. if ( defined( $comparators->{$meth_comp} ) ) {
  104. $meth = '_check_method';
  105. push @func_args, join( '_', @meth_elements ),
  106. $comparators->{$meth_comp};
  107. }
  108. else {
  109. return sub {
  110. my $self = shift;
  111. $self->croak("Sub $meth_name could not be defined");
  112. }
  113. }
  114. }
  115. return sub {
  116. my $self = shift;
  117. local $Test::Builder::Level = $Test::Builder::Level + 2;
  118. $self->$meth( @func_args, @_ );
  119. };
  120. }
  121. 1;
  122. =head1 NAME
  123. Selenium::Remote::Role::DoesTesting - Role implementing the common logic used for testing
  124. =cut