1
0

TestHarness.pm 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 os => (
  55. is => 'ro',
  56. init_args => undef,
  57. default => sub {
  58. my $os = $^O;
  59. if ($os =~ m/(aix|freebsd|openbsd|sunos|solaris)/) {
  60. $os = 'linux';
  61. }
  62. return $os;
  63. }
  64. );
  65. has base_caps => (
  66. is => 'rw',
  67. lazy => 1,
  68. default => sub {
  69. my ($self) = @_;
  70. my $args = {
  71. browser_name => 'firefox',
  72. remote_conn => $self->mock_remote_conn
  73. };
  74. return $args;
  75. }
  76. );
  77. has mock_remote_conn => (
  78. is => 'ro',
  79. lazy => 1,
  80. builder => sub {
  81. my ($self) = @_;
  82. if ($self->record) {
  83. return Selenium::Remote::Mock::RemoteConnection->new(
  84. record => 1
  85. );
  86. }
  87. else {
  88. return Selenium::Remote::Mock::RemoteConnection->new(
  89. replay => 1,
  90. replay_file => $self->mock_file
  91. );
  92. }
  93. }
  94. );
  95. has mock_file => (
  96. is => 'ro',
  97. lazy => 1,
  98. builder => sub {
  99. my ($self) = @_;
  100. # Since FindBin uses a Begin block, and we're using it in the
  101. # tests themselves, $FindBin::Bin will already be initialized
  102. # to the folder that the *.t files live in - that is, `t`.
  103. my $mock_folder = $FindBin::Bin . '/mock-recordings/';
  104. my $test_name = lc($self->calling_file);
  105. $test_name =~ s/\.t$//;
  106. my $mock_file = $mock_folder . $test_name . '-mock-' . $self->os . '.json';
  107. # If we're replaying, we need a mock to read from. Otherwise,
  108. # we can't do anything
  109. if (not $self->record) {
  110. plan skip_all => "Mocking of tests is not been enabled for this platform"
  111. unless -e $mock_file;
  112. }
  113. return $mock_file;
  114. }
  115. );
  116. has website => (
  117. is => 'ro',
  118. default => sub {
  119. my ($self) = @_;
  120. my $port = 63636;
  121. return 'http://' . $self->domain . ':' . $port;
  122. }
  123. );
  124. has domain => (
  125. is => 'ro',
  126. default => sub { 'localhost' }
  127. );
  128. sub DEMOLISH {
  129. my ($self) = @_;
  130. if ($self->record) {
  131. $self->mock_remote_conn->dump_session_store($self->mock_file);
  132. }
  133. }
  134. 1;