Base.pm 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 AUTOLOAD {
  27. our $AUTOLOAD;
  28. my $method = $AUTOLOAD;
  29. my ($self,@args) = @_;
  30. return Playwright::Base::_request($self, args => [@args], command => $method, object => $self->{guid}, type => $self->{type} );
  31. }
  32. sub new ($class, %options) {
  33. my $self = bless({
  34. spec => $Playwright::spec->{$options{type}}{members},
  35. type => $options{type},
  36. guid => $options{id},
  37. ua => $options{handle}{ua},
  38. port => $options{handle}{port},
  39. }, $class);
  40. return ($self);
  41. }
  42. sub _coerce($spec,%args) {
  43. #Coerce bools correctly
  44. my @argspec = values(%{$spec->{$args{command}}{args}});
  45. @argspec = sort { $a->{order} <=> $b->{order} } @argspec;
  46. for (my $i=0; $i < scalar(@argspec); $i++) {
  47. next unless $i < @{$args{args}};
  48. my $arg = $args{args}[$i];
  49. my $type = $argspec[$i]->{type};
  50. if ($type->{name} eq 'boolean') {
  51. my $truthy = int(!!$arg);
  52. $args{args}[$i] = $truthy ? JSON::true : JSON::false;
  53. } elsif ($type->{name} eq 'Object' ) {
  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 _request ($self, %args) {
  65. %args = Playwright::Base::_coerce($self->{spec},%args);
  66. return AsyncData->new( sub { &Playwright::Base::_do($self, %args) }) if $args{command} =~ m/^waitFor/;
  67. my $msg = Playwright::Base::_do->($self,%args);
  68. if (ref $msg eq 'ARRAY') {
  69. @$msg = map {
  70. my $subject = $_;
  71. $subject = $Playwright::mapper{$_->{_type}}->($self,$_) if (ref $_ eq 'HASH') && $_->{_type} && exists $Playwright::mapper{$_->{_type}};
  72. $subject
  73. } @$msg;
  74. }
  75. return $Playwright::mapper{$msg->{_type}}->($self,$msg) if (ref $msg eq 'HASH') && $msg->{_type} && exists $Playwright::mapper{$msg->{_type}};
  76. return $msg;
  77. }
  78. sub _do ($self, %args) {
  79. return Playwright::Util::request ('POST', 'command', $self->{port}, $self->{ua}, %args);
  80. }
  81. 1;