Bladeren bron

Now we can pass complex args!

George S. Baugh 5 jaren geleden
bovenliggende
commit
8fccc07a78
3 gewijzigde bestanden met toevoegingen van 18 en 12 verwijderingen
  1. 2 6
      bin/playwright.js
  2. 10 0
      example.pl
  3. 6 6
      lib/Playwright.pm

+ 2 - 6
bin/playwright.js

@@ -62,7 +62,6 @@ var browser;
 var pages = {};
 var responses = {};
 
-//XXX this is probably a race but I don't care yet
 app.use(express.json())
 
 app.get('/session', async (req, res) => {
@@ -88,18 +87,15 @@ app.get('/session', async (req, res) => {
     res.json({ error: false, message: 'Browser started successfully.' });
 });
 
-app.get('/command', async (req, res) => {
+app.post('/command', async (req, res) => {
 
-	var payload = req.query;
+	var payload = req.body;
     var page    = payload.page;
     var result  = payload.result;
     var command = payload.command;
     var args    = payload.args || [];
 
     var result = {};
-    if (typeof args !== 'Array') {
-        args = [args];
-    }
 
     if (pages[page] && spec.Page.members[command]) {
         // Operate on the provided page

+ 10 - 0
example.pl

@@ -0,0 +1,10 @@
+use strict;
+use warnings;
+
+use Data::Dumper;
+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());

+ 6 - 6
lib/Playwright.pm

@@ -7,7 +7,6 @@ use sigtrap qw/die normal-signals/;
 
 use File::Basename();
 use Cwd();
-use URI::Query();
 use Net::EmptyPort();
 use LWP::UserAgent();
 use Sub::Install();
@@ -124,8 +123,6 @@ sub new ($class, %options) {
     }, $class);
 
     my $res = $self->_request( \%transmogrify, url => 'session' );
-    use Data::Dumper;
-    print Dumper($res);
     confess("Could not create new session") if $res->{error};
 
     return ($self, Playwright::Page->new( browser => $self, page => 'default' ));
@@ -179,11 +176,14 @@ sub _start_server($browser,$visible, $port, $debug) {
 
 sub _request ($self, $translator, %args) {
     $translator //= \%transmogrify;
-    my $qq = URI::Query->new(%args);
     my $url = $args{url} // 'command';
-    my $fullurl = "http://localhost:$self->{port}/$url?$qq";
+    my $fullurl = "http://localhost:$self->{port}/$url";
+
+    my $method = $url eq 'command' ? 'POST' : 'GET';
 
-    my $request  = HTTP::Request->new( 'GET', $fullurl );#, $header, $content );
+    my $request  = HTTP::Request->new( $method, $fullurl);
+    $request->header( 'Content-type' => 'application/json' );
+    $request->content( JSON::MaybeXS::encode_json(\%args) );
     my $response = $self->{ua}->request($request);
     my $decoded  = JSON::MaybeXS::decode_json($response->decoded_content());