TestHarness.pm 3.3 KB

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