| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- package TestHarness;
- # ABSTRACT: Take care of set up for recording/replaying mocks
- use FindBin;
- use Moo;
- use Selenium::Remote::Mock::RemoteConnection;
- use Test::More;
- =head1 SYNOPSIS
- my $harness = TestHarness->new(
- this_file => $FindBin::Script
- );
- my %selenium_args = %{ $harness->base_caps };
- $harness->skip_all_unless_mocks_exist;
- =attr this_file
- Required. Pass in the short name of the test file in use so we can
- figure out where the corresponding recording belongs. For a test file
- named C<t/01-driver.t>, we'd expect this argument to be
- C<01-driver.t>.
- =cut
- has calling_file => (
- is => 'ro',
- init_arg => 'this_file',
- required => 1
- );
- has record => (
- is => 'ro',
- init_args => undef,
- default => sub {
- if (defined $ENV{WD_MOCKING_RECORD}
- && $ENV{WD_MOCKING_RECORD} == 1) {
- return 1;
- }
- else {
- return 0;
- }
- }
- );
- has os => (
- is => 'ro',
- init_args => undef,
- default => sub {
- my $os = $^O;
- if ($os =~ m/(aix|freebsd|openbsd|sunos|solaris)/) {
- $os = 'linux';
- }
- return $os;
- }
- );
- has base_caps => (
- is => 'rw',
- lazy => 1,
- default => sub {
- my ($self) = @_;
- my $args = {
- browser_name => 'firefox',
- remote_conn => $self->mock_remote_conn
- };
- return $args;
- }
- );
- has mock_remote_conn => (
- is => 'ro',
- lazy => 1,
- builder => sub {
- my ($self) = @_;
- if ($self->record) {
- return Selenium::Remote::Mock::RemoteConnection->new(
- record => 1
- );
- }
- else {
- return Selenium::Remote::Mock::RemoteConnection->new(
- replay => 1,
- replay_file => $self->mock_file
- );
- }
- }
- );
- has mock_file => (
- is => 'ro',
- lazy => 1,
- builder => sub {
- my ($self) = @_;
- # Since FindBin uses a Begin block, and we're using it in the
- # tests themselves, $FindBin::Bin will already be initialized
- # to the folder that the *.t files live in - that is, `t`.
- my $mock_folder = $FindBin::Bin . '/mock-recordings/';
- my $test_name = lc($self->calling_file);
- $test_name =~ s/\.t$//;
- my $mock_file = $mock_folder . $test_name . '-mock-' . $self->os . '.json';
- # If we're replaying, we need a mock to read from. Otherwise,
- # we can't do anything
- if (not $self->record) {
- plan skip_all => "Mocking of tests is not been enabled for this platform"
- unless -e $mock_file;
- }
- return $mock_file;
- }
- );
- sub DEMOLISH {
- my ($self) = @_;
- if ($self->record) {
- $self->mock_remote_conn->dump_session_store($self->mock_file);
- }
- }
- 1;
|