| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- package Playwright::Base;
- use strict;
- use warnings;
- use Sub::Install();
- use Playwright::Util();
- #ABSTRACT: Object representing Playwright pages
- no warnings 'experimental';
- use feature qw{signatures};
- =head2 DESCRIPTION
- Base class for each Playwright class magic'd up by Sub::Install in Playwright's BEGIN block.
- You probably shouldn't use this.
- The specification for each class can be inspected with the 'spec' property:
- use Data::Dumper;
- my $object = Playwright::Base->new(...);
- print Dumper($object->{spec});
- =head1 CONSTRUCTOR
- =head2 new(HASH) = (Playwright::Base)
- Creates a new page and returns a handle to interact with it.
- =head3 INPUT
- handle (Playwright) : Playwright object.
- id (STRING) : _guid returned by a response from the Playwright server with the provided type.
- type (STRING) : Type to actually use
- =cut
- our %methods_to_rename = (
- '$' => 'select',
- '$$' => 'selectMulti',
- '$eval' => 'eval',
- '$$eval' => 'evalMulti',
- );
- sub new ($class, %options) {
- my $self = bless({
- spec => $Playwright::spec->{$options{type}}{members},
- type => $options{type},
- guid => $options{id},
- ua => $options{handle}{ua},
- port => $options{handle}{port},
- }, $class);
- # Install the subroutines if they aren't already
- foreach my $method (keys(%{$self->{spec}})) {
- my $renamed = exists $methods_to_rename{$method} ? $methods_to_rename{$method} : $method;
- Sub::Install::install_sub({
- code => sub {
- my $self = shift;
- Playwright::Base::_request($self, args => [@_], command => $method, object => $self->{guid}, type => $self->{type} );
- },
- as => $renamed,
- into => $class,
- }) unless $self->can($renamed);
- }
- return ($self);
- }
- sub _request ($self, %args) {
- my $msg = Playwright::Util::request ('POST', 'command', $self->{port}, $self->{ua}, %args);
- #TODO Check spec and see if we need to coerce a bool
- if (ref $msg eq 'ARRAY') {
- @$msg = map {
- my $subject = $_;
- $subject = $Playwright::mapper{$_->{_type}}->($self,$_) if (ref $_ eq 'HASH') && $_->{_type} && exists $Playwright::mapper{$_->{_type}};
- $subject
- } @$msg;
- }
- return $Playwright::mapper{$msg->{_type}}->($self,$msg) if (ref $msg eq 'HASH') && $msg->{_type} && exists $Playwright::mapper{$msg->{_type}};
- return $msg;
- }
- 1;
|