Page.pm 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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=v1.5.1&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,Playwright::Frame)
  24. Creates a new page and returns a handle to interact with it, along with a Playwright::Frame (the main Frame) to interact with (supposing the page is a FrameSet).
  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( page => $self, frame => $res->{_guid} );
  34. },
  35. ElementHandle => sub {
  36. my ($self, $res) = @_;
  37. require Playwright::Element;
  38. return Playwright::Element->new( page => $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{page},
  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 { _request(shift, \%transmogrify, args => [@_], command => $method, page => $self->{guid} ) },
  56. as => $method,
  57. }) unless $self->can($method);
  58. }
  59. return ($self);#, $self->mainFrame());
  60. }
  61. =head1 METHODS
  62. =head2 spec
  63. Return the relevant methods and their definitions for this module which are built dynamically from the Playwright API spec.
  64. =cut
  65. sub spec ($self) {
  66. return %{$self->{spec}};
  67. }
  68. sub _request ($self,$translator, %options) {
  69. $options{page} = $self->{guid};
  70. return $self->{browser}->_request($translator, %options);
  71. }
  72. 1;