| 1 | package Playwright::Util; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 3 | $Playwright::Util::VERSION = '1.641'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use v5.28; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use JSON::MaybeXS(); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use Carp qw{confess}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Sereal::Encoder; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use Sereal::Decoder; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use File::Temp; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use POSIX(); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 3 | ▶ | use Scalar::Util qw{reftype}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 3 | ▶ | use Cwd(); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 3 | ▶ | use MIME::Base64(); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | #ABSTRACT: Common utility functions for the Playwright module | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 3 | ▶ | no warnings 'experimental'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 3 | ▶ | use feature qw{signatures}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 3 | ▶ | use constant IS_WIN => $^O eq 'MSWin32'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 5 | ▶ | sub request ( $method, $url, $host, $port, $ua, %args ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
request: called CC=8 SCAR=20.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 5 | my $fullurl = "http://$host:$port/$url"; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | # Handle passing Playwright elements as arguments | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | # Seems we also pass Playwright pages to get CDP Handles | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 5 | ▶ | if ( ref $args{args} eq 'ARRAY' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 2 | @{ $args{args} } = map { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 6 | my $transformed = $_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 6 | ▶ | if ( ref $_ && reftype $_ eq 'HASH' && exists $_->{guid} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
ref $_ and builtin::reftype($_) eq "HASH" and exists $_->{"guid"}
ref $_ and builtin::reftype($_) eq "HASH"
MC/DC0%0/3
ref $_ and builtin::reftype($_) eq "HASH" and exists $_->{"guid"}
ref $_
builtin::reftype($_) eq "HASH"
exists $_->{"guid"}
Truth table75%3/4
A and B and C
A= ref $_B= builtin::reftype($_) eq "HASH"C= exists $_->{"guid"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | $transformed = { uuid => $_->{guid} }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 6 | $transformed; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 2 | } @{ $args{args} }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 2 | $args{args} = _encode_file_payloads( $args{args} ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 4 | my $request = HTTP::Request->new( $method, $fullurl ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 4 | $request->header( 'Content-type' => 'application/json' ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 4 | $request->content( JSON::MaybeXS::encode_json( \%args ) ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 4 | my $response = $ua->request($request); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 4 | my $content = $response->decoded_content(); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | # If we get this kind of response the server failed to come up :( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 4 | ▶ | die "playwright server failed to spawn!" | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | if $content =~ m/^Can't connect to/; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 4 | local $@; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 4 | ▶ | my $decoded = eval { JSON::MaybeXS::decode_json($content) } or do { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 1 | confess( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | qq[error decoding Playwright server response: $@\ncontent:\n$content\n] | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 3 | my $msg = $decoded->{message}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 3 | ▶ | confess($msg) if $decoded->{error}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 2 | return $msg; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | # JSON cannot carry the raw bytes of a file payload, so playwright_server expects them as { base64 => ... }. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | # Containers are copied so that the caller's arguments stay as they were. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 2 | ▶ | sub _encode_file_payloads ($args) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_encode_file_payloads: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 2 | my $copy = [@$args]; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 2 | my @queue = map { \$_ } @$copy; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 2 | ▶ | while ( my $slot = shift @queue ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 12 | my $value = $$slot; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 12 | my $type = ref $value; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 12 | ▶ | if ( $type eq 'ARRAY' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 1 | $$slot = [@$value]; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 1 | push @queue, map { \$_ } @{$$slot}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | elsif ( $type eq 'HASH' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 7 | $$slot = {%$value}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 7 | ▶ | if ( _is_file_payload($value) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | $$slot->{buffer} = { base64 => | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 3 | MIME::Base64::encode_base64( $value->{buffer}, '' ) }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 2 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 4 | push @queue, map { \$$slot->{$_} } keys %{$$slot}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 1 | return $copy; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 7 | ▶ | sub _is_file_payload ($hash) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_is_file_payload: called CC=4 SCAR=14.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | return | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | exists $hash->{name} | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | && exists $hash->{mimeType} | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | && defined $hash->{buffer} | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 7 | ▶ | && !ref $hash->{buffer}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%6/9
exists $hash->{'name'} && exists $hash->{'mimeType'} && defined $hash->{'buffer'} && !ref($hash->{'buffer'})
exists $hash->{"name"} and exists $hash->{"mimeType"} and defined $hash->{"buffer"}
exists $hash->{"name"} and exists $hash->{"mimeType"}
MC/DC25%1/4
exists $hash->{'name'} && exists $hash->{'mimeType'} && defined $hash->{'buffer'} && !ref($hash->{'buffer'})
exists $hash->{"name"}
exists $hash->{"mimeType"}
defined $hash->{"buffer"}
!ref($hash->{'buffer'})
Truth table40%2/5
A and B and C && not(D)
A= exists $hash->{"name"}B= exists $hash->{"mimeType"}C= defined $hash->{"buffer"}D= !ref($hash->{'buffer'})
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | sub arr2hash ( $array, $primary_key, $callback = '' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
arr2hash: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | my $inside_out = {}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | @$inside_out{ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | map { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | ▶ | $callback ? $callback->( $_->{$primary_key} ) : $_->{$primary_key} | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | } @$array | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | } = @$array; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | return $inside_out; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | # Serialize a subprocess because NOTHING ON CPAN DOES THIS GRRRRR | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | ▶ | sub async ($subroutine) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
async: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | # The fork would result in the tmpdir getting whacked when it terminates. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | my $fh = File::Temp->new(); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | ▶ | my $pid = fork() // die "Could not fork"; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
fork // die("Could not fork")
MC/DC0%0/1
fork // die("Could not fork")
fork
Truth table0%0/2
A
A= fork
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | ▶ | _child( $fh->filename, $subroutine ) unless $pid; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | return { pid => $pid, file => $fh }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | ▶ | sub _child ( $filename, $subroutine ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_child: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | Sereal::Encoder->encode_to_file( $filename, $subroutine->() ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | # Prevent destructors from firing due to exiting instantly...unless we are on windows, where they won't. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | POSIX::_exit(0) unless IS_WIN; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | exit 0; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | ▶ | sub await ($to_wait) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
await: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 0 | waitpid( $to_wait->{pid}, 0 ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | confess("Timed out while waiting for event.") | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | ▶ | unless -f $to_wait->{file}->filename && -s _; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
-f $to_wait->{"file"}->filename and -s _
MC/DC0%0/2
-f $to_wait->{"file"}->filename and -s _
-f $to_wait->{"file"}->filename
-s _
Truth table0%0/3
A and B
A= -f $to_wait->{"file"}->filenameB= -s _
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | return Sereal::Decoder->decode_from_file( $to_wait->{file}->filename ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | # Make author tests work | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 0 | ▶ | sub find_node_modules { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
find_node_modules: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | return _find('node_modules'); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | ▶ | sub find_playwright_server { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
find_playwright_server: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 0 | return _find('bin/playwright_server'); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | ▶ | sub _find { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_find: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 0 | my $to_find = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | my $dir = | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 0 | File::Basename::dirname( Cwd::abs_path( $INC{'Playwright/Util.pm'} ) ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 0 | ▶ | while ( !-e "$dir/$to_find" ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | $dir = Cwd::abs_path("$dir/.."); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 0 | ▶ | last if $dir eq '/'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 0 | return Cwd::abs_path("$dir/$to_find"); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||