Playwright.pm 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package Playwright;
  2. use strict;
  3. use warnings;
  4. use sigtrap qw/die normal-signals/;
  5. use File::Basename();
  6. use Cwd();
  7. use LWP::UserAgent();
  8. use Sub::Install();
  9. use Net::EmptyPort();
  10. use JSON::MaybeXS();
  11. use File::Slurper();
  12. use Carp qw{confess};
  13. use Playwright::Base();
  14. use Playwright::Util();
  15. #ABSTRACT: Perl client for Playwright
  16. no warnings 'experimental';
  17. use feature qw{signatures state};
  18. =head2 SYNOPSIS
  19. use Playwright;
  20. my ($browser,$page) = Playwright->new( browser => "chrome" );
  21. $page->goto('http://www.google.com');
  22. my $browser_version = $browser->version();
  23. $browser->quit();
  24. =head2 DESCRIPTION
  25. Perl interface to a lightweight node.js webserver that proxies commands runnable by Playwright.
  26. Currently understands commands you can send to the following Playwright classes,
  27. commands for which can be sent via instances of the noted module
  28. =over 4
  29. =item B<Browser> - L<Playwright> L<https://playwright.dev/#version=master&path=docs%2Fapi.md&q=class-browser>
  30. =item B<BrowserContext> - L<Playwright> L<https://playwright.dev/#version=master&path=docs%2Fapi.md&q=class-browsercontext>
  31. =item B<Page> - L<Playwright::Page> L<https://playwright.dev/#version=v1.5.1&path=docs%2Fapi.md&q=class-page>
  32. =item B<Response> - L<Playwright::Response> L<https://playwright.dev/#version=v1.5.1&path=docs%2Fapi.md&q=class-response>
  33. =back
  34. The specification for the above classes can also be inspected with the 'spec' method for each respective class:
  35. use Data::Dumper;
  36. print Dumper($browser->spec , $page->spec, ...);
  37. =head1 CONSTRUCTOR
  38. =head2 new(HASH) = (Playwright)
  39. Creates a new browser and returns a handle to interact with it.
  40. =head3 INPUT
  41. browser (STRING) : Name of the browser to use. One of (chrome, firefox, webkit).
  42. visible (BOOL) : Whether to start the browser such that it displays on your desktop (headless or not).
  43. debug (BOOL) : Print extra messages from the Playwright server process
  44. =cut
  45. our ($spec, $server_bin, %mapper);
  46. BEGIN {
  47. my $path2here = File::Basename::dirname(Cwd::abs_path($INC{'Playwright.pm'}));
  48. my $specfile = "$path2here/../api.json";
  49. confess("Can't locate Playwright specification in '$specfile'!") unless -f $specfile;
  50. my $spec_raw = File::Slurper::read_text($specfile);
  51. my $decoder = JSON::MaybeXS->new();
  52. $spec = $decoder->decode($spec_raw);
  53. foreach my $class (keys(%$spec)) {
  54. $mapper{$class} = sub {
  55. my ($self, $res) = @_;
  56. my $class = "Playwright::$class";
  57. return $class->new( handle => $self, id => $res->{_guid}, type => $class );
  58. };
  59. #All of the Playwright::* Classes are made by this MAGIC
  60. Sub::Install::install_sub({
  61. code => sub ($classname,%options) {
  62. @class::ISA = qw{Playwright::Base};
  63. $options{type} = $class;
  64. return Playwright::Base::new($classname,%options);
  65. },
  66. as => 'new',
  67. into => "Playwright::$class",
  68. });
  69. }
  70. # Make sure it's possible to start the server
  71. $server_bin = "$path2here/../bin/playwright.js";
  72. confess("Can't locate Playwright server in '$server_bin'!") unless -f $specfile;
  73. }
  74. sub new ($class, %options) {
  75. #XXX yes, this is a race, so we need retries in _start_server
  76. my $port = Net::EmptyPort::empty_port();
  77. my $self = bless({
  78. ua => $options{ua} // LWP::UserAgent->new(),
  79. port => $port,
  80. debug => $options{debug},
  81. pid => _start_server( $port, $options{debug}),
  82. }, $class);
  83. return $self;
  84. }
  85. =head1 METHODS
  86. =head2 launch(HASH) = Playwright::Browser
  87. The Argument hash here is essentially those you'd see from browserType.launch(). See:
  88. L<https://playwright.dev/#version=v1.5.1&path=docs%2Fapi.md&q=browsertypelaunchoptions>
  89. There is an additional "special" argument, that of 'type', which is used to specify what type of browser to use, e.g. 'firefox'.
  90. =cut
  91. sub launch ($self, %args) {
  92. #TODO coerce types based on spec
  93. my $msg = Playwright::Util::request ('POST', 'session', $self->{port}, $self->{ua}, type => delete $args{type}, args => [\%args] );
  94. return $Playwright::mapper{$msg->{_type}}->($self,$msg) if (ref $msg eq 'HASH') && $msg->{_type} && exists $Playwright::mapper{$msg->{_type}};
  95. return $msg;
  96. }
  97. =head2 quit, DESTROY
  98. Terminate the browser session and wait for the Playwright server to terminate.
  99. Automatically called when the Playwright object goes out of scope.
  100. =cut
  101. sub quit ($self) {
  102. Playwright::Util::request ('GET', 'shutdown', $self->{port}, $self->{ua} );
  103. return waitpid($self->{pid},0);
  104. }
  105. sub DESTROY ($self) {
  106. $self->quit();
  107. }
  108. sub _start_server($port, $debug) {
  109. $debug = $debug ? '-d' : '';
  110. $ENV{DEBUG} = 'pw:api';
  111. my $pid = fork // confess("Could not fork");
  112. if ($pid) {
  113. print "Waiting for port to come up..." if $debug;
  114. Net::EmptyPort::wait_port($port,30) or confess("Server never came up after 30s!");
  115. print "done\n" if $debug;
  116. return $pid;
  117. }
  118. exec( $server_bin, "-p", $port, $debug);
  119. }
  120. 1;