Base.pm 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package Playwright::Base;
  2. use strict;
  3. use warnings;
  4. use Sub::Install();
  5. use Async;
  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' property:
  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. =cut
  26. sub new ( $class, %options ) {
  27. my $self = bless(
  28. {
  29. spec => $Playwright::spec->{ $options{type} }{members},
  30. type => $options{type},
  31. guid => $options{id},
  32. ua => $options{handle}{ua},
  33. port => $options{handle}{port},
  34. },
  35. $class
  36. );
  37. return ($self);
  38. }
  39. sub _coerce ( $spec, %args ) {
  40. #Coerce bools correctly
  41. my @argspec = values( %{ $spec->{ $args{command} }{args} } );
  42. @argspec = sort { $a->{order} <=> $b->{order} } @argspec;
  43. for ( my $i = 0 ; $i < scalar(@argspec) ; $i++ ) {
  44. next unless $i < @{ $args{args} };
  45. my $arg = $args{args}[$i];
  46. my $type = $argspec[$i]->{type};
  47. if ( $type->{name} eq 'boolean' ) {
  48. my $truthy = int( !!$arg );
  49. $args{args}[$i] = $truthy ? JSON::true : JSON::false;
  50. }
  51. elsif ( $type->{name} eq 'Object' ) {
  52. foreach my $prop ( keys( %{ $type->{properties} } ) ) {
  53. next unless exists $arg->{$prop};
  54. my $truthy = int( !!$arg->{$prop} );
  55. next unless $type->{properties}{$prop}{type}{name} eq 'boolean';
  56. $args{args}[$i]->{$prop} = $truthy ? JSON::true : JSON::false;
  57. }
  58. }
  59. }
  60. return %args;
  61. }
  62. sub _request ( $self, %args ) {
  63. %args = Playwright::Base::_coerce( $self->{spec}, %args );
  64. return AsyncData->new( sub { &Playwright::Base::_do( $self, %args ) } )
  65. if $args{command} =~ m/^waitFor/;
  66. my $msg = Playwright::Base::_do->( $self, %args );
  67. if ( ref $msg eq 'ARRAY' ) {
  68. @$msg = map {
  69. my $subject = $_;
  70. $subject = $Playwright::mapper{ $_->{_type} }->( $self, $_ )
  71. if ( ref $_ eq 'HASH' )
  72. && $_->{_type}
  73. && exists $Playwright::mapper{ $_->{_type} };
  74. $subject
  75. } @$msg;
  76. }
  77. return $Playwright::mapper{ $msg->{_type} }->( $self, $msg )
  78. if ( ref $msg eq 'HASH' )
  79. && $msg->{_type}
  80. && exists $Playwright::mapper{ $msg->{_type} };
  81. return $msg;
  82. }
  83. sub _do ( $self, %args ) {
  84. return Playwright::Util::request( 'POST', 'command', $self->{port},
  85. $self->{ua}, %args );
  86. }
  87. 1;