George S. Baugh пре 4 година
родитељ
комит
f74a5cf144
5 измењених фајлова са 22 додато и 1 уклоњено
  1. 3 0
      Changes
  2. 4 0
      bin/playwright_server
  3. 1 1
      dist.ini
  4. 3 0
      lib/Playwright.pm
  5. 11 0
      lib/Playwright/Util.pm

+ 3 - 0
Changes

@@ -1,5 +1,8 @@
 Revision history for Playwright
 
+0.012 2021-08-03 TEODESIAN
+    - Automatically translate element handles passed as args objects to the playwright process.
+
 0.011 2021-07-27 TEODESIAN
     - Make no attempts whatsoever to install node deps for users, instead giving them advice how to self-service fix their problems.
 

+ 4 - 0
bin/playwright_server

@@ -160,9 +160,13 @@ app.post('/command', async (req, res) => {
 
             //XXX We have to do a bit of 'special' handling for scripts
             // This has implications for the type of scripts you can use
+            // In addition, we translate anything with a guid (previously found elements)
             if (command == 'evaluate' || command == 'evaluateHandle') {
                 var toEval = args.shift();
                 const fun = new Function (toEval);
+                args = args.map( x =>
+                    x.uuid ? objects[x.uuid] : x
+                );
                 args = [
                     fun,
                     ...args

+ 1 - 1
dist.ini

@@ -1,5 +1,5 @@
 name = Playwright
-version = 0.011
+version = 0.012
 author = George S. Baugh <george@troglodyne.net>
 license = MIT
 copyright_holder = Troglodyne LLC

+ 3 - 0
lib/Playwright.pm

@@ -137,6 +137,9 @@ L<https://playwright.dev/docs/api/class-page#pageevalselector-pagefunction-arg>
 You will have to refer to the arguments array as described here:
 L<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments>
 
+You can also pass Playwright::ElementHandle objects as returned by the select() and selectMulti() routines.
+They will be correctly translated into DOMNodes as you would get from the querySelector() javascript functions.
+
 =head3 example of evaluate()
 
     # Read the console

+ 11 - 0
lib/Playwright/Util.pm

@@ -26,6 +26,17 @@ De-duplicates request logic in the Playwright Modules.
 sub request ( $method, $url, $port, $ua, %args ) {
     my $fullurl = "http://localhost:$port/$url";
 
+    # Handle passing Playwright elements as arguments
+    if (ref $args{args} eq 'ARRAY') {
+        @{$args{args}} = map {
+            my $transformed = $_;
+            if (ref($_) eq 'Playwright::ElementHandle' ) {
+                $transformed = { uuid => $_->{guid} }
+            }
+            $transformed;
+        } @{$args{args}};
+    }
+
     my $request = HTTP::Request->new( $method, $fullurl );
     $request->header( 'Content-type' => 'application/json' );
     $request->content( JSON::MaybeXS::encode_json( \%args ) );