Base.pm 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package Playwright::Base;
  2. use strict;
  3. use warnings;
  4. use v5.28;
  5. use Sub::Install();
  6. use JSON;
  7. use Playwright::Util();
  8. #ABSTRACT: Object representing Playwright pages
  9. no warnings 'experimental';
  10. use feature qw{signatures};
  11. =head2 DESCRIPTION
  12. Base class for each Playwright class magic'd up by Sub::Install in Playwright's BEGIN block.
  13. You probably shouldn't use this.
  14. The specification for each class can be inspected with the 'spec' method:
  15. use Data::Dumper;
  16. my $object = Playwright::Base->new(...);
  17. print Dumper($object->spec());
  18. =head1 CONSTRUCTOR
  19. =head2 new(HASH) = (Playwright::Base)
  20. Creates a new page and returns a handle to interact with it.
  21. =head3 INPUT
  22. handle (Playwright) : Playwright object.
  23. id (STRING) : _guid returned by a response from the Playwright server with the provided type.
  24. type (STRING) : Type to actually use
  25. parent (Playwright::*) : Parent Object (such as a page)
  26. =cut
  27. sub new ( $class, %options ) {
  28. my $self = bless(
  29. {
  30. type => $options{type},
  31. guid => $options{id},
  32. ua => $options{handle}{ua},
  33. port => $options{handle}{port},
  34. host => $options{handle}{host},
  35. parent => $options{parent},
  36. },
  37. $class
  38. );
  39. return ($self);
  40. }
  41. sub _coerce ( $spec, %args ) {
  42. #Coerce bools correctly
  43. my @argspec = values( %{ $spec->{ $args{command} }{args} } );
  44. @argspec = sort { $a->{order} <=> $b->{order} } @argspec;
  45. for ( my $i = 0 ; $i < scalar(@argspec) ; $i++ ) {
  46. next unless $i < @{ $args{args} };
  47. my $arg = $args{args}[$i];
  48. my $type = $argspec[$i]->{type};
  49. if ( $type->{name} eq 'boolean' ) {
  50. my $truthy = int( !!$arg );
  51. $args{args}[$i] = $truthy ? JSON::true : JSON::false;
  52. }
  53. elsif ( $type->{name} eq 'Object' ) {
  54. $type->{properties} = Playwright::Util::arr2hash($type->{properties},'name') if ref $type->{properties} eq 'ARRAY';
  55. foreach my $prop ( keys( %{ $type->{properties} } ) ) {
  56. next unless exists $arg->{$prop};
  57. my $truthy = int( !!$arg->{$prop} );
  58. next unless $type->{properties}{$prop}{type}{name} eq 'boolean';
  59. $args{args}[$i]->{$prop} = $truthy ? JSON::true : JSON::false;
  60. }
  61. }
  62. }
  63. return %args;
  64. }
  65. sub _api_request ( $self, %args ) {
  66. %args = Playwright::Base::_coerce( $self->spec(), %args );
  67. return Playwright::Util::async( sub { &Playwright::Base::_do( $self, %args ) } )
  68. if $args{command} =~ m/^waitFor/;
  69. my $msg = Playwright::Base::_do->( $self, %args );
  70. if ( ref $msg eq 'ARRAY' ) {
  71. @$msg = map {
  72. my $subject = $_;
  73. $subject = $Playwright::mapper{ $_->{_type} }->( $self, $_ )
  74. if ( ref $_ eq 'HASH' )
  75. && $_->{_type}
  76. && exists $Playwright::mapper{ $_->{_type} };
  77. $subject
  78. } @$msg;
  79. }
  80. return $Playwright::mapper{ $msg->{_type} }->( $self, $msg )
  81. if ( ref $msg eq 'HASH' )
  82. && $msg->{_type}
  83. && exists $Playwright::mapper{ $msg->{_type} };
  84. return $msg;
  85. }
  86. sub _do ( $self, %args ) {
  87. return Playwright::Util::request( 'POST', 'command', $self->{host}, $self->{port},
  88. $self->{ua}, %args );
  89. }
  90. sub spec {
  91. return $Playwright::spec;
  92. }
  93. 1;