Explorar el Código

Responses working

George S. Baugh hace 5 años
padre
commit
7dc260d791
Se han modificado 6 ficheros con 23 adiciones y 21 borrados
  1. 1 1
      README.md
  2. 8 7
      bin/playwright.js
  3. 1 1
      example.pl
  4. 8 12
      lib/Playwright.pm
  5. 5 0
      lib/Playwright/Page.pm
  6. 0 0
      lib/Playwright/Response.pm

+ 1 - 1
README.md

@@ -11,7 +11,7 @@ Finally, a solution!
 A little node webserver written in [express][xp] is spun up which exposes the entire playwright API.
 You build a bunch of little actions to do much like action chains in Selenium, and then make 'em go whir.
 
-The best way to do this is probably using [Promise::XS][xs].
+See example.pl for usage examples.
 
 [pw]:https://github.com/microsoft/playwright
 [srd]:https://metacpan.org/pod/Selenium::Remote::Driver

+ 8 - 7
bin/playwright.js

@@ -91,7 +91,7 @@ app.post('/command', async (req, res) => {
 
 	var payload = req.body;
     var page    = payload.page;
-    var result  = payload.result;
+    var resp  = payload.result;
     var command = payload.command;
     var args    = payload.args || [];
 
@@ -101,8 +101,13 @@ app.post('/command', async (req, res) => {
         // Operate on the provided page
         const res = await pages[page][command](...args);
         result = { error : false, message : res };
-    } else if ( responses[result] && spec.Result.members[command]) {
-        const res = await responses[result][command]
+
+        if (res._type === 'Response') {
+            responses[res._guid] = res;
+        }
+
+    } else if ( responses[resp] && spec.Response.members[command]) {
+        const res = await responses[resp][command](...args);
         result = { error : false, message : res };
     } else if ( spec.Browser.members[command] || spec.BrowserContext.members[command] ) {
         const res = await browser[command](...args);
@@ -111,10 +116,6 @@ app.post('/command', async (req, res) => {
         if (command == 'newPage') {
             pages[res._guid] = res;
         }
-        if (res._type === 'Response') {
-            responses[res._guid] = res;
-        }
-
         result = { error : false, message : res };
     } else {
         result = { error : true, message : "No such page, or " + command + " is not a globally recognized command for puppeteer" };

+ 1 - 1
example.pl

@@ -7,4 +7,4 @@ use Playwright;
 my ($browser,$page) = Playwright->new( browser => 'chrome', visible => 1 );
 
 my $res = $page->goto('http://google.com', { waitUntil => 'networkidle' });
-print Dumper($res, $browser->version());
+print Dumper($res->status(), $browser->version());

+ 8 - 12
lib/Playwright.pm

@@ -43,7 +43,7 @@ commands for which can be sent via instances of the noted module
 
 =item B<Page> - L<Playwright::Page> L<https://playwright.dev/#version=v1.5.1&path=docs%2Fapi.md&q=class-page>
 
-=item B<Result> - L<Playwright::Result> L<https://playwright.dev/#version=v1.5.1&path=docs%2Fapi.md&q=class-response>
+=item B<Response> - L<Playwright::Response> L<https://playwright.dev/#version=v1.5.1&path=docs%2Fapi.md&q=class-response>
 
 =back
 
@@ -74,11 +74,6 @@ my %transmogrify = (
         require Playwright::Page;
         return Playwright::Page->new(   browser => $self, page => $res->{_guid} );
     },
-    Result => sub {
-        my ($self, $res) = @_;
-        require Playwright::Response;
-        return Playwright::Response->new( browser => $self, id   => $res->{_guid} );
-    },
 );
 
 BEGIN {
@@ -122,9 +117,7 @@ sub new ($class, %options) {
         pid     => _start_server($options{browser},$options{visible}, $port, $options{debug}),
     }, $class);
 
-    my $res = $self->_request( \%transmogrify, url => 'session' );
-    confess("Could not create new session") if $res->{error};
-
+    $self->_request( \%transmogrify, url => 'session' );
     return ($self, Playwright::Page->new( browser => $self, page => 'default' ));
 }
 
@@ -186,9 +179,12 @@ sub _request ($self, $translator, %args) {
     $request->content( JSON::MaybeXS::encode_json(\%args) );
     my $response = $self->{ua}->request($request);
     my $decoded  = JSON::MaybeXS::decode_json($response->decoded_content());
-    
-    return $translator->{$decoded->{_type}}->($self,$decoded) if $decoded->{_type} && exists $translator->{$decoded->{_type}};
-    return $decoded;
+    my $msg = $decoded->{message};
+
+    confess($msg) if $decoded->{error};
+
+    return $translator->{$msg->{_type}}->($self,$msg) if (ref $msg eq 'HASH') && $msg->{_type} && exists $translator->{$msg->{_type}};
+    return $msg;
 }
 
 1;

+ 5 - 0
lib/Playwright/Page.pm

@@ -54,6 +54,11 @@ my %transmogrify = (
         require Playwright::Element;
         return Playwright::Element->new( page => $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) {

+ 0 - 0
lib/Playwright/Result.pm → lib/Playwright/Response.pm