Base.pm 2.9 KB

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