TestHarness.pm 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package TestHarness;
  2. # ABSTRACT: Take care of set up for recording/replaying mocks
  3. use FindBin;
  4. use Moo;
  5. use Selenium::Remote::Mock::RemoteConnection;
  6. use Test::More;
  7. =head1 SYNOPSIS
  8. my $harness = TestHarness->new(
  9. this_file => $FindBin::Script
  10. );
  11. my %selenium_args = %{ $harness->base_caps };
  12. =head1 DESCRIPTION
  13. A setup class for all the repetitive things we need to do before
  14. running tests. First, we're deciding whether the test is in C<record>
  15. or C<replay> mode. If we're recording, we'll end up writing all the
  16. HTTP request/response pairs out to L</mock_file>. If we're replaying,
  17. we'll look for our OS-appropriate mock_file and try to read from it.
  18. After we figure that out, we can instantiate our
  19. Mock::RemoteConnection with the proper constructor arguments and
  20. return that as our base_args for use in the tests! Finally, on
  21. destruction, if we're recording, we make sure to dump out all of the
  22. request/response pairs to the mock_file.
  23. =attr this_file
  24. Required. Pass in the short name of the test file in use so we can
  25. figure out where the corresponding recording belongs. For a test file
  26. named C<t/01-driver.t>, we'd expect this argument to be
  27. C<01-driver.t>.
  28. =cut
  29. has calling_file => (
  30. is => 'ro',
  31. init_arg => 'this_file',
  32. required => 1
  33. );
  34. =attr record
  35. Optional. Determines whether or not this test run should record new
  36. mocks, or look up a previous recording to replay against them. If the
  37. parameter is not used during construction, the default behavior is to
  38. check for the environment variable WD_MOCKING_RECORD to be defined and
  39. equal to 1.
  40. =cut
  41. has record => (
  42. is => 'ro',
  43. init_args => undef,
  44. default => sub {
  45. if ( defined $ENV{WD_MOCKING_RECORD}
  46. && $ENV{WD_MOCKING_RECORD} == 1 )
  47. {
  48. return 1;
  49. }
  50. else {
  51. return 0;
  52. }
  53. }
  54. );
  55. has base_caps => (
  56. is => 'rw',
  57. lazy => 1,
  58. default => sub {
  59. my ($self) = @_;
  60. my $args = {
  61. browser_name => 'firefox',
  62. remote_conn => $self->mock_remote_conn
  63. };
  64. return $args;
  65. }
  66. );
  67. has mock_remote_conn => (
  68. is => 'ro',
  69. lazy => 1,
  70. builder => sub {
  71. my ($self) = @_;
  72. if ( $self->record ) {
  73. return Selenium::Remote::Mock::RemoteConnection->new( record => 1 );
  74. }
  75. else {
  76. return Selenium::Remote::Mock::RemoteConnection->new(
  77. replay => 1,
  78. replay_file => $self->mock_file
  79. );
  80. }
  81. }
  82. );
  83. has mock_file => (
  84. is => 'ro',
  85. lazy => 1,
  86. builder => sub {
  87. my ($self) = @_;
  88. # Since FindBin uses a Begin block, and we're using it in the
  89. # tests themselves, $FindBin::Bin will already be initialized
  90. # to the folder that the *.t files live in - that is, `t`.
  91. my $mock_folder = $FindBin::Bin . '/mock-recordings/';
  92. my $test_name = lc( $self->calling_file );
  93. $test_name =~ s/\.t$//;
  94. my $mock_file = $mock_folder . $test_name . '-mock.json';
  95. # If we're replaying, we need a mock to read from. Otherwise,
  96. # we can't do anything
  97. if ( not $self->record ) {
  98. plan skip_all =>
  99. "Mocking of tests is not been enabled for this platform"
  100. unless -e $mock_file;
  101. }
  102. return $mock_file;
  103. }
  104. );
  105. has website => (
  106. is => 'ro',
  107. default => sub {
  108. my ($self) = @_;
  109. my $port = 63636;
  110. return 'http://' . $self->domain . ':' . $port;
  111. }
  112. );
  113. has domain => (
  114. is => 'ro',
  115. default => sub { 'localhost' }
  116. );
  117. sub DEMOLISH {
  118. my ($self) = @_;
  119. if ( $self->record ) {
  120. $self->mock_remote_conn->dump_session_store( $self->mock_file );
  121. }
  122. }
  123. 1;