Playwright.pm 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 JSON::PP;
  20. use Playwright;
  21. my $handle = Playwright->new();
  22. my $browser = $handle->launch( headless => JSON::PP::false, type => 'chrome' );
  23. my $page = $browser->newPage();
  24. my $res = $page->goto('http://google.com', { waitUntil => 'networkidle' });
  25. my $frameset = $page->mainFrame();
  26. my $kidframes = $frameset->childFrames();
  27. =head2 DESCRIPTION
  28. Perl interface to a lightweight node.js webserver that proxies commands runnable by Playwright.
  29. Currently understands commands you can send to all the playwright classes defined in api.json.
  30. See L<https://playwright.dev/#version=master&path=docs%2Fapi.md&q=>
  31. for what the classes do, and their usage.
  32. There is one major exception in how things work versus the documentation.
  33. The selector functions have to be renamed from starting with $ for obvious reasons.
  34. The renamed functions are as follows:
  35. =over 4
  36. =item $ => select
  37. =item $$ => selectMulti
  38. =item $eval => eval
  39. =item $$eval => evalMulti
  40. =back
  41. These functions are present as part of the Page, Frame and ElementHandle classes.
  42. =head1 CONSTRUCTOR
  43. =head2 new(HASH) = (Playwright)
  44. Creates a new browser and returns a handle to interact with it.
  45. =head3 INPUT
  46. debug (BOOL) : Print extra messages from the Playwright server process
  47. =cut
  48. our ($spec, $server_bin, %mapper);
  49. BEGIN {
  50. my $path2here = File::Basename::dirname(Cwd::abs_path($INC{'Playwright.pm'}));
  51. my $specfile = "$path2here/../api.json";
  52. confess("Can't locate Playwright specification in '$specfile'!") unless -f $specfile;
  53. my $spec_raw = File::Slurper::read_text($specfile);
  54. my $decoder = JSON::MaybeXS->new();
  55. $spec = $decoder->decode($spec_raw);
  56. foreach my $class (keys(%$spec)) {
  57. $mapper{$class} = sub {
  58. my ($self, $res) = @_;
  59. my $class = "Playwright::$class";
  60. return $class->new( handle => $self, id => $res->{_guid}, type => $class );
  61. };
  62. #All of the Playwright::* Classes are made by this MAGIC
  63. Sub::Install::install_sub({
  64. code => sub ($classname,%options) {
  65. @class::ISA = qw{Playwright::Base};
  66. $options{type} = $class;
  67. return Playwright::Base::new($classname,%options);
  68. },
  69. as => 'new',
  70. into => "Playwright::$class",
  71. });
  72. }
  73. # Make sure it's possible to start the server
  74. $server_bin = "$path2here/../bin/playwright.js";
  75. confess("Can't locate Playwright server in '$server_bin'!") unless -f $specfile;
  76. }
  77. sub new ($class, %options) {
  78. #XXX yes, this is a race, so we need retries in _start_server
  79. my $port = Net::EmptyPort::empty_port();
  80. my $self = bless({
  81. ua => $options{ua} // LWP::UserAgent->new(),
  82. port => $port,
  83. debug => $options{debug},
  84. pid => _start_server( $port, $options{debug}),
  85. }, $class);
  86. return $self;
  87. }
  88. =head1 METHODS
  89. =head2 launch(HASH) = Playwright::Browser
  90. The Argument hash here is essentially those you'd see from browserType.launch(). See:
  91. L<https://playwright.dev/#version=v1.5.1&path=docs%2Fapi.md&q=browsertypelaunchoptions>
  92. There is an additional "special" argument, that of 'type', which is used to specify what type of browser to use, e.g. 'firefox'.
  93. =cut
  94. sub launch ($self, %args) {
  95. #TODO coerce types based on spec
  96. my $msg = Playwright::Util::request ('POST', 'session', $self->{port}, $self->{ua}, type => delete $args{type}, args => [\%args] );
  97. return $Playwright::mapper{$msg->{_type}}->($self,$msg) if (ref $msg eq 'HASH') && $msg->{_type} && exists $Playwright::mapper{$msg->{_type}};
  98. return $msg;
  99. }
  100. =head2 quit, DESTROY
  101. Terminate the browser session and wait for the Playwright server to terminate.
  102. Automatically called when the Playwright object goes out of scope.
  103. =cut
  104. sub quit ($self) {
  105. Playwright::Util::request ('GET', 'shutdown', $self->{port}, $self->{ua} );
  106. return waitpid($self->{pid},0);
  107. }
  108. sub DESTROY ($self) {
  109. $self->quit();
  110. }
  111. sub _start_server($port, $debug) {
  112. $debug = $debug ? '-d' : '';
  113. $ENV{DEBUG} = 'pw:api';
  114. my $pid = fork // confess("Could not fork");
  115. if ($pid) {
  116. print "Waiting for port to come up..." if $debug;
  117. Net::EmptyPort::wait_port($port,30) or confess("Server never came up after 30s!");
  118. print "done\n" if $debug;
  119. return $pid;
  120. }
  121. exec( $server_bin, "-p", $port, $debug);
  122. }
  123. 1;