Playwright.pm 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 URI::Query();
  8. use Net::EmptyPort();
  9. use LWP::UserAgent();
  10. use Sub::Install();
  11. use JSON::MaybeXS();
  12. use File::Slurper();
  13. use Carp qw{confess};
  14. use Playwright::Page();
  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<Result> - L<Playwright::Result> 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,Playwright::Page)
  39. Creates a new browser and returns a handle to interact with it, along with a new page Handle to interact with therein.
  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, %class_spec);
  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. %class_spec = (
  54. %{$spec->{Browser}{members}},
  55. %{$spec->{BrowserContext}{members}}
  56. );
  57. # Install the subroutines if they aren't already
  58. foreach my $method (keys(%class_spec)) {
  59. Sub::Install::install_sub({
  60. code => sub { _request(@_) },
  61. as => $method,
  62. });
  63. }
  64. # Make sure it's possible to start the server
  65. $server_bin = "$path2here/../bin/playwright.js";
  66. confess("Can't locate Playwright server in '$server_bin'!") unless -f $specfile;
  67. 1;
  68. }
  69. sub new ($class, %options) {
  70. #XXX yes, this is a race, so we need retries in _start_server
  71. my $port = Net::EmptyPort::empty_port();
  72. my $self = bless({
  73. spec => $spec,
  74. ua => $options{ua} // LWP::UserAgent->new(),
  75. browser => $options{browser},
  76. visible => $options{visible},
  77. port => $port,
  78. debug => $options{debug},
  79. pid => _start_server($options{browser},$options{visible}, $port, $options{debug}),
  80. }, $class);
  81. return ($self, Playwright::Page->new( browser => $self, page => 'default' ));
  82. }
  83. =head1 METHODS
  84. =head2 spec
  85. Return the relevant methods and their definitions for this module which are built dynamically from the Playwright API spec.
  86. =cut
  87. sub spec ($self) {
  88. return %class_spec;
  89. }
  90. =head2 quit, DESTROY
  91. Terminate the browser session and wait for the Playwright server to terminate.
  92. Automatically called when the Playwright object goes out of scope.
  93. =cut
  94. sub quit ($self) {
  95. $self->_request( url => 'shutdown' );
  96. return waitpid($self->{pid},0);
  97. }
  98. sub DESTROY ($self) {
  99. $self->quit();
  100. }
  101. sub _start_server($browser,$visible, $port, $debug) {
  102. confess("Invalid browser '$browser'") unless grep { $_ eq $browser } qw{chrome firefox webkit};
  103. $visible = $visible ? '-v' : '';
  104. $debug = $debug ? '-d' : '';
  105. my $pid = fork // confess("Could not fork");
  106. return $pid if $pid;
  107. exec( $server_bin, $browser, $visible, "-p", $port, $debug);
  108. }
  109. my %transmogrify = (
  110. Page => sub {
  111. my ($self, $res) = @_;
  112. require Playwright::Page;
  113. return Playwright::Page->new( browser => $self, page => $res->{_guid} );
  114. },
  115. Result => sub {
  116. my ($self, $res) = @_;
  117. require Playwright::Result;
  118. return Playwright::Result->new( browser => $self, id => $res->{_guid} );
  119. },
  120. );
  121. sub _request ($self, %args) {
  122. my $qq = URI::Query->new(%args);
  123. my $url = $args{url} // 'command';
  124. my $fullurl = "http://localhost:$self->{port}/$url?$qq";
  125. my $request = HTTP::Request->new( 'GET', $fullurl );#, $header, $content );
  126. my $response = $self->{ua}->request($request);
  127. my $decoded = JSON::MaybeXS::decode_json($response->decoded_content());
  128. return $transmogrify{$decoded->{_type}}->($self,$decoded) if $decoded->{_type} && exists $transmogrify{$decoded->{_type}};
  129. return $decoded;
  130. }
  131. 1;