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. return 1;
  48. }
  49. else {
  50. return 0;
  51. }
  52. }
  53. );
  54. has base_caps => (
  55. is => 'rw',
  56. lazy => 1,
  57. default => sub {
  58. my ($self) = @_;
  59. my $args = {
  60. browser_name => 'firefox',
  61. remote_conn => $self->mock_remote_conn
  62. };
  63. return $args;
  64. }
  65. );
  66. has mock_remote_conn => (
  67. is => 'ro',
  68. lazy => 1,
  69. builder => sub {
  70. my ($self) = @_;
  71. if ($self->record) {
  72. return Selenium::Remote::Mock::RemoteConnection->new(
  73. record => 1
  74. );
  75. }
  76. else {
  77. return Selenium::Remote::Mock::RemoteConnection->new(
  78. replay => 1,
  79. replay_file => $self->mock_file
  80. );
  81. }
  82. }
  83. );
  84. has mock_file => (
  85. is => 'ro',
  86. lazy => 1,
  87. builder => sub {
  88. my ($self) = @_;
  89. # Since FindBin uses a Begin block, and we're using it in the
  90. # tests themselves, $FindBin::Bin will already be initialized
  91. # to the folder that the *.t files live in - that is, `t`.
  92. my $mock_folder = $FindBin::Bin . '/mock-recordings/';
  93. my $test_name = lc($self->calling_file);
  94. $test_name =~ s/\.t$//;
  95. my $mock_file = $mock_folder . $test_name . '-mock.json';
  96. # If we're replaying, we need a mock to read from. Otherwise,
  97. # we can't do anything
  98. if (not $self->record) {
  99. plan skip_all => "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;