Просмотр исходного кода

Fix #8 (I think) - - add kbd/mouse interactions

George S. Baugh 5 лет назад
Родитель
Сommit
d85b327684
5 измененных файлов с 47 добавлено и 4 удалено
  1. 3 0
      .gitignore
  2. 15 4
      bin/playwright.js
  3. 12 0
      example.pl
  4. 3 0
      lib/Playwright.pm
  5. 14 0
      lib/Playwright/Base.pm

+ 3 - 0
.gitignore

@@ -45,3 +45,6 @@ inc/
 # Node stuff
 node_modules
 package-lock.json
+
+#nobody need know of our love, baby
+test.jpg

+ 15 - 4
bin/playwright.js

@@ -42,7 +42,6 @@ app.post('/session', async (req, res) => {
     var type    = payload.type;
     var args    = payload.args || [];
 
-    console.log(type,args);
 
     var result;
     if ( type && browsers[type] ) {
@@ -66,12 +65,24 @@ app.post('/command', async (req, res) => {
     var object  = payload.object;
     var command = payload.command;
     var args    = payload.args || [];
-
     var result = {};
 
-    if (objects[object] && spec[type] && spec[type].members[command]) {
+    if (argv.debug) {
+        console.log(type,object,command,args);
+    }
+
+    var subject = objects[object];
+    if (subject) {
+        if (type == 'Mouse') {
+            subject = objects[object].mouse;
+        } else if (type == 'Keyboard' ) {
+            subject = objects[object].keyboard;
+        }
+    }
+
+    if (subject && spec[type] && spec[type].members[command]) {
         try {
-            const res = await objects[object][command](...args);
+            const res = await subject[command](...args);
             result = { error : false, message : res };
 
             if (Array.isArray(res)) {

+ 12 - 0
example.pl

@@ -60,3 +60,15 @@ foreach my $input (@$inputs) {
 # Said better selector
 my $actual_input = $page->select('input[name=q]');
 $actual_input->fill('whee');
+
+# Take screen of said element
+$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');
+
+

+ 3 - 0
lib/Playwright.pm

@@ -102,6 +102,9 @@ BEGIN {
         });
     }
 
+    $mapper{mouse}    = sub { my ($self, $res) = @_; return Playwright::Mouse->new( handle => $self, id => $res->{_guid}, type => 'Mouse' ) };
+    $mapper{keyboard} = sub { my ($self, $res) = @_; return Playwright::Keyboard->new( handle => $self, id => $res->{_guid}, type => 'Keyboard' ) };
+
     # Make sure it's possible to start the server
     $server_bin = "$path2here/../bin/playwright.js";
     confess("Can't locate Playwright server in '$server_bin'!") unless -f $specfile;

+ 14 - 0
lib/Playwright/Base.pm

@@ -54,6 +54,20 @@ sub new ($class, %options) {
         port    => $options{handle}{port},
     }, $class);
 
+    # Hack in mouse and keyboard objects for the Page class
+    if ($self->{type} eq 'Page') {
+        foreach my $hid (qw{keyboard mouse}) {
+            Sub::Install::install_sub({
+                code => sub {
+                    my $self = shift;
+                    $Playwright::mapper{$hid}->($self, { _type => 'Page', _guid => $self->{guid} }) if exists $Playwright::mapper{$hid};
+                },
+                as   => $hid,
+                into => $class,
+            }) unless $self->can($hid);
+        }
+    }
+
     # Install the subroutines if they aren't already
     foreach my $method (keys(%{$self->{spec}})) {
         my $renamed = exists $methods_to_rename{$method} ? $methods_to_rename{$method} : $method;