Page.pm 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. );
  41. sub new ($class, %options) {
  42. my $self = bless({
  43. spec => $options{browser}{spec}{Page}{members},
  44. browser => $options{browser},
  45. guid => $options{page},
  46. }, $class);
  47. # Install the subroutines if they aren't already
  48. foreach my $method (keys(%{$self->{spec}})) {
  49. Sub::Install::install_sub({
  50. code => sub { _request(shift, \%transmogrify, args => [@_], command => $method, page => $self->{guid} ) },
  51. as => $method,
  52. }) unless $self->can($method);
  53. }
  54. return ($self);#, $self->mainFrame());
  55. }
  56. =head1 METHODS
  57. =head2 spec
  58. Return the relevant methods and their definitions for this module which are built dynamically from the Playwright API spec.
  59. =cut
  60. sub spec ($self) {
  61. return %{$self->{spec}};
  62. }
  63. sub _request ($self,$translator, %options) {
  64. $options{page} = $self->{guid};
  65. return $self->{browser}->_request($translator, %options);
  66. }
  67. 1;