File Coverage

blib/lib/Playwright/Base.pm
Criterion Covered Total %
statement 69 74 93.2
branch 17 24 70.8
condition 4 12 33.3
subroutine 13 14 92.8
pod 1 2 50.0
total 104 126 82.5


line stmt bran cond sub pod time code
1             package Playwright::Base;
2             $Playwright::Base::VERSION = '1.551';
3 2     2   262527 use strict;
  2         4  
  2         66  
4 2     2   10 use warnings;
  2         7  
  2         83  
5              
6 2     2   47 use v5.28;
  2         7  
7              
8 2     2   795 use Sub::Install();
  2         3500  
  2         42  
9              
10 2     2   1664 use JSON;
  2         21914  
  2         11  
11 2     2   1228 use Playwright::Util();
  2         7  
  2         63  
12              
13             #ABSTRACT: Object representing Playwright pages
14              
15 2     2   11 no warnings 'experimental';
  2         4  
  2         83  
16 2     2   10 use feature qw{signatures};
  2         3  
  2         2333  
17              
18 1     1 1 394617 sub new ( $class, %options ) {
  1         4  
  1         6  
  1         2  
19              
20             my $self = bless(
21             {
22             type => $options{type},
23             guid => $options{id},
24             ua => $options{handle}{ua},
25             port => $options{handle}{port},
26             host => $options{handle}{host},
27             parent => $options{parent},
28             },
29 1         13 $class
30             );
31              
32 1         5 return ($self);
33             }
34              
35 3     3   7 sub _coerce ( $spec, %args ) {
  3         9  
  3         9  
  3         6  
36              
37             #Coerce bools correctly
38 3         6 my @argspec = values( %{ $spec->{ $args{command} }{args} } );
  3         888  
39 3         15 @argspec = sort { $a->{order} <=> $b->{order} } @argspec;
  5         15  
40              
41 3         15 for ( my $i = 0 ; $i < scalar(@argspec) ; $i++ ) {
42 4 100       10 next unless $i < @{ $args{args} };
  4         46  
43 3         10 my $arg = $args{args}[$i];
44 3         7 my $type = $argspec[$i]->{type};
45 3 100       16 if ( $type->{name} eq 'boolean' ) {
    100          
46 1         3 my $truthy = int( !!$arg );
47 1 50       5 $args{args}[$i] = $truthy ? JSON::true : JSON::false;
48             }
49             elsif ( $type->{name} eq 'Object' ) {
50             $type->{properties} =
51             Playwright::Util::arr2hash( $type->{properties}, 'name' )
52 1 50       6 if ref $type->{properties} eq 'ARRAY';
53 1         2 foreach my $prop ( keys( %{ $type->{properties} } ) ) {
  1         5  
54 3 100       46 next unless exists $arg->{$prop};
55 2         5 my $truthy = int( !!$arg->{$prop} );
56 2 100       11 next unless $type->{properties}{$prop}{type}{name} eq 'boolean';
57 1 50       7 $args{args}[$i]->{$prop} = $truthy ? JSON::true : JSON::false;
58             }
59             }
60             }
61              
62 3         20 return %args;
63             }
64              
65 2     2   7307 sub _api_request ( $self, %args ) {
  2         5  
  2         6  
  2         24  
66              
67 2         7 %args = Playwright::Base::_coerce( $self->spec(), %args );
68              
69             return Playwright::Util::async(
70 0     0   0 sub { &Playwright::Base::_do( $self, %args ) } )
71 2 50       11 if $args{command} =~ m/^waitFor/;
72              
73 2         7 my $msg = Playwright::Base::_do->( $self, %args );
74              
75 2 50       16 if ( ref $msg eq 'ARRAY' ) {
76             @$msg = map {
77 0         0 my $subject = $_;
  0         0  
78             $subject = $Playwright::mapper{ $_->{_type} }->( $self, $_ )
79             if ( ref $_ eq 'HASH' )
80             && $_->{_type}
81 0 0 0     0 && exists $Playwright::mapper{ $_->{_type} };
      0        
82 0         0 $subject
83             } @$msg;
84             }
85             return $Playwright::mapper{ $msg->{_type} }->( $self, $msg )
86             if ( ref $msg eq 'HASH' )
87             && $msg->{_type}
88 2 100 66     23 && exists $Playwright::mapper{ $msg->{_type} };
      66        
89 1         7 return $msg;
90             }
91              
92 2     2   4 sub _do ( $self, %args ) {
  2         22  
  2         6  
  2         4  
93             return Playwright::Util::request( 'POST', 'command', $self->{host},
94 2         16 $self->{port}, $self->{ua}, %args );
95             }
96              
97             sub spec {
98 4     4 0 51 return $Playwright::spec;
99             }
100              
101             1;
102              
103             __END__