Base.pm 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. parent => $options{parent},
  35. },
  36. $class
  37. );
  38. return ($self);
  39. }
  40. sub _coerce ( $spec, %args ) {
  41. #Coerce bools correctly
  42. my @argspec = values( %{ $spec->{ $args{command} }{args} } );
  43. @argspec = sort { $a->{order} <=> $b->{order} } @argspec;
  44. for ( my $i = 0 ; $i < scalar(@argspec) ; $i++ ) {
  45. next unless $i < @{ $args{args} };
  46. my $arg = $args{args}[$i];
  47. my $type = $argspec[$i]->{type};
  48. if ( $type->{name} eq 'boolean' ) {
  49. my $truthy = int( !!$arg );
  50. $args{args}[$i] = $truthy ? JSON::true : JSON::false;
  51. }
  52. elsif ( $type->{name} eq 'Object' ) {
  53. $type->{properties} = Playwright::Util::arr2hash($type->{properties},'name') if ref $type->{properties} eq 'ARRAY';
  54. foreach my $prop ( keys( %{ $type->{properties} } ) ) {
  55. next unless exists $arg->{$prop};
  56. my $truthy = int( !!$arg->{$prop} );
  57. next unless $type->{properties}{$prop}{type}{name} eq 'boolean';
  58. $args{args}[$i]->{$prop} = $truthy ? JSON::true : JSON::false;
  59. }
  60. }
  61. }
  62. return %args;
  63. }
  64. sub _api_request ( $self, %args ) {
  65. %args = Playwright::Base::_coerce( $self->spec(), %args );
  66. return Playwright::Util::async( sub { &Playwright::Base::_do( $self, %args ) } )
  67. if $args{command} =~ m/^waitFor/;
  68. my $msg = Playwright::Base::_do->( $self, %args );
  69. if ( ref $msg eq 'ARRAY' ) {
  70. @$msg = map {
  71. my $subject = $_;
  72. $subject = $Playwright::mapper{ $_->{_type} }->( $self, $_ )
  73. if ( ref $_ eq 'HASH' )
  74. && $_->{_type}
  75. && exists $Playwright::mapper{ $_->{_type} };
  76. $subject
  77. } @$msg;
  78. }
  79. return $Playwright::mapper{ $msg->{_type} }->( $self, $msg )
  80. if ( ref $msg eq 'HASH' )
  81. && $msg->{_type}
  82. && exists $Playwright::mapper{ $msg->{_type} };
  83. return $msg;
  84. }
  85. sub _do ( $self, %args ) {
  86. return Playwright::Util::request( 'POST', 'command', $self->{port},
  87. $self->{ua}, %args );
  88. }
  89. sub spec {
  90. return $Playwright::spec;
  91. }
  92. 1;