George S. Baugh 5 年 前
コミット
7f0d10b0b3
5 ファイル変更56 行追加65 行削除
  1. 0 20
      arg_order.js
  2. 14 1
      bin/playwright_server
  3. 21 25
      lib/Playwright.pm
  4. 0 0
      share/api.json
  5. 21 19
      t/Playwright.t

+ 0 - 20
arg_order.js

@@ -1,20 +0,0 @@
-#!/usr/bin/node
-
-"use strict";
-
-const fs = require('fs');
-
-// Defines our interface
-let rawdata = fs.readFileSync('share/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('share/api.json',JSON.stringify(spec));

+ 14 - 1
bin/playwright_server

@@ -16,6 +16,16 @@ var theFile = path.dirname(sharedir) + '/api.json';
 let rawdata = fs.readFileSync(theFile);
 let spec = JSON.parse(rawdata);
 
+// Establish argument order for callers
+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++;
+        }
+    }
+}
+
 const argv = yargs
     .option('debug', {
         alias: 'd',
@@ -42,12 +52,15 @@ var userdata = {};
 
 app.use(express.json())
 
+app.get('/spec', async (req, res) => {
+    res.json( { error : false, message : spec } );
+});
+
 app.post('/session', async (req, res) => {
     var payload = req.body;
     var type    = payload.type;
     var args    = payload.args || [];
 
-
     var result;
     if ( type && browsers[type] ) {
         try {

+ 21 - 25
lib/Playwright.pm

@@ -111,8 +111,10 @@ Creates a new browser and returns a handle to interact with it.
 
 our ( $spec, $server_bin, $node_bin, %mapper, %methods_to_rename );
 
-sub _check_node ( $path2here, $decoder ) {
+sub _check_node {
 
+    my $path2here = File::Basename::dirname( Cwd::abs_path( $INC{'Playwright.pm'} ) );
+    my $decoder  = JSON::MaybeXS->new();
     # Make sure it's possible to start the server
     $server_bin = "$path2here/../bin/playwright_server";
     $server_bin = -f $server_bin ? $server_bin : File::Which::which('playwright_server');
@@ -146,20 +148,6 @@ sub _check_node ( $path2here, $decoder ) {
     }
 }
 
-sub _check_and_build_spec {
-    my $path2here =
-      File::Basename::dirname( Cwd::abs_path( $INC{'Playwright.pm'} ) );
-    my $specfile = "$path2here/../share/api.json";
-    $specfile = -f $specfile ? $specfile : File::ShareDir::module_dir('Playwright')."/api.json";
-    confess("Can't locate Playwright specification in '$specfile'!")
-      unless -f $specfile;
-
-    my $spec_raw = File::Slurper::read_text($specfile);
-    my $decoder  = JSON::MaybeXS->new();
-    $spec = $decoder->decode($spec_raw);
-    return ( $path2here, $decoder );
-}
-
 sub _build_classes {
     $mapper{mouse} = sub {
         my ( $self, $res ) = @_;
@@ -207,7 +195,7 @@ sub _build_classes {
                 as   => 'new',
                 into => "Playwright::$class",
             }
-        );
+        ) unless "Playwright::$class"->can('new');;
 
         # Hack in mouse and keyboard objects for the Page class
         if ( $class eq 'Page' ) {
@@ -227,7 +215,7 @@ sub _build_classes {
                         as   => $hid,
                         into => "Playwright::$class",
                     }
-                );
+                ) unless "Playwright::$class"->can($hid);
             }
         }
 
@@ -254,19 +242,14 @@ sub _build_classes {
                     as   => $renamed,
                     into => "Playwright::$class",
                 }
-            );
+            ) unless "Playwright::$class"->can($renamed);
         }
     }
-
 }
 
-BEGIN {
+sub BEGIN {
     our $SKIP_BEGIN;
-    if ( !$SKIP_BEGIN ) {
-        my ( $path2here, $decoder ) = _check_and_build_spec();
-        _build_classes();
-        _check_node( $path2here, $decoder );
-    }
+    _check_node() unless $SKIP_BEGIN;
 }
 
 sub new ( $class, %options ) {
@@ -284,9 +267,22 @@ sub new ( $class, %options ) {
         $class
     );
 
+    $self->_check_and_build_spec();
+    _build_classes();
+
     return $self;
 }
 
+sub _check_and_build_spec ($self) {
+    return $spec if ref $spec eq 'HASH';
+
+    $spec = Playwright::Util::request(
+        'GET', 'spec', $self->{port}, $self->{ua},
+    );
+
+    return $spec;
+}
+
 =head1 METHODS
 
 =head2 launch(HASH) = Playwright::Browser

ファイルの差分が大きいため隠しています
+ 0 - 0
share/api.json


+ 21 - 19
t/Playwright.t

@@ -17,15 +17,15 @@ require Playwright;
 my $path2here = File::Basename::dirname(Cwd::abs_path($INC{'Playwright.pm'}));
 
 subtest "_check_and_build_spec" => sub {
-    #Simulate file not existing
-    my $json = Test::MockFile->file("$path2here/../share/api.json");
-    like( dies { Playwright::_check_and_build_spec() }, qr/specification/i, "Nonexistant api.json throws");
-
-    undef $json;
-    $json = Test::MockFile->file("$path2here/../share/api.json", '{"a":"b"}');
-    my ($path) = Playwright::_check_and_build_spec();
-    is($Playwright::spec, { a => 'b'}, "Spec parsed correctly");
-    is($path,$path2here, "Path to module built correctly");
+    local $Playwright::spec = {};
+
+    is(Playwright::_check_and_build_spec({}),{},"Already defined spec short-circuits");
+
+    my $utilmock = Test::MockModule->new('Playwright::Util');
+    $utilmock->redefine('request', sub { 'eee' });
+
+    undef $Playwright::spec;
+    is(Playwright::_check_and_build_spec({ ua => 'eeep', port => 666}),'eee',"Fetch works when spec undef");
 };
 
 subtest "_build_classes" => sub {
@@ -61,25 +61,24 @@ subtest "_build_classes" => sub {
 };
 
 subtest "_check_node" => sub {
-    my $decoder = JSON::MaybeXS->new();
+    my $which = Test::MockModule->new('File::Which');
+    $which->redefine('which', sub { "$path2here/../bin/playwright_server" });
 
     my $bin = Test::MockFile->file("$path2here/../bin/playwright_server");
-
-    like( dies { Playwright::_check_node($path2here, $decoder) }, qr/server in/i, "Server not existing throws");
+    like( dies { Playwright::_check_node() }, qr/server in/i, "Server not existing throws");
 
     undef $bin;
     $bin = Test::MockFile->file("$path2here/../bin/playwright_server",'');
 
-    my $which = Test::MockModule->new('File::Which');
     $which->redefine('which', sub { shift eq 'node' ? '/bogus' : '/hokum' });
     my $node = Test::MockFile->file('/bogus', undef, { mode => 0777 } );
     my $npm  = Test::MockFile->file('/hokum', undef, { mode => 0777 } );
 
-    like( dies { Playwright::_check_node($path2here, $decoder) }, qr/node must exist/i, "node not existing throws");
+    like( dies { Playwright::_check_node() }, qr/node must exist/i, "node not existing throws");
     undef $node;
     $node = Test::MockFile->file('/bogus', '', { mode => 0777 } );
 
-    like( dies { Playwright::_check_node($path2here, $decoder) }, qr/npm must exist/i, "npm not existing throws");
+    like( dies { Playwright::_check_node() }, qr/npm must exist/i, "npm not existing throws");
     undef $npm;
     $npm  = Test::MockFile->file('/hokum', '', { mode => 0777 } );
 
@@ -87,7 +86,7 @@ subtest "_check_node" => sub {
     $fakecapture->redefine('capture_stderr', sub { 'oh no' });
 
     $qxret = '';
-    like( dies { Playwright::_check_node($path2here, $decoder) }, qr/could not list/i, "package.json not existing throws");
+    like( dies { Playwright::_check_node() }, qr/could not list/i, "package.json not existing throws");
 
     $qxret = '{
         "name": "playwright-server-perl",
@@ -122,7 +121,7 @@ subtest "_check_node" => sub {
     #like( dies { Playwright::_check_node($path2here, $decoder) }, qr/installing node/i, "npm failure throws");
     $fakecapture->redefine('capture_stderr', sub { 'package-lock' });
     $qxcode = 0;
-    ok( lives { Playwright::_check_node($path2here, $decoder) }, "Can run all the way thru") or note $@;
+    ok( lives { Playwright::_check_node() }, "Can run all the way thru") or note $@;
 };
 
 subtest "new" => sub {
@@ -130,10 +129,13 @@ subtest "new" => sub {
     $portmock->redefine('empty_port', sub { 420 });
 
     my $lwpmock = Test::MockModule->new('LWP::UserAgent');
-    $lwpmock->redefine('new', sub { 'LWP' });
+    $lwpmock->redefine('new', sub { bless({},'LWP::UserAgent') });
+    $lwpmock->redefine('request', sub {});
 
     my $selfmock = Test::MockModule->new('Playwright');
     $selfmock->redefine('_start_server', sub { 666 });
+    $selfmock->redefine('_check_and_build_spec', sub {});
+    $selfmock->redefine('_build_classes',sub {});
     $selfmock->redefine('DESTROY', sub {});
 
     my $expected = bless({
@@ -147,7 +149,7 @@ subtest "new" => sub {
     is(Playwright->new( ua => 'whee', debug => 1), $expected, "Constructor functions as expected");
 
     $expected = bless({
-        ua     => 'LWP',
+        ua     => bless({},'LWP::UserAgent'),
         debug  => undef,
         parent => $$,
         pid    => 666,

この差分においてかなりの量のファイルが変更されているため、一部のファイルを表示していません