Playwright.pm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. package Playwright;
  2. use strict;
  3. use warnings;
  4. use 5.006;
  5. use v5.28.0; # Before 5.006, v5.10.0 would not be understood.
  6. use sigtrap qw/die normal-signals/;
  7. use File::pushd;
  8. use File::ShareDir();
  9. use File::Basename();
  10. use Cwd();
  11. use LWP::UserAgent();
  12. use Sub::Install();
  13. use Net::EmptyPort();
  14. use JSON::MaybeXS();
  15. use File::Slurper();
  16. use File::Which();
  17. use Capture::Tiny qw{capture capture_stderr};
  18. use Carp qw{confess};
  19. use Playwright::Base();
  20. use Playwright::Util();
  21. #ABSTRACT: Perl client for Playwright
  22. use 5.006;
  23. use v5.28.0; # Before 5.006, v5.10.0 would not be understood.
  24. no warnings 'experimental';
  25. use feature qw{signatures};
  26. =head1 SYNOPSIS
  27. use JSON::PP;
  28. use Playwright;
  29. my $handle = Playwright->new();
  30. my $browser = $handle->launch( headless => JSON::PP::false, type => 'chrome' );
  31. my $page = $browser->newPage();
  32. my $res = $page->goto('http://google.com', { waitUntil => 'networkidle' });
  33. my $frameset = $page->mainFrame();
  34. my $kidframes = $frameset->childFrames();
  35. =head1 DESCRIPTION
  36. Perl interface to a lightweight node.js webserver that proxies commands runnable by Playwright.
  37. Checks and automatically installs a copy of the node dependencies in the local folder if needed.
  38. Currently understands commands you can send to all the playwright classes defined in api.json (installed wherever your OS puts shared files for CPAN distributions).
  39. See L<https://playwright.dev/#version=master&path=docs%2Fapi.md&q=>
  40. for what the classes do, and their usage.
  41. There are two major exceptions in how things work versus the documentation.
  42. =head2 Selectors
  43. The selector functions have to be renamed from starting with $ for obvious reasons.
  44. The renamed functions are as follows:
  45. =over 4
  46. =item $ => select
  47. =item $$ => selectMulti
  48. =item $eval => eval
  49. =item $$eval => evalMulti
  50. =back
  51. These functions are present as part of the Page, Frame and ElementHandle classes.
  52. =head2 Scripts
  53. The evaluate() and evaluateHandle() functions can only be run in string mode.
  54. To maximize the usefulness of these, I have wrapped the string passed with the following function:
  55. const fun = new Function (toEval);
  56. args = [
  57. fun,
  58. ...args
  59. ];
  60. As such you can effectively treat the script string as a function body.
  61. The same restriction on only being able to pass one arg remains from the upstream:
  62. L<https://playwright.dev/#version=master&path=docs%2Fapi.md&q=pageevaluatepagefunction-arg>
  63. You will have to refer to the arguments array as described here:
  64. L<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments>
  65. =head2 Asynchronous operations
  66. The waitFor* methods defined on various classes will return an instance of L<AsyncData>, a part of the L<Async> module.
  67. You will then need to wait on the result of the backgrounded action with the await() method documented below.
  68. # Assuming $handle is a Playwright object
  69. my $async = $page->waitForEvent('console');
  70. $page->evaluate('console.log("whee")');
  71. my $result = $handle->await( $async );
  72. my $logged = $result->text();
  73. =head1 INSTALLATION NOTE
  74. If you install this module from CPAN, you will likely encounter a croak() telling you to install node module dependencies.
  75. Follow the instructions and things should be just fine.
  76. =head1 CONSTRUCTOR
  77. =head2 new(HASH) = (Playwright)
  78. Creates a new browser and returns a handle to interact with it.
  79. =head3 INPUT
  80. debug (BOOL) : Print extra messages from the Playwright server process
  81. =cut
  82. our ( $spec, $server_bin, $node_bin, %mapper, %methods_to_rename );
  83. sub _check_node {
  84. my $global_install = '';
  85. my $path2here = File::Basename::dirname( Cwd::abs_path( $INC{'Playwright.pm'} ) );
  86. my $decoder = JSON::MaybeXS->new();
  87. # Make sure it's possible to start the server
  88. $server_bin = "$path2here/../bin/playwright_server";
  89. if (!-f $server_bin ) {
  90. $server_bin = File::Which::which('playwright_server');
  91. $global_install = 1;
  92. }
  93. confess("Can't locate Playwright server in '$server_bin'!")
  94. unless -f $server_bin;
  95. # Check that node and npm are installed
  96. $node_bin = File::Which::which('node');
  97. confess("node must exist and be executable") unless -x $node_bin;
  98. # Check for the necessary modules, this relies on package.json
  99. my $npm_bin = File::Which::which('npm');
  100. confess("npm must exist and be executable") unless -x $npm_bin;
  101. my $dep_raw;
  102. {
  103. #XXX the node Depsolver is deranged, global modules DO NOT WORK
  104. my $curdir = pushd(File::Basename::dirname($server_bin));
  105. ($dep_raw) = capture { system($npm_bin, qw{list --json}) };
  106. confess("Could not list available node modules!") unless $dep_raw;
  107. chomp $dep_raw;
  108. my $deptree = $decoder->decode($dep_raw);
  109. my @needed = qw{express uuid yargs playwright};
  110. my @has = keys( %{ $deptree->{dependencies} } );
  111. my @deps = grep {my $subj=$_; grep { $_ eq $subj } @needed } @has;
  112. my $need_deps = scalar(@deps) != scalar(@needed);
  113. #This is really just for developers
  114. if ( $need_deps ) {
  115. confess("Production install of node dependencies must be done manually by nonroot users. Run the following:\n\n pushd '$curdir' && sudo npm i yargs express playwright uuid; popd\n\n") if $global_install;
  116. my $err = capture_stderr { qx{npm i} };
  117. my $exit = $? >> 8;
  118. # Ignore failing for bogus reasons
  119. if ( $err !~ m/package-lock/ ) {
  120. confess("Error installing node dependencies:\n$err") if $exit;
  121. }
  122. }
  123. }
  124. }
  125. sub _build_classes {
  126. $mapper{mouse} = sub {
  127. my ( $self, $res ) = @_;
  128. return Playwright::Mouse->new(
  129. handle => $self,
  130. id => $res->{_guid},
  131. type => 'Mouse'
  132. );
  133. };
  134. $mapper{keyboard} = sub {
  135. my ( $self, $res ) = @_;
  136. return Playwright::Keyboard->new(
  137. handle => $self,
  138. id => $res->{_guid},
  139. type => 'Keyboard'
  140. );
  141. };
  142. %methods_to_rename = (
  143. '$' => 'select',
  144. '$$' => 'selectMulti',
  145. '$eval' => 'eval',
  146. '$$eval' => 'evalMulti',
  147. );
  148. foreach my $class ( keys(%$spec) ) {
  149. $mapper{$class} = sub {
  150. my ( $self, $res ) = @_;
  151. my $class = "Playwright::$class";
  152. return $class->new(
  153. handle => $self,
  154. id => $res->{_guid},
  155. type => $class
  156. );
  157. };
  158. #All of the Playwright::* Classes are made by this MAGIC
  159. Sub::Install::install_sub(
  160. {
  161. code => sub ( $classname, %options ) {
  162. @class::ISA = qw{Playwright::Base};
  163. $options{type} = $class;
  164. return Playwright::Base::new( $classname, %options );
  165. },
  166. as => 'new',
  167. into => "Playwright::$class",
  168. }
  169. ) unless "Playwright::$class"->can('new');;
  170. # Hack in mouse and keyboard objects for the Page class
  171. if ( $class eq 'Page' ) {
  172. foreach my $hid (qw{keyboard mouse}) {
  173. Sub::Install::install_sub(
  174. {
  175. code => sub {
  176. my $self = shift;
  177. $Playwright::mapper{$hid}->(
  178. $self,
  179. {
  180. _type => $self->{type},
  181. _guid => $self->{guid}
  182. }
  183. ) if exists $Playwright::mapper{$hid};
  184. },
  185. as => $hid,
  186. into => "Playwright::$class",
  187. }
  188. ) unless "Playwright::$class"->can($hid);
  189. }
  190. }
  191. # Install the subroutines if they aren't already
  192. foreach my $method ( ( keys( %{ $spec->{$class}{members} } ), 'on' ) ) {
  193. next if grep { $_ eq $method } qw{keyboard mouse};
  194. my $renamed =
  195. exists $methods_to_rename{$method}
  196. ? $methods_to_rename{$method}
  197. : $method;
  198. Sub::Install::install_sub(
  199. {
  200. code => sub {
  201. my $self = shift;
  202. Playwright::Base::_request(
  203. $self,
  204. args => [@_],
  205. command => $method,
  206. object => $self->{guid},
  207. type => $self->{type}
  208. );
  209. },
  210. as => $renamed,
  211. into => "Playwright::$class",
  212. }
  213. ) unless "Playwright::$class"->can($renamed);
  214. }
  215. }
  216. }
  217. sub BEGIN {
  218. our $SKIP_BEGIN;
  219. _check_node() unless $SKIP_BEGIN;
  220. }
  221. sub new ( $class, %options ) {
  222. #XXX yes, this is a race, so we need retries in _start_server
  223. my $port = Net::EmptyPort::empty_port();
  224. my $self = bless(
  225. {
  226. ua => $options{ua} // LWP::UserAgent->new(),
  227. port => $port,
  228. debug => $options{debug},
  229. pid => _start_server( $port, $options{debug} ),
  230. parent => $$,
  231. },
  232. $class
  233. );
  234. $self->_check_and_build_spec();
  235. _build_classes();
  236. return $self;
  237. }
  238. sub _check_and_build_spec ($self) {
  239. return $spec if ref $spec eq 'HASH';
  240. $spec = Playwright::Util::request(
  241. 'GET', 'spec', $self->{port}, $self->{ua},
  242. );
  243. confess("Could not retrieve Playwright specification. Check that your playwright installation is correct and complete.") unless ref $spec eq 'HASH';
  244. return $spec;
  245. }
  246. =head1 METHODS
  247. =head2 launch(HASH) = Playwright::Browser
  248. The Argument hash here is essentially those you'd see from browserType.launch(). See:
  249. L<https://playwright.dev/#version=v1.5.1&path=docs%2Fapi.md&q=browsertypelaunchoptions>
  250. There is an additional "special" argument, that of 'type', which is used to specify what type of browser to use, e.g. 'firefox'.
  251. =cut
  252. sub launch ( $self, %args ) {
  253. Playwright::Base::_coerce(
  254. $spec->{BrowserType}{members},
  255. args => [ \%args ],
  256. command => 'launch'
  257. );
  258. delete $args{command};
  259. my $msg = Playwright::Util::request(
  260. 'POST', 'session', $self->{port}, $self->{ua},
  261. type => delete $args{type},
  262. args => [ \%args ]
  263. );
  264. return $Playwright::mapper{ $msg->{_type} }->( $self, $msg )
  265. if ( ref $msg eq 'HASH' )
  266. && $msg->{_type}
  267. && exists $Playwright::mapper{ $msg->{_type} };
  268. return $msg;
  269. }
  270. =head2 await (AsyncData) = Object
  271. Waits for an asynchronous operation returned by the waitFor* methods to complete and returns the value.
  272. =cut
  273. sub await ( $self, $promise ) {
  274. confess("Input must be an AsyncData") unless $promise->isa('AsyncData');
  275. my $obj = $promise->result(1);
  276. return $obj unless $obj->{_type};
  277. my $class = "Playwright::$obj->{_type}";
  278. return $class->new(
  279. type => $obj->{_type},
  280. id => $obj->{_guid},
  281. handle => $self
  282. );
  283. }
  284. =head2 quit, DESTROY
  285. Terminate the browser session and wait for the Playwright server to terminate.
  286. Automatically called when the Playwright object goes out of scope.
  287. =cut
  288. sub quit ($self) {
  289. #Prevent destructor from firing in child processes so we can do things like async()
  290. return unless $$ == $self->{parent};
  291. Playwright::Util::request( 'GET', 'shutdown', $self->{port}, $self->{ua} );
  292. return waitpid( $self->{pid}, 0 );
  293. }
  294. sub DESTROY ($self) {
  295. $self->quit();
  296. }
  297. sub _start_server ( $port, $debug ) {
  298. $debug = $debug ? '-d' : '';
  299. $ENV{DEBUG} = 'pw:api' if $debug;
  300. my $pid = fork // confess("Could not fork");
  301. if ($pid) {
  302. print "Waiting for port to come up..." if $debug;
  303. Net::EmptyPort::wait_port( $port, 30 )
  304. or confess("Server never came up after 30s!");
  305. print "done\n" if $debug;
  306. return $pid;
  307. }
  308. exec( $node_bin, $server_bin, "-p", $port, $debug );
  309. }
  310. 1;