1
0

TestHarness.pm 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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.
  17. =cut
  18. has calling_file => (
  19. is => 'ro',
  20. init_arg => 'this_file',
  21. required => 1
  22. );
  23. has record => (
  24. is => 'ro',
  25. init_args => undef,
  26. default => sub {
  27. if (defined $ENV{WD_MOCKING_RECORD}
  28. && $ENV{WD_MOCKING_RECORD} == 1) {
  29. return 1
  30. }
  31. else {
  32. return 0
  33. }
  34. }
  35. );
  36. has os => (
  37. is => 'ro',
  38. init_args => undef,
  39. default => sub {
  40. my $os = $^O;
  41. if ($os =~ m/(aix|freebsd|openbsd|sunos|solaris)/) {
  42. $os = 'linux';
  43. }
  44. return $os;
  45. }
  46. );
  47. has base_caps => (
  48. is => 'rw',
  49. lazy => 1,
  50. default => sub {
  51. my ($self) = @_;
  52. my $args = {
  53. browser_name => 'firefox',
  54. remote_conn => $self->mock_remote_conn
  55. };
  56. return $args;
  57. }
  58. );
  59. has mock_remote_conn => (
  60. is => 'ro',
  61. lazy => 1,
  62. builder => sub {
  63. my ($self) = @_;
  64. if ($self->record) {
  65. return Selenium::Remote::Mock::RemoteConnection->new(
  66. record => 1
  67. );
  68. }
  69. else {
  70. return Selenium::Remote::Mock::RemoteConnection->new(
  71. replay => 1,
  72. replay_file => $self->mock_file
  73. );
  74. }
  75. }
  76. );
  77. has mock_file => (
  78. is => 'ro',
  79. lazy => 1,
  80. builder => sub {
  81. my ($self) = @_;
  82. # Since FindBin uses a Begin block, and we're using it in the
  83. # tests themselves, $findBin::Bin will already be initialized
  84. # to the folder that the *.t files live in - that is, `t`.
  85. my $mock_folder = $FindBin::Bin . '/mock-recordings/';
  86. my $test_name = lc($self->calling_file);
  87. $test_name =~ s/\.t$//;
  88. return $mock_folder . $test_name . '-mock-' . $self->os . '.json';
  89. }
  90. );
  91. sub mocks_exist_for_platform {
  92. my ($self) = @_;
  93. if ($self->record) {
  94. return 1;
  95. }
  96. else {
  97. # When we're replaying a test, we need recordings to be able
  98. # to do anything
  99. return -e $self->mock_file;
  100. }
  101. }
  102. sub DEMOLISH {
  103. my ($self) = @_;
  104. if ($self->record) {
  105. $self->remote_conn->dump_session_store($self->mock_file);
  106. }
  107. }
  108. 1;