WWW-Mechanize-PhantomJS

 view release on metacpan or  search on metacpan

lib/WWW/Mechanize/PhantomJS.pm  view on Meta::CPAN

    unless ($options{pid}) {
        my @cmd= $class->build_command_line( \%options );
        $options{ kill_pid } = 1;
        if( @cmd > 1 ) {
            # We can do a proper pipe-open
            my $mode = shift @cmd;
            $options{ pid } = open $options{fh}, $mode, @cmd
                or die "Couldn't launch [@cmd]: $! / $?";
        } else {
            # We can't do a proper pipe-open, so do the single-arg open
            # in the hope that everything has been set up properly
            $options{ pid } = open $options{fh}, $cmd[0]
                or die "Couldn't launch [$cmd[0]]: $! / $?";
        };

        # Just to give PhantomJS time to start up, make sure it accepts connections
        my $wait = time + ($options{ wait } || 20);
        while ( time < $wait ) {
            my $t = time;
            my $socket = IO::Socket::INET->new(
                PeerHost => $localhost,
                PeerPort => $options{ port },
                Proto    => 'tcp',
            );
            if( $socket ) {
                close $socket;
                sleep 0.1;
                last;
            };
            sleep 0.1 if time - $t < 1;
        }
    }

    # Connect to it
    eval {
        $options{ driver } ||= Selenium::Remote::Driver->new(
            'port' => $options{ port },
            remote_server_addr => $localhost,
            auto_close => 0,
            error_handler => sub {
                #warn ref$_[0];
                #warn "<<@CARP_NOT>>";
                #warn ((caller($_))[0,1,2])
                #    for 1..4;
                local @CARP_NOT = (@CARP_NOT, ref $_[0],'Try::Tiny');
                # Reraise the error
                croak $_[1]
            },
        );
        # (Monkey)patch Selenium::Remote::Driver
        $options{ driver }->commands->get_cmds->{get}->{no_content_success}= 0;
    };

    # if PhantomJS started, but so slow or unresponsive that SRD cannot connect to it,
    # kill it manually to avoid waiting for it indefinitely
    if ( $@ ) {
        kill 9, delete $options{ pid } if $options{ kill_pid };
        die $@;
    }

     my $self= bless \%options => $class;

     $self->eval_in_phantomjs(<<'JS');
         var page= this;
         page.errors= [];
         page.alerts= [];
         page.confirms= {};
         page.onError= function(msg, trace) {
             //_log.warn("Caught JS error", msg);
             page.errors.push({ "message": msg, "trace": trace });
         };
         page.onConsoleMessage= function(msg, line, file) {
            // line and file are declared but will never be used :(
             page.errors.push({ "message": msg, "trace": [{"line":line,"file":file}] });
         };
         page.onAlert = function(msg) {
             page.alerts.push(msg);
         };
         page.onConfirm= function(msg) {
             return page.confirms[msg];
         };
JS

     $self
};

=head2 C<< $mech->phantomjs_version >>

  print $mech->phantomjs_version;

Returns the version of the PhantomJS executable that is used.

=cut

sub phantomjs_version {
    my( $self )= @_;
    $self->{phantomjs_version} ||= do {
        my $version= `$self->{ launch_exe } --version`;
        $version=~ s!\s+!!g;
        $version
    };
}

=head2 C<< $mech->ghostdriver_version >>

  print $mech->ghostdriver_version;

Returns the version of the ghostdriver script that is used.

=cut

sub ghostdriver_version {
    my( $self )= @_;
    $self->{ghostdriver_version} ||= do {
        $self->eval_in_phantomjs('return ghostdriver.version');
    };
}

=head2 C<< $mech->driver >>

    my $selenium= $mech->driver

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 1.651 second using v1.00-cache-2.02-grep-82fe00e-cpan-48ebf85a1963 )