Base.pm 2.1 KB

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