Util.pm 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package Playwright::Util;
  2. use strict;
  3. use warnings;
  4. use v5.28;
  5. use JSON::MaybeXS();
  6. use Carp qw{confess};
  7. use Sereal::Encoder;
  8. use Sereal::Decoder;
  9. use File::Temp;
  10. use POSIX();
  11. #ABSTRACT: Common utility functions for the Playwright module
  12. no warnings 'experimental';
  13. use feature qw{signatures};
  14. =head2 request(STRING method, STRING url, INTEGER port, LWP::UserAgent ua, HASH args) = HASH
  15. De-duplicates request logic in the Playwright Modules.
  16. =cut
  17. sub request ( $method, $url, $port, $ua, %args ) {
  18. my $fullurl = "http://localhost:$port/$url";
  19. # Handle passing Playwright elements as arguments
  20. if (ref $args{args} eq 'ARRAY') {
  21. @{$args{args}} = map {
  22. my $transformed = $_;
  23. if (ref($_) eq 'Playwright::ElementHandle' ) {
  24. $transformed = { uuid => $_->{guid} }
  25. }
  26. $transformed;
  27. } @{$args{args}};
  28. }
  29. my $request = HTTP::Request->new( $method, $fullurl );
  30. $request->header( 'Content-type' => 'application/json' );
  31. $request->content( JSON::MaybeXS::encode_json( \%args ) );
  32. my $response = $ua->request($request);
  33. my $content = $response->decoded_content();
  34. # If we get this kind of response the server failed to come up :(
  35. die "playwright server failed to spawn!" if $content =~ m/^Can't connect to/;
  36. my $decoded = JSON::MaybeXS::decode_json($content);
  37. my $msg = $decoded->{message};
  38. confess($msg) if $decoded->{error};
  39. return $msg;
  40. }
  41. sub arr2hash ($array,$primary_key,$callback='') {
  42. my $inside_out = {};
  43. @$inside_out{map { $callback ? $callback->($_->{$primary_key}) : $_->{$primary_key} } @$array} = @$array;
  44. return $inside_out;
  45. }
  46. # Serialize a subprocess because NOTHING ON CPAN DOES THIS GRRRRR
  47. sub async ($subroutine) {
  48. # The fork would result in the tmpdir getting whacked when it terminates.
  49. my (undef, $filename) = File::Temp::tempfile();
  50. my $pid = fork() // die "Could not fork";
  51. _child($filename, $subroutine) unless $pid;
  52. return { pid => $pid, file => $filename };
  53. }
  54. sub _child ($filename,$subroutine) {
  55. Sereal::Encoder->encode_to_file($filename,$subroutine->());
  56. # Prevent destructors from firing due to exiting instantly
  57. POSIX::_exit(0);
  58. }
  59. sub await ($to_wait) {
  60. waitpid($to_wait->{pid},0);
  61. confess("Timed out while waiting for event.") unless -f $to_wait->{file} && -s _;
  62. return Sereal::Decoder->decode_from_file($to_wait->{file});
  63. }
  64. 1;