Base.pm 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. our %methods_to_rename = (
  25. '$' => 'select',
  26. '$$' => 'selectMulti',
  27. '$eval' => 'eval',
  28. '$$eval' => 'evalMulti',
  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. # Hack in mouse and keyboard objects for the Page class
  39. if ($self->{type} eq 'Page') {
  40. foreach my $hid (qw{keyboard mouse}) {
  41. Sub::Install::install_sub({
  42. code => sub {
  43. my $self = shift;
  44. $Playwright::mapper{$hid}->($self, { _type => 'Page', _guid => $self->{guid} }) if exists $Playwright::mapper{$hid};
  45. },
  46. as => $hid,
  47. into => $class,
  48. }) unless $self->can($hid);
  49. }
  50. }
  51. # Install the subroutines if they aren't already
  52. foreach my $method (keys(%{$self->{spec}})) {
  53. my $renamed = exists $methods_to_rename{$method} ? $methods_to_rename{$method} : $method;
  54. Sub::Install::install_sub({
  55. code => sub {
  56. my $self = shift;
  57. Playwright::Base::_request($self, args => [@_], command => $method, object => $self->{guid}, type => $self->{type} );
  58. },
  59. as => $renamed,
  60. into => $class,
  61. }) unless $self->can($renamed);
  62. }
  63. return ($self);
  64. }
  65. sub _request ($self, %args) {
  66. my $msg = Playwright::Util::request ('POST', 'command', $self->{port}, $self->{ua}, %args);
  67. #TODO Check spec and see if we need to coerce a bool
  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. 1;