Преглед на файлове

Fix #6: Coerce bools, also fix wrong browser being used

George S. Baugh преди 5 години
родител
ревизия
3cffb859ab
променени са 6 файла, в които са добавени 60 реда и са изтрити 12 реда
  1. 0 0
      api.json
  2. 20 0
      arg_order.js
  3. 1 1
      bin/playwright.js
  4. 6 5
      example.pl
  5. 4 3
      lib/Playwright.pm
  6. 29 3
      lib/Playwright/Base.pm

Файловите разлики са ограничени, защото са твърде много
+ 0 - 0
api.json


+ 20 - 0
arg_order.js

@@ -0,0 +1,20 @@
+#!/usr/bin/node
+
+"use strict";
+
+const fs = require('fs');
+
+// Defines our interface
+let rawdata = fs.readFileSync('api.json');
+let spec = JSON.parse(rawdata);
+
+for (var classname of Object.keys(spec)) {
+    for (var method of Object.keys(spec[classname].members)) {
+        var order = 0;
+        for (var arg of Object.keys(spec[classname].members[method].args) ) {
+            spec[classname].members[method].args[arg].order = order++;
+        }
+    }
+}
+
+fs.writeFileSync('api.json',JSON.stringify(spec));

+ 1 - 1
bin/playwright.js

@@ -49,7 +49,7 @@ app.post('/session', async (req, res) => {
     var result;
     if ( type && browsers[type] ) {
         try {
-            var browser = await firefox.launch(...args);
+            var browser = await browsers[type].launch(...args);
             objects[browser._guid] = browser;
             result = { error : false, message : browser };
         } catch (e) {

+ 6 - 5
example.pl

@@ -2,7 +2,6 @@ use strict;
 use warnings;
 
 use Data::Dumper;
-use JSON::PP;
 
 use Playwright;
 
@@ -11,10 +10,10 @@ use Try::Tiny;
 my $handle = Playwright->new( debug => 1 );
 
 # Open a new chrome instance
-my $browser = $handle->launch( headless => JSON::PP::false, type => 'chrome' );
+my $browser = $handle->launch( headless => 0, type => 'firefox' );
 
 # Open a tab therein
-my $page = $browser->newPage({ videosPath => 'video', acceptDownloads => JSON::PP::true });
+my $page = $browser->newPage({ videosPath => 'video', acceptDownloads => 1 });
 my $bideo = $page->video;
 
 my $vidpath = $bideo->path;
@@ -79,7 +78,6 @@ $actual_input->screenshot({ path => 'test.jpg' });
 
 # Fiddle with HIDs
 my $mouse = $page->mouse;
-print "GOT HERE\n";
 $mouse->move( 0, 0 );
 my $keyboard = $page->keyboard();
 $keyboard->type('F12');
@@ -90,8 +88,9 @@ use Cwd qw{abs_path};
 my $pg = abs_path("$FindBin::Bin/at/test.html");
 
 # Handle dialogs on page start, and dialog after dialog
+# NOTE -- the 'load' event won't fire until the dialog is dismissed in some browsers 
 $promise = $page->waitForEvent('dialog');
-$page->goto("file://$pg");
+$page->goto("file://$pg", { waitUntil => 'networkidle' });
 
 my $dlg = $handle->await($promise);
 $promise = $page->waitForEvent('dialog');
@@ -100,6 +99,8 @@ $dlg = $handle->await($promise);
 $dlg->accept();
 
 # Download stuff -- note this requries acceptDownloads = true in the page open
+# NOTE -- the 'download' event fires unreliably, as not all browsers properly obey the 'download' property in hrefs.
+# Chrome, for example would choke here on an intermediate dialog.
 $promise = $page->waitForEvent('download');
 $page->select('#d-lo')->click();
 

+ 4 - 3
lib/Playwright.pm

@@ -205,7 +205,10 @@ There is an additional "special" argument, that of 'type', which is used to spec
 =cut
 
 sub launch ($self, %args) {
-    #TODO coerce types based on spec
+
+    Playwright::Base::_coerce($spec->{BrowserType}{members}, args => [\%args], command => 'launch' );
+    delete $args{command};
+
     my $msg = Playwright::Util::request ('POST', 'session', $self->{port}, $self->{ua}, type => delete $args{type}, args => [\%args] );
     return $Playwright::mapper{$msg->{_type}}->($self,$msg) if (ref $msg eq 'HASH') && $msg->{_type} && exists $Playwright::mapper{$msg->{_type}};
     return $msg;
@@ -220,8 +223,6 @@ Waits for an asynchronous operation returned by the waitFor* methods to complete
 sub await ($self, $promise) {
     confess("Input must be an AsyncData") unless $promise->isa('AsyncData');
     my $obj = $promise->result(1);
-    use Data::Dumper;
-    print Dumper($obj);
     my $class = "Playwright::$obj->{_type}";
     return $obj unless $class;
     return $class->new( type => $obj->{_type}, id => $obj->{_guid}, handle => $self ); 

+ 29 - 3
lib/Playwright/Base.pm

@@ -6,6 +6,7 @@ use warnings;
 use Sub::Install();
 
 use Async;
+use JSON;
 use Playwright::Util();
 
 #ABSTRACT: Object representing Playwright pages
@@ -59,14 +60,39 @@ sub new ($class, %options) {
     return ($self);
 }
 
+sub _coerce($spec,%args) {
+    #Coerce bools correctly
+    my @argspec = values(%{$spec->{$args{command}}{args}});
+    @argspec = sort { $a->{order} <=> $b->{order} } @argspec;
+
+    for (my $i=0; $i < scalar(@argspec); $i++) {
+        next unless $i < @{$args{args}};
+        my $arg = $args{args}[$i];
+        my $type = $argspec[$i]->{type};
+        if ($type->{name} eq 'boolean') {
+            my $truthy = int(!!$arg);
+            $args{args}[$i] = $truthy ? JSON::true : JSON::false;
+        } elsif ($type->{name} eq 'Object' ) {
+            foreach my $prop (keys(%{$type->{properties}})) {
+                next unless exists $arg->{$prop};
+                my $truthy = int(!!$arg->{$prop});
+                next unless $type->{properties}{$prop}{type}{name} eq 'boolean';
+                $args{args}[$i]->{$prop} = $truthy ? JSON::true : JSON::false;
+            }
+        }
+    }
+
+    return %args;
+}
+
 sub _request ($self, %args) {
+
+    %args = Playwright::Base::_coerce($self->{spec},%args);
+
     return AsyncData->new( sub { &Playwright::Base::_do($self, %args) }) if $args{command} =~ m/^waitFor/;
 
     my $msg = Playwright::Base::_do->($self,%args);
 
-    use Data::Dumper;
-    print Dumper($msg);
-
     if (ref $msg eq 'ARRAY') {
         @$msg = map {
             my $subject = $_;

Някои файлове не бяха показани, защото твърде много файлове са промени