Playwright.pm 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 File::Which();
  13. use Capture::Tiny qw{capture_stderr};
  14. use Carp qw{confess};
  15. use Playwright::Base();
  16. use Playwright::Util();
  17. #ABSTRACT: Perl client for Playwright
  18. no warnings 'experimental';
  19. use feature qw{signatures state};
  20. =head1 SYNOPSIS
  21. use JSON::PP;
  22. use Playwright;
  23. my $handle = Playwright->new();
  24. my $browser = $handle->launch( headless => JSON::PP::false, type => 'chrome' );
  25. my $page = $browser->newPage();
  26. my $res = $page->goto('http://google.com', { waitUntil => 'networkidle' });
  27. my $frameset = $page->mainFrame();
  28. my $kidframes = $frameset->childFrames();
  29. =head1 DESCRIPTION
  30. Perl interface to a lightweight node.js webserver that proxies commands runnable by Playwright.
  31. Checks and automatically installs a copy of the node dependencies in the local folder if needed.
  32. Currently understands commands you can send to all the playwright classes defined in api.json.
  33. See L<https://playwright.dev/#version=master&path=docs%2Fapi.md&q=>
  34. for what the classes do, and their usage.
  35. There are two major exceptions in how things work versus the documentation.
  36. =head2 Selectors
  37. The selector functions have to be renamed from starting with $ for obvious reasons.
  38. The renamed functions are as follows:
  39. =over 4
  40. =item $ => select
  41. =item $$ => selectMulti
  42. =item $eval => eval
  43. =item $$eval => evalMulti
  44. =back
  45. These functions are present as part of the Page, Frame and ElementHandle classes.
  46. =head2 Scripts
  47. The evaluate() and evaluateHandle() functions can only be run in string mode.
  48. To maximize the usefulness of these, I have wrapped the string passed with the following function:
  49. const fun = new Function (toEval);
  50. args = [
  51. fun,
  52. ...args
  53. ];
  54. As such you can effectively treat the script string as a function body.
  55. The same restriction on only being able to pass one arg remains from the upstream:
  56. L<https://playwright.dev/#version=master&path=docs%2Fapi.md&q=pageevaluatepagefunction-arg>
  57. You will have to refer to the arguments array as described here:
  58. L<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments>
  59. =head2 Asynchronous operations
  60. The waitFor* methods defined on various classes will return an instance of L<AsyncData>, a part of the L<Async> module.
  61. You will then need to wait on the result of the backgrounded action with the await() method documented below.
  62. # Assuming $handle is a Playwright object
  63. my $async = $page->waitForEvent('console');
  64. $page->evaluate('console.log("whee")');
  65. my $result = $handle->await( $async );
  66. my $logged = $result->text();
  67. =head1 CONSTRUCTOR
  68. =head2 new(HASH) = (Playwright)
  69. Creates a new browser and returns a handle to interact with it.
  70. =head3 INPUT
  71. debug (BOOL) : Print extra messages from the Playwright server process
  72. =cut
  73. our ($spec, $server_bin, $node_bin, %mapper, %methods_to_rename);
  74. BEGIN {
  75. my $path2here = File::Basename::dirname(Cwd::abs_path($INC{'Playwright.pm'}));
  76. my $specfile = "$path2here/../api.json";
  77. confess("Can't locate Playwright specification in '$specfile'!") unless -f $specfile;
  78. my $spec_raw = File::Slurper::read_text($specfile);
  79. my $decoder = JSON::MaybeXS->new();
  80. $spec = $decoder->decode($spec_raw);
  81. $mapper{mouse} = sub { my ($self, $res) = @_; return Playwright::Mouse->new( handle => $self, id => $res->{_guid}, type => 'Mouse' ) };
  82. $mapper{keyboard} = sub { my ($self, $res) = @_; return Playwright::Keyboard->new( handle => $self, id => $res->{_guid}, type => 'Keyboard' ) };
  83. %methods_to_rename = (
  84. '$' => 'select',
  85. '$$' => 'selectMulti',
  86. '$eval' => 'eval',
  87. '$$eval' => 'evalMulti',
  88. );
  89. foreach my $class (keys(%$spec)) {
  90. $mapper{$class} = sub {
  91. my ($self, $res) = @_;
  92. my $class = "Playwright::$class";
  93. return $class->new( handle => $self, id => $res->{_guid}, type => $class );
  94. };
  95. #All of the Playwright::* Classes are made by this MAGIC
  96. Sub::Install::install_sub({
  97. code => sub ($classname,%options) {
  98. @class::ISA = qw{Playwright::Base};
  99. $options{type} = $class;
  100. return Playwright::Base::new($classname,%options);
  101. },
  102. as => 'new',
  103. into => "Playwright::$class",
  104. });
  105. # Hack in mouse and keyboard objects for the Page class
  106. if ($class eq 'Page') {
  107. foreach my $hid (qw{keyboard mouse}) {
  108. Sub::Install::install_sub({
  109. code => sub {
  110. my $self = shift;
  111. $Playwright::mapper{$hid}->($self, { _type => $self->{type}, _guid => $self->{guid} }) if exists $Playwright::mapper{$hid};
  112. },
  113. as => $hid,
  114. into => "Playwright::$class",
  115. });
  116. }
  117. }
  118. # Install the subroutines if they aren't already
  119. foreach my $method ((keys(%{$spec->{$class}{members}}), 'on')) {
  120. next if grep { $_ eq $method } qw{keyboard mouse};
  121. my $renamed = exists $methods_to_rename{$method} ? $methods_to_rename{$method} : $method;
  122. Sub::Install::install_sub({
  123. code => sub {
  124. my $self = shift;
  125. Playwright::Base::_request($self, args => [@_], command => $method, object => $self->{guid}, type => $self->{type} );
  126. },
  127. as => $renamed,
  128. into => "Playwright::$class",
  129. });
  130. }
  131. }
  132. # Make sure it's possible to start the server
  133. $server_bin = "$path2here/../bin/playwright.js";
  134. confess("Can't locate Playwright server in '$server_bin'!") unless -f $specfile;
  135. #TODO make this portable with File::Which etc
  136. # Check that node and npm are installed
  137. $node_bin = File::Which::which('node');
  138. confess("node must exist and be executable") unless -x $node_bin;
  139. # Check for the necessary modules, this relies on package.json
  140. my $npm_bin = File::Which::which('npm');
  141. confess("npm must exist and be executable") unless -x $npm_bin;
  142. my $dep_raw;
  143. capture_stderr { $dep_raw = qx{$npm_bin list --json} };
  144. confess("Could not list available node modules!") unless $dep_raw;
  145. chomp $dep_raw;
  146. my $deptree = $decoder->decode($dep_raw);
  147. my @deps = map { $deptree->{dependencies}{$_} } keys(%{$deptree->{dependencies}});
  148. if ( grep { $_->{missing} } @deps ) {
  149. my $err = capture_stderr { qx{npm i} };
  150. my $exit = $? >> 8;
  151. # Ignore failing for bogus reasons
  152. if ($err !~ m/package-lock/) {
  153. confess("Error installing node dependencies:\n$err") unless $exit;
  154. }
  155. }
  156. }
  157. sub new ($class, %options) {
  158. #XXX yes, this is a race, so we need retries in _start_server
  159. my $port = Net::EmptyPort::empty_port();
  160. my $self = bless({
  161. ua => $options{ua} // LWP::UserAgent->new(),
  162. port => $port,
  163. debug => $options{debug},
  164. pid => _start_server( $port, $options{debug}),
  165. parent => $$,
  166. }, $class);
  167. return $self;
  168. }
  169. =head1 METHODS
  170. =head2 launch(HASH) = Playwright::Browser
  171. The Argument hash here is essentially those you'd see from browserType.launch(). See:
  172. L<https://playwright.dev/#version=v1.5.1&path=docs%2Fapi.md&q=browsertypelaunchoptions>
  173. There is an additional "special" argument, that of 'type', which is used to specify what type of browser to use, e.g. 'firefox'.
  174. =cut
  175. sub launch ($self, %args) {
  176. Playwright::Base::_coerce($spec->{BrowserType}{members}, args => [\%args], command => 'launch' );
  177. delete $args{command};
  178. my $msg = Playwright::Util::request ('POST', 'session', $self->{port}, $self->{ua}, type => delete $args{type}, args => [\%args] );
  179. return $Playwright::mapper{$msg->{_type}}->($self,$msg) if (ref $msg eq 'HASH') && $msg->{_type} && exists $Playwright::mapper{$msg->{_type}};
  180. return $msg;
  181. }
  182. =head2 await (AsyncData) = Object
  183. Waits for an asynchronous operation returned by the waitFor* methods to complete and returns the value.
  184. =cut
  185. sub await ($self, $promise) {
  186. confess("Input must be an AsyncData") unless $promise->isa('AsyncData');
  187. my $obj = $promise->result(1);
  188. my $class = "Playwright::$obj->{_type}";
  189. return $obj unless $class;
  190. return $class->new( type => $obj->{_type}, id => $obj->{_guid}, handle => $self );
  191. }
  192. =head2 quit, DESTROY
  193. Terminate the browser session and wait for the Playwright server to terminate.
  194. Automatically called when the Playwright object goes out of scope.
  195. =cut
  196. sub quit ($self) {
  197. #Prevent destructor from firing in child processes so we can do things like async()
  198. return unless $$ == $self->{parent};
  199. Playwright::Util::request ('GET', 'shutdown', $self->{port}, $self->{ua} );
  200. return waitpid($self->{pid},0);
  201. }
  202. sub DESTROY ($self) {
  203. $self->quit();
  204. }
  205. sub _start_server($port, $debug) {
  206. $debug = $debug ? '-d' : '';
  207. $ENV{DEBUG} = 'pw:api' if $debug;
  208. my $pid = fork // confess("Could not fork");
  209. if ($pid) {
  210. print "Waiting for port to come up..." if $debug;
  211. Net::EmptyPort::wait_port($port,30) or confess("Server never came up after 30s!");
  212. print "done\n" if $debug;
  213. return $pid;
  214. }
  215. exec( $node_bin, $server_bin, "-p", $port, $debug);
  216. }
  217. 1;