1
0

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