Base.pm 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 new ($class, %options) {
  25. my $self = bless({
  26. spec => $Playwright::spec->{$options{type}}{members},
  27. type => $options{type},
  28. guid => $options{id},
  29. ua => $options{handle}{ua},
  30. port => $options{handle}{port},
  31. }, $class);
  32. # Install the subroutines if they aren't already
  33. foreach my $method (keys(%{$self->{spec}})) {
  34. Sub::Install::install_sub({
  35. code => sub {
  36. my $self = shift;
  37. Playwright::Base::_request($self, args => [@_], command => $method, object => $self->{guid}, type => $self->{type} );
  38. },
  39. as => $method,
  40. into => $class,
  41. }) unless $self->can($method);
  42. }
  43. return ($self);
  44. }
  45. sub _request ($self, %args) {
  46. my $msg = Playwright::Util::request ('POST', 'command', $self->{port}, $self->{ua}, %args);
  47. return $Playwright::mapper{$msg->{_type}}->($self,$msg) if (ref $msg eq 'HASH') && $msg->{_type} && exists $Playwright::mapper{$msg->{_type}};
  48. return $msg;
  49. }
  50. 1;