Sfoglia il codice sorgente

Fix #13: console messages workin'

George S. Baugh 5 anni fa
parent
commit
eac8be4a31
3 ha cambiato i file con 27 aggiunte e 13 eliminazioni
  1. 3 10
      example.pl
  2. 17 1
      lib/Playwright.pm
  3. 7 2
      lib/Playwright/Base.pm

+ 3 - 10
example.pl

@@ -3,7 +3,6 @@ use warnings;
 
 use Data::Dumper;
 use JSON::PP;
-use Async;
 
 use Playwright;
 
@@ -52,17 +51,11 @@ print Dumper($result);
 # Read the console
 $page->on('console',"return [...arguments]");
 
-#XXX this is unfortunately stringifying this object
-my $proc = Async->new( sub {
-    return $page->waitForEvent('console');
-});
+my $promise = $page->waitForEvent('console');
 $page->evaluate("console.log('hug')");
-my $console_log = $proc->result(1);
+my $console_log = $handle->await( $promise );
 
-use Data::Dumper;
-print Dumper($console_log);
-
-#print "Logged to console: '".$console_log->text()."'\n";
+print "Logged to console: '".$console_log->text()."'\n";
 
 # Use a selector to find which input is visible and type into it
 # Ideally you'd use a better selector to solve this problem, but this is just showing off

+ 17 - 1
lib/Playwright.pm

@@ -5,6 +5,10 @@ use warnings;
 
 use sigtrap qw/die normal-signals/;
 
+require Exporter;
+our @ISA = qw(Exporter);
+our @EXPORT_OK = qw(await);
+
 use File::Basename();
 use Cwd();
 use LWP::UserAgent();
@@ -151,7 +155,6 @@ BEGIN {
             next if grep { $_ eq $method } qw{keyboard mouse};
             my $renamed = exists $methods_to_rename{$method} ? $methods_to_rename{$method} : $method;
 
-            print "Installing sub $renamed into Playwright::$class\n";
             Sub::Install::install_sub({
                 code => sub {
                     my $self = shift;
@@ -201,6 +204,19 @@ sub launch ($self, %args) {
     return $msg;
 }
 
+=head2 await (Playwright::Promise) = MIXED
+
+Waits for an asynchronous operation returned by the waitFor* methods to complete and returns the value.
+
+=cut
+
+sub await ($self, $promise) {
+    confess("Input must be an AsyncData") unless $promise->isa('AsyncData');
+    my $obj = $promise->result(1);
+    my $class = "Playwright::$obj->{_type}";
+    return $class->new( type => $obj->{_type}, id => $obj->{_guid}, handle => $self ); 
+}
+
 =head2 quit, DESTROY
 
 Terminate the browser session and wait for the Playwright server to terminate.

+ 7 - 2
lib/Playwright/Base.pm

@@ -5,6 +5,7 @@ use warnings;
 
 use Sub::Install();
 
+use Async;
 use Playwright::Util();
 
 #ABSTRACT: Object representing Playwright pages
@@ -59,9 +60,9 @@ sub new ($class, %options) {
 }
 
 sub _request ($self, %args) {
-    my $msg = Playwright::Util::request ('POST', 'command', $self->{port}, $self->{ua}, %args);
+    return AsyncData->new( sub { &Playwright::Base::_do($self, %args) }) if $args{command} =~ m/^waitFor/;
 
-    #TODO Check spec and see if we need to coerce a bool
+    my $msg = Playwright::Base::_do->($self,%args);
 
     if (ref $msg eq 'ARRAY') {
         @$msg = map {
@@ -74,4 +75,8 @@ sub _request ($self, %args) {
     return $msg;
 }
 
+sub _do ($self, %args) {
+    return Playwright::Util::request ('POST', 'command', $self->{port}, $self->{ua}, %args);
+}
+
 1;