DoesTesting.pm 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. $rv = $self->$method(@r_args);
  61. }
  62. catch {
  63. $self->croak($_);
  64. };
  65. my $default_test_name = $method;
  66. $default_test_name .= "'" . join("' ", @r_args) . "'"
  67. if $num_of_args > 0;
  68. my $test_name = pop @args // $default_test_name;
  69. return $self->ok( $rv, $test_name);
  70. }
  71. # build the subs with the correct arg set
  72. sub _build_sub {
  73. my $self = shift;
  74. my $meth_name = shift;
  75. my @func_args;
  76. my $comparators = {
  77. is => 'is_eq',
  78. isnt => 'isnt_eq',
  79. like => 'like',
  80. unlike => 'unlike',
  81. };
  82. my @meth_elements = split( '_', $meth_name );
  83. my $meth = '_check_ok';
  84. my $meth_comp = pop @meth_elements;
  85. if ( $meth_comp eq 'ok' ) {
  86. push @func_args, join( '_', @meth_elements );
  87. }
  88. else {
  89. if ( defined( $comparators->{$meth_comp} ) ) {
  90. $meth = '_check_method';
  91. push @func_args, join( '_', @meth_elements ),
  92. $comparators->{$meth_comp};
  93. }
  94. else {
  95. return sub {
  96. my $self = shift;
  97. $self->croak("Sub $meth_name could not be defined");
  98. }
  99. }
  100. }
  101. return sub {
  102. my $self = shift;
  103. local $Test::Builder::Level = $Test::Builder::Level + 2;
  104. $self->$meth( @func_args, @_ );
  105. };
  106. }
  107. 1;
  108. =head1 NAME
  109. Selenium::Remote::Role::DoesTesting - Role implementing the common logic used for testing
  110. =cut