Base.pm 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package Playwright::Base;
  2. use strict;
  3. use warnings;
  4. use Sub::Install();
  5. use Playwright::Util();
  6. #ABSTRACT: Object representing Playwright pages
  7. no warnings 'experimental';
  8. use feature qw{signatures};
  9. =head2 DESCRIPTION
  10. Base class for each Playwright class magic'd up by Sub::Install in Playwright's BEGIN block.
  11. You probably shouldn't use this.
  12. The specification for each class can be inspected with the 'spec' property:
  13. use Data::Dumper;
  14. my $object = Playwright::Base->new(...);
  15. print Dumper($object->{spec});
  16. =head1 CONSTRUCTOR
  17. =head2 new(HASH) = (Playwright::Base)
  18. Creates a new page and returns a handle to interact with it.
  19. =head3 INPUT
  20. handle (Playwright) : Playwright object.
  21. id (STRING) : _guid returned by a response from the Playwright server with the provided type.
  22. type (STRING) : Type to actually use
  23. =cut
  24. sub AUTOLOAD {
  25. our $AUTOLOAD;
  26. my $method = $AUTOLOAD;
  27. my ($self,@args) = @_;
  28. return Playwright::Base::_request($self, args => [@args], command => $method, object => $self->{guid}, type => $self->{type} );
  29. }
  30. sub new ($class, %options) {
  31. my $self = bless({
  32. spec => $Playwright::spec->{$options{type}}{members},
  33. type => $options{type},
  34. guid => $options{id},
  35. ua => $options{handle}{ua},
  36. port => $options{handle}{port},
  37. }, $class);
  38. return ($self);
  39. }
  40. sub _request ($self, %args) {
  41. my $msg = Playwright::Util::request ('POST', 'command', $self->{port}, $self->{ua}, %args);
  42. #TODO Check spec and see if we need to coerce a bool
  43. if (ref $msg eq 'ARRAY') {
  44. @$msg = map {
  45. my $subject = $_;
  46. $subject = $Playwright::mapper{$_->{_type}}->($self,$_) if (ref $_ eq 'HASH') && $_->{_type} && exists $Playwright::mapper{$_->{_type}};
  47. $subject
  48. } @$msg;
  49. }
  50. return $Playwright::mapper{$msg->{_type}}->($self,$msg) if (ref $msg eq 'HASH') && $msg->{_type} && exists $Playwright::mapper{$msg->{_type}};
  51. return $msg;
  52. }
  53. 1;