Base.pm 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 SYNOPSIS
  10. use Playwright;
  11. my ($handle,$page) = Playwright->new( handle => "chrome" );
  12. $page->goto('http://www.google.com');
  13. my $handle_version = $handle->version();
  14. $handle->quit();
  15. =head2 DESCRIPTION
  16. Base class for each Playwright class.
  17. You probably shouldn't use this directly; instead use a subclass.
  18. The specification for each class can also be inspected with the 'spec' method:
  19. use Data::Dumper;
  20. my $page = Playwright::Base->new(...);
  21. print Dumper($page->spec('Page'));
  22. =head1 CONSTRUCTOR
  23. =head2 new(HASH) = (Playwright::Base)
  24. Creates a new page and returns a handle to interact with it.
  25. =head3 INPUT
  26. handle (Playwright) : Playwright object.
  27. spec (HASHREF) : Specification for the class to build.
  28. id (STRING) : _guid returned by a response from the Playwright server with the provided type.
  29. type (STRING) : Type to actually use
  30. =cut
  31. sub new ($class, %options) {
  32. my $self = bless({
  33. spec => $Playwright::spec->{$options{type}}{members},
  34. type => $options{type},
  35. guid => $options{id},
  36. ua => $options{handle}{ua},
  37. port => $options{handle}{port},
  38. }, $class);
  39. # Install the subroutines if they aren't already
  40. foreach my $method (keys(%{$self->{spec}})) {
  41. Sub::Install::install_sub({
  42. code => sub {
  43. my $self = shift;
  44. $self->_request( args => [@_], command => $method, object => $self->{guid}, type => $self->{type} )
  45. },
  46. as => $method,
  47. }) unless $self->can($method);
  48. }
  49. return ($self);
  50. }
  51. =head1 METHODS
  52. =head2 spec
  53. Return the relevant methods and their definitions for this module which are built dynamically from the Playwright API spec.
  54. =cut
  55. sub spec ($self) {
  56. return %{$self->{spec}};
  57. }
  58. sub _request ($self, %args) {
  59. my $msg = Playwright::Util::request ('POST', 'command', $self->{port}, $self->{ua}, %args);
  60. return $Playwright::mapper{$msg->{_type}}->($self,$msg) if (ref $msg eq 'HASH') && $msg->{_type} && exists $Playwright::mapper{$msg->{_type}};
  61. return $msg;
  62. }
  63. 1;