1
0

TestHarness.pm 2.8 KB

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