Page.pm 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package Playwright::Page;
  2. use strict;
  3. use warnings;
  4. use Sub::Install();
  5. use Carp qw{confess};
  6. #ABSTRACT: Object representing Playwright pages
  7. no warnings 'experimental';
  8. use feature qw{signatures state};
  9. =head2 SYNOPSIS
  10. use Playwright;
  11. my ($browser,$page) = Playwright->new( browser => "chrome" );
  12. $page->goto('http://www.google.com');
  13. my $browser_version = $browser->version();
  14. $browser->quit();
  15. =head2 DESCRIPTION
  16. Perl interface to a lightweight node.js webserver that proxies commands runnable by Playwright in the 'Page' Class.
  17. See L<https://playwright.dev/#version=master&path=docs%2Fapi.md&q=class-page> for more information.
  18. The specification for this class can also be inspected with the 'spec' method:
  19. use Data::Dumper;
  20. my $page = Playwright::Page->new(...);
  21. print Dumper($page->spec);
  22. =head1 CONSTRUCTOR
  23. =head2 new(HASH) = (Playwright::Page)
  24. Creates a new page and returns a handle to interact with it.
  25. =head3 INPUT
  26. browser (Playwright) : Playwright object.
  27. page (STRING) : _guid returned by a response from the Playwright server with _type of 'Page'.
  28. =cut
  29. my %transmogrify = (
  30. Frame => sub {
  31. my ($self, $res) = @_;
  32. require Playwright::Frame;
  33. return Playwright::Frame->new( browser => $self, id => $res->{_guid} );
  34. },
  35. ElementHandle => sub {
  36. my ($self, $res) = @_;
  37. require Playwright::Element;
  38. return Playwright::Element->new( browser => $self, id => $res->{_guid} );
  39. },
  40. Response => sub {
  41. my ($self, $res) = @_;
  42. require Playwright::Response;
  43. return Playwright::Response->new( browser => $self, id => $res->{_guid} );
  44. },
  45. );
  46. sub new ($class, %options) {
  47. my $self = bless({
  48. spec => $options{browser}{spec}{Page}{members},
  49. browser => $options{browser},
  50. guid => $options{id},
  51. }, $class);
  52. # Install the subroutines if they aren't already
  53. foreach my $method (keys(%{$self->{spec}})) {
  54. Sub::Install::install_sub({
  55. code => sub {
  56. my $self = shift;
  57. $self->{browser}->_request( \%transmogrify, args => [@_], command => $method, page => $self->{guid} )
  58. },
  59. as => $method,
  60. }) unless $self->can($method);
  61. }
  62. return ($self);
  63. }
  64. =head1 METHODS
  65. =head2 spec
  66. Return the relevant methods and their definitions for this module which are built dynamically from the Playwright API spec.
  67. =cut
  68. sub spec ($self) {
  69. return %{$self->{spec}};
  70. }
  71. 1;