TestHarness.pm 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. has record => (
  35. is => 'ro',
  36. init_args => undef,
  37. default => sub {
  38. if (defined $ENV{WD_MOCKING_RECORD}
  39. && $ENV{WD_MOCKING_RECORD} == 1) {
  40. return 1;
  41. }
  42. else {
  43. return 0;
  44. }
  45. }
  46. );
  47. has os => (
  48. is => 'ro',
  49. init_args => undef,
  50. default => sub {
  51. my $os = $^O;
  52. if ($os =~ m/(aix|freebsd|openbsd|sunos|solaris)/) {
  53. $os = 'linux';
  54. }
  55. return $os;
  56. }
  57. );
  58. has base_caps => (
  59. is => 'rw',
  60. lazy => 1,
  61. default => sub {
  62. my ($self) = @_;
  63. my $args = {
  64. browser_name => 'firefox',
  65. remote_conn => $self->mock_remote_conn
  66. };
  67. return $args;
  68. }
  69. );
  70. has mock_remote_conn => (
  71. is => 'ro',
  72. lazy => 1,
  73. builder => sub {
  74. my ($self) = @_;
  75. if ($self->record) {
  76. return Selenium::Remote::Mock::RemoteConnection->new(
  77. record => 1
  78. );
  79. }
  80. else {
  81. return Selenium::Remote::Mock::RemoteConnection->new(
  82. replay => 1,
  83. replay_file => $self->mock_file
  84. );
  85. }
  86. }
  87. );
  88. has mock_file => (
  89. is => 'ro',
  90. lazy => 1,
  91. builder => sub {
  92. my ($self) = @_;
  93. # Since FindBin uses a Begin block, and we're using it in the
  94. # tests themselves, $FindBin::Bin will already be initialized
  95. # to the folder that the *.t files live in - that is, `t`.
  96. my $mock_folder = $FindBin::Bin . '/mock-recordings/';
  97. my $test_name = lc($self->calling_file);
  98. $test_name =~ s/\.t$//;
  99. my $mock_file = $mock_folder . $test_name . '-mock-' . $self->os . '.json';
  100. # If we're replaying, we need a mock to read from. Otherwise,
  101. # we can't do anything
  102. if (not $self->record) {
  103. plan skip_all => "Mocking of tests is not been enabled for this platform"
  104. unless -e $mock_file;
  105. }
  106. return $mock_file;
  107. }
  108. );
  109. sub DEMOLISH {
  110. my ($self) = @_;
  111. if ($self->record) {
  112. $self->mock_remote_conn->dump_session_store($self->mock_file);
  113. }
  114. }
  115. 1;