TestHarness.pm 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package TestHarness;
  2. # ABSTRACT: Take care of set up for recording/replaying mocks
  3. use Moo;
  4. use FindBin;
  5. use Selenium::Remote::Mock::RemoteConnection;
  6. =head1 SYNOPSIS
  7. my $harness = TestHarness->new(
  8. this_file => $FindBin::Script
  9. );
  10. my %selenium_args = %{ $harness->base_caps };
  11. unless ($harness->mocks_exist_for_platform) {
  12. plan skip_all => "Mocking of tests is not been enabled for this platform";
  13. }
  14. =attr this_file
  15. Required. Pass in the short name of the test file in use so we can
  16. figure out where the corresponding recording belongs. For a test file
  17. named C<t/01-driver.t>, we'd expect this argument to be
  18. C<01-driver.t>.
  19. =cut
  20. has calling_file => (
  21. is => 'ro',
  22. init_arg => 'this_file',
  23. required => 1
  24. );
  25. has record => (
  26. is => 'ro',
  27. init_args => undef,
  28. default => sub {
  29. if (defined $ENV{WD_MOCKING_RECORD}
  30. && $ENV{WD_MOCKING_RECORD} == 1) {
  31. return 1;
  32. }
  33. else {
  34. return 0;
  35. }
  36. }
  37. );
  38. has os => (
  39. is => 'ro',
  40. init_args => undef,
  41. default => sub {
  42. my $os = $^O;
  43. if ($os =~ m/(aix|freebsd|openbsd|sunos|solaris)/) {
  44. $os = 'linux';
  45. }
  46. return $os;
  47. }
  48. );
  49. has base_caps => (
  50. is => 'rw',
  51. lazy => 1,
  52. default => sub {
  53. my ($self) = @_;
  54. my $args = {
  55. browser_name => 'firefox',
  56. remote_conn => $self->mock_remote_conn
  57. };
  58. return $args;
  59. }
  60. );
  61. has mock_remote_conn => (
  62. is => 'ro',
  63. lazy => 1,
  64. builder => sub {
  65. my ($self) = @_;
  66. if ($self->record) {
  67. return Selenium::Remote::Mock::RemoteConnection->new(
  68. record => 1
  69. );
  70. }
  71. else {
  72. return Selenium::Remote::Mock::RemoteConnection->new(
  73. replay => 1,
  74. replay_file => $self->mock_file
  75. );
  76. }
  77. }
  78. );
  79. has mock_file => (
  80. is => 'ro',
  81. lazy => 1,
  82. builder => sub {
  83. my ($self) = @_;
  84. # Since FindBin uses a Begin block, and we're using it in the
  85. # tests themselves, $FindBin::Bin will already be initialized
  86. # to the folder that the *.t files live in - that is, `t`.
  87. my $mock_folder = $FindBin::Bin . '/mock-recordings/';
  88. my $test_name = lc($self->calling_file);
  89. $test_name =~ s/\.t$//;
  90. return $mock_folder . $test_name . '-mock-' . $self->os . '.json';
  91. }
  92. );
  93. sub mocks_exist_for_platform {
  94. my ($self) = @_;
  95. if ($self->record) {
  96. return 1;
  97. }
  98. else {
  99. # When we're replaying a test, we need recordings to be able
  100. # to do anything
  101. return -e $self->mock_file;
  102. }
  103. }
  104. sub DEMOLISH {
  105. my ($self) = @_;
  106. if ($self->record) {
  107. $self->mock_remote_conn->dump_session_store($self->mock_file);
  108. }
  109. }
  110. 1;