Ver código fonte

Errors propagating properly now, gonna refactor some things now

George S. Baugh 5 anos atrás
pai
commit
4a77112c03
6 arquivos alterados com 162 adições e 40 exclusões
  1. 39 14
      bin/playwright.js
  2. 3 0
      example.pl
  3. 4 4
      lib/Playwright.pm
  4. 98 0
      lib/Playwright/Frame.pm
  5. 11 13
      lib/Playwright/Page.pm
  6. 7 9
      lib/Playwright/Response.pm

+ 39 - 14
bin/playwright.js

@@ -61,6 +61,7 @@ const port = argv.port || 6969;
 var browser;
 var pages = {};
 var responses = {};
+var frames = {};
 
 app.use(express.json())
 
@@ -91,7 +92,8 @@ app.post('/command', async (req, res) => {
 
 	var payload = req.body;
     var page    = payload.page;
-    var resp  = payload.result;
+    var resp    = payload.result;
+    var frame   = payload.frame;
     var command = payload.command;
     var args    = payload.args || [];
 
@@ -99,26 +101,49 @@ app.post('/command', async (req, res) => {
 
     if (pages[page] && spec.Page.members[command]) {
         // Operate on the provided page
-        const res = await pages[page][command](...args);
-        result = { error : false, message : res };
-
-        if (res._type === 'Response') {
-            responses[res._guid] = res;
+        try {
+            const res = await pages[page][command](...args);
+            result = { error : false, message : res };
+
+            if (res._type === 'Response') {
+                responses[res._guid] = res;
+            }
+            if (res._type === 'Frame') {
+                frames[res._guid] = res;
+            }
+        } catch (e) {
+            result = { error : true, message : e.message };
         }
 
     } else if ( responses[resp] && spec.Response.members[command]) {
-        const res = await responses[resp][command](...args);
-        result = { error : false, message : res };
+        try {
+            const res = await responses[resp][command](...args);
+            result = { error : false, message : res };
+        } catch (e) {
+            result = { error : true, message : e.message };
+        }
+    } else if ( frames[frame] && spec.Frame.members[command]) {
+        try {
+            const res = await responses[resp][command](...args);
+            result = { error : false, message : res };
+        } catch (e) {
+            result = { error : true, message : e.message };
+        }
     } else if ( spec.Browser.members[command] || spec.BrowserContext.members[command] ) {
-        const res = await browser[command](...args);
+        try {
+            const res = await browser[command](...args);
+            result = { error : false, message : res };
 
-        //File things away for our client modules to interact with
-        if (command == 'newPage') {
-            pages[res._guid] = res;
+            if (command == 'newPage') {
+                pages[res._guid] = res;
+            }
+
+        } catch (e) {
+            result = { error : true, message : e.message };
         }
-        result = { error : false, message : res };
+
     } else {
-        result = { error : true, message : "No such page, or " + command + " is not a globally recognized command for puppeteer" };
+        result = { error : true, message : "No such object, or " + command + " is not a globally recognized command for puppeteer" };
     }
 
     res.json(result);

+ 3 - 0
example.pl

@@ -8,3 +8,6 @@ my ($browser,$page) = Playwright->new( browser => 'chrome', visible => 1 );
 
 my $res = $page->goto('http://google.com', { waitUntil => 'networkidle' });
 print Dumper($res->status(), $browser->version());
+my $frameset = $page->mainFrame();
+print Dumper($frameset->{guid});
+print Dumper($frameset->childFrames());

+ 4 - 4
lib/Playwright.pm

@@ -54,9 +54,9 @@ The specification for the above classes can also be inspected with the 'spec' me
 
 =head1 CONSTRUCTOR
 
-=head2 new(HASH) = (Playwright,Playwright::Page)
+=head2 new(HASH) = (Playwright)
 
-Creates a new browser and returns a handle to interact with it, along with a new page Handle to interact with therein.
+Creates a new browser and returns a handle to interact with it.
 
 =head3 INPUT
 
@@ -72,7 +72,7 @@ my %transmogrify = (
     Page   => sub { 
         my ($self, $res) = @_;
         require Playwright::Page;
-        return Playwright::Page->new(   browser => $self, page => $res->{_guid} );
+        return Playwright::Page->new(   browser => $self, id => $res->{_guid} );
     },
 );
 
@@ -118,7 +118,7 @@ sub new ($class, %options) {
     }, $class);
 
     $self->_request( \%transmogrify, url => 'session' );
-    return ($self, Playwright::Page->new( browser => $self, page => 'default' ));
+    return ($self, Playwright::Page->new( browser => $self, id => 'default' ));
 }
 
 =head1 METHODS

+ 98 - 0
lib/Playwright/Frame.pm

@@ -0,0 +1,98 @@
+package Playwright::Frame;
+
+use strict;
+use warnings;
+
+use Sub::Install();
+use Carp qw{confess};
+
+#ABSTRACT: Object representing Playwright pages
+
+no warnings 'experimental';
+use feature qw{signatures state};
+
+=head2 SYNOPSIS
+
+    use Playwright;
+    my ($browser,$page) = Playwright->new( browser => "chrome" );
+    $page->goto('http://www.google.com');
+    my $browser_version = $browser->version();
+    $browser->quit();
+
+=head2 DESCRIPTION
+
+Perl interface to a lightweight node.js webserver that proxies commands runnable by Playwright in the 'Frame' Class.
+See L<https://playwright.dev/#version=master&path=docs%2Fapi.md&q=class-frame> for more information.
+
+The specification for this class can also be inspected with the 'spec' method:
+
+    use Data::Dumper;
+    my $page = Playwright::Page->new(...);
+    print Dumper($page->spec);
+
+=head1 CONSTRUCTOR
+
+=head2 new(HASH) = (Playwright::Frame)
+
+Creates a new page and returns a handle to interact with it, along with a Playwright::Frame (the main Frame) to interact with (supposing the page is a FrameSet).
+
+=head3 INPUT
+
+    browser (Playwright) : Playwright object.
+    page (STRING) : _guid returned by a response from the Playwright server with _type of 'Page'.
+
+=cut
+
+my %transmogrify = (
+    Frame         => sub {
+        my ($self, $res) = @_;
+        require Playwright::Frame;
+        return Playwright::Frame->new( browser => $self, id => $res->{_guid} );
+    },
+    ElementHandle => sub {
+        my ($self, $res) = @_;
+        require Playwright::Element;
+        return Playwright::Element->new( browser => $self, id    => $res->{_guid} ); 
+    },
+    Response => sub {
+        my ($self, $res) = @_;
+        require Playwright::Response;
+        return Playwright::Response->new( browser => $self, id   => $res->{_guid} );
+    },
+);
+
+sub new ($class, %options) {
+
+    my $self = bless({
+        spec    => $options{browser}{spec}{Frame}{members},
+        browser => $options{browser},
+        guid    => $options{id},
+    }, $class);
+
+    # Install the subroutines if they aren't already
+    foreach my $method (keys(%{$self->{spec}})) {
+        Sub::Install::install_sub({
+            code => sub {
+                my $self = shift;
+                $self->{browser}->_request( \%transmogrify, args => [@_], command => $method, frame => $self->{guid} )
+            },
+            as   => $method,
+        }) unless $self->can($method);
+    }
+
+    return ($self);
+}
+
+=head1 METHODS
+
+=head2 spec
+
+Return the relevant methods and their definitions for this module which are built dynamically from the Playwright API spec.
+
+=cut
+
+sub spec ($self) {
+    return %{$self->{spec}};
+}
+
+1;

+ 11 - 13
lib/Playwright/Page.pm

@@ -22,7 +22,7 @@ use feature qw{signatures state};
 =head2 DESCRIPTION
 
 Perl interface to a lightweight node.js webserver that proxies commands runnable by Playwright in the 'Page' Class.
-See L<https://playwright.dev/#version=v1.5.1&path=docs%2Fapi.md&q=class-page> for more information.
+See L<https://playwright.dev/#version=master&path=docs%2Fapi.md&q=class-page> for more information.
 
 The specification for this class can also be inspected with the 'spec' method:
 
@@ -32,9 +32,9 @@ The specification for this class can also be inspected with the 'spec' method:
 
 =head1 CONSTRUCTOR
 
-=head2 new(HASH) = (Playwright,Playwright::Frame)
+=head2 new(HASH) = (Playwright::Page)
 
-Creates a new page and returns a handle to interact with it, along with a Playwright::Frame (the main Frame) to interact with (supposing the page is a FrameSet).
+Creates a new page and returns a handle to interact with it.
 
 =head3 INPUT
 
@@ -47,12 +47,12 @@ my %transmogrify = (
     Frame         => sub {
         my ($self, $res) = @_;
         require Playwright::Frame;
-        return Playwright::Frame->new(   page => $self, frame => $res->{_guid} );
+        return Playwright::Frame->new( browser => $self, id => $res->{_guid} );
     },
     ElementHandle => sub {
         my ($self, $res) = @_;
         require Playwright::Element;
-        return Playwright::Element->new( page => $self, id    => $res->{_guid} ); 
+        return Playwright::Element->new( browser => $self, id    => $res->{_guid} ); 
     },
     Response => sub {
         my ($self, $res) = @_;
@@ -66,18 +66,21 @@ sub new ($class, %options) {
     my $self = bless({
         spec    => $options{browser}{spec}{Page}{members},
         browser => $options{browser},
-        guid    => $options{page},
+        guid    => $options{id},
     }, $class);
 
     # Install the subroutines if they aren't already
     foreach my $method (keys(%{$self->{spec}})) {
         Sub::Install::install_sub({
-            code => sub { _request(shift, \%transmogrify, args => [@_], command => $method, page => $self->{guid} ) },
+            code => sub { 
+                my $self = shift;
+                $self->{browser}->_request( \%transmogrify, args => [@_], command => $method, page => $self->{guid} )
+            },
             as   => $method,
         }) unless $self->can($method);
     }
 
-    return ($self);#, $self->mainFrame());
+    return ($self);
 }
 
 =head1 METHODS
@@ -92,9 +95,4 @@ sub spec ($self) {
     return %{$self->{spec}};
 }
 
-sub _request ($self,$translator, %options) {
-    $options{page} = $self->{guid};
-    return $self->{browser}->_request($translator, %options);
-}
-
 1;

+ 7 - 9
lib/Playwright/Response.pm

@@ -20,8 +20,8 @@ use feature qw{signatures state};
 
 =head2 DESCRIPTION
 
-Perl interface to a lightweight node.js webserver that proxies commands runnable by Playwright in the 'Page' Class.
-See L<https://playwright.dev/#version=v1.5.1&path=docs%2Fapi.md&q=class-page> for more information.
+Perl interface to a lightweight node.js webserver that proxies commands runnable by Playwright in the 'Response' Class.
+See L<https://playwright.dev/#version=master&path=docs%2Fapi.md&q=class-response> for more information.
 
 The specification for this class can also be inspected with the 'spec' method:
 
@@ -32,7 +32,7 @@ The specification for this class can also be inspected with the 'spec' method:
 
 =head1 CONSTRUCTOR
 
-=head2 new(HASH) = (Playwright,Playwright::Frame)
+=head2 new(HASH) = (Playwright::Response)
 
 Creates a new page and returns a handle to interact with it, along with a Playwright::Frame (the main Frame) to interact with (supposing the page is a FrameSet).
 
@@ -54,7 +54,10 @@ sub new ($class, %options) {
     # Install the subroutines if they aren't already
     foreach my $method (keys(%{$self->{spec}})) {
         Sub::Install::install_sub({
-            code => sub { _request(shift, undef, args => [@_], command => $method, result => $self->{guid} ) },
+            code => sub {
+                my $self = shift;
+                $self->{browser}->_request( undef, args => [@_], command => $method, result => $self->{guid} );
+            },
             as   => $method,
         }) unless $self->can($method);
     }
@@ -74,9 +77,4 @@ sub spec ($self) {
     return %{$self->{spec}};
 }
 
-sub _request ($self,$translator, %options) {
-    $options{result} = $self->{guid};
-    return $self->{browser}->_request($translator, %options);
-}
-
 1;