AnyEvent-WebDriver

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN

    Unless otherwise stated, all delays and time differences in this module
    are represented as an integer number of milliseconds, which is perhaps
    surprising to users of my other modules but is what the WebDriver spec
    uses.

  WEBDRIVER OBJECTS
    new AnyEvent::WebDriver key => value...
        Create a new WebDriver object. Example for a remote WebDriver
        connection (the only type supported at the moment):

           my $wd = new AnyEvent::WebDriver endpoint => "http://localhost:4444";

        Supported keys are:

        endpoint => $string
            For remote connections, the endpoint to connect to (defaults to
            "http://localhost:4444").

        proxy => $proxyspec
            The proxy to use (same as the "proxy" argument used by
            AnyEvent::HTTP). The default is "undef", which disables proxies.
            To use the system-provided proxy (e.g. "http_proxy" environment
            variable), specify the string "default".

        autodelete => $boolean
            If true (the default), then automatically execute

README  view on Meta::CPAN

            requests (the safe default of AnyEvent::HTTP) by setting this to
            "undef".

    $al = $wd->actions
        Creates an action list associated with this WebDriver. See ACTION
        LISTS, below, for full details.

    $sessionstring = $wd->save_session
        Save the current session in a string so it can be restored load with
        "load_session". Note that only the session data itself is stored
        (currently the session id and capabilities), not the endpoint
        information itself.

        The main use of this function is in conjunction with disabled
        "autodelete", to save a session to e.g., and restore it later. It
        could presumably used for other applications, such as using the same
        session from multiple processes and so on.

    $wd->load_session ($sessionstring)
    $wd->set_session ($sessionid, $capabilities)
        Starts using the given session, as identified by $sessionid.

README  view on Meta::CPAN


        No session-dependent methods must be called before this function
        returns successfully, and only one session can be created per
        WebDriver object.

        On success, "$wd->{sid}" is set to the session ID, and
        "$wd->{capabilities}" is set to the returned capabilities.

        Simple example of creating a WebDriver object and a new session:

           my $wd = new AnyEvent::WebDriver endpoint => "http://localhost:4444";
           $wd->new_session ({});

        Real-world example with capability negotiation:

           $wd->new_session ({
              capabilities => {
                 alwaysMatch => {
                    pageLoadStrategy        => "eager",
                    unhandledPromptBehavior => "dismiss",
                    # proxy => { proxyType => "manual", httpProxy => "1.2.3.4:56", sslProxy => "1.2.3.4:56" },

README  view on Meta::CPAN

    All the simplified API methods are very thin wrappers around WebDriver
    commands of the same name. They are all implemented in terms of the
    low-level methods ("req", "get", "post" and "delete"), which exist in
    blocking and callback-based variants ("req_", "get_", "post_" and
    "delete_").

    Examples are after the function descriptions.

    $wd->req_ ($method, $uri, $body, $cb->($status, $value))
    $value = $wd->req ($method, $uri, $body)
        Appends the $uri to the "endpoint/session/{sessionid}/" URL and
        makes a HTTP $method request ("GET", "POST" etc.). "POST" requests
        can provide a UTF-8-encoded JSON text as HTTP request body, or the
        empty string to indicate no body is used.

        For the callback version, the callback gets passed the HTTP status
        code (200 for every successful request), and the value of the
        "value" key in the JSON response object as second argument.

    $wd->get_ ($uri, $cb->($status, $value))
    $value = $wd->get ($uri)

WebDriver.pm  view on Meta::CPAN

      $func_->(@_, my $cv = AE::cv);
      my ($status, $res) = $cv->recv;

      if ($status ne "200") {
         my $msg;

         if (exists $res->{error}) {
            $msg = "AyEvent::WebDriver: $res->{error}: $res->{message}";
            $msg .= "\n$res->{stacktrace}caught at" if length $res->{stacktrace};
         } else {
            $msg = "AnyEvent::WebDriver: http status $status (wrong endpoint?), caught";
         }

         Carp::croak $msg;
      }

      $res
   };

   goto &$name;
}

=head2 WEBDRIVER OBJECTS

=over

=item new AnyEvent::WebDriver key => value...

Create a new WebDriver object. Example for a remote WebDriver connection
(the only type supported at the moment):

   my $wd = new AnyEvent::WebDriver endpoint => "http://localhost:4444";

Supported keys are:

=over

=item endpoint => $string

For remote connections, the endpoint to connect to (defaults to C<http://localhost:4444>).

=item proxy => $proxyspec

The proxy to use (same as the C<proxy> argument used by
L<AnyEvent::HTTP>). The default is C<undef>, which disables proxies. To
use the system-provided proxy (e.g. C<http_proxy> environment variable),
specify the string C<default>.

=item autodelete => $boolean

WebDriver.pm  view on Meta::CPAN

safe default of L<AnyEvent::HTTP>) by setting this to C<undef>.

=back

=cut

sub new {
   my ($class, %kv) = @_;

   bless {
      endpoint   => "http://localhost:4444",
      proxy      => undef,
      persistent => 1,
      autodelete => 1,
      timeout    => 300,
      %kv,
   }, $class
}

sub DESTROY {
   my ($self) = @_;

WebDriver.pm  view on Meta::CPAN

=cut

sub actions {
   AnyEvent::WebDriver::Actions->new (wd => $_[0])
}

=item $sessionstring = $wd->save_session

Save the current session in a string so it can be restored load with
C<load_session>. Note that only the session data itself is stored
(currently the session id and capabilities), not the endpoint information
itself.

The main use of this function is in conjunction with disabled
C<autodelete>, to save a session to e.g., and restore it later. It could
presumably used for other applications, such as using the same session
from multiple processes and so on.

=item $wd->load_session ($sessionstring)

=item $wd->set_session ($sessionid, $capabilities)

WebDriver.pm  view on Meta::CPAN


   $self->set_session ($session->[1], $session->[2]);
}

sub set_session {
   my ($self, $sid, $caps) = @_;

   $self->{sid}          = $sid;
   $self->{capabilities} = $caps;

   $self->{_ep} = "$self->{endpoint}/session/$self->{sid}/";
}

=back

=head2 SIMPLIFIED API

This section documents the simplified API, which is really just a very
thin wrapper around the WebDriver protocol commands. They all block the
caller until the result is available (using L<AnyEvent> condvars), so must
not be called from an event loop callback - see L<EVENT BASED API> for an

WebDriver.pm  view on Meta::CPAN

(e.g. C<capabilities>).

No session-dependent methods must be called before this function returns
successfully, and only one session can be created per WebDriver object.

On success, C<< $wd->{sid} >> is set to the session ID, and C<<
$wd->{capabilities} >> is set to the returned capabilities.

Simple example of creating a WebDriver object and a new session:

   my $wd = new AnyEvent::WebDriver endpoint => "http://localhost:4444";
   $wd->new_session ({});

Real-world example with capability negotiation:

   $wd->new_session ({
      capabilities => {
         alwaysMatch => {
            pageLoadStrategy        => "eager",
            unhandledPromptBehavior => "dismiss",
            # proxy => { proxyType => "manual", httpProxy => "1.2.3.4:56", sslProxy => "1.2.3.4:56" },

WebDriver.pm  view on Meta::CPAN

If you have URLs for Safari/IE/Edge etc. capabilities, feel free to tell
me about them.

=cut

sub new_session_ {
   my ($self, $kv, $cb) = @_;

   $kv->{capabilities} ||= {}; # required by protocol

   local $self->{_ep} = "$self->{endpoint}/";
   $self->post_ (session => $kv, sub {
      my ($status, $res) = @_;

      exists $res->{capabilities}
         or $status = "500"; # blasted chromedriver

      $self->set_session ($res->{sessionId}, $res->{capabilities})
         if $status eq "200";

      $cb->($status, $res);

WebDriver.pm  view on Meta::CPAN

=cut

sub delete_session_ {
   my ($self, $cb) = @_;

   my $sid = delete $self->{sid};
   delete $self->{capoabilities};

   return unless defined $sid;

   local $self->{_ep} = "$self->{endpoint}/session/$sid";
   $self->delete_ ("" => $cb);
}

=item $timeouts = $wd->get_timeouts

Get the current timeouts, e.g.:

   my $timeouts = $wd->get_timeouts;
   => { implicit => 0, pageLoad => 300000, script => 30000 }

WebDriver.pm  view on Meta::CPAN

C<delete_>).

Examples are after the function descriptions.

=over

=item $wd->req_ ($method, $uri, $body, $cb->($status, $value))

=item $value = $wd->req ($method, $uri, $body)

Appends the C<$uri> to the C<endpoint/session/{sessionid}/> URL and makes
a HTTP C<$method> request (C<GET>, C<POST> etc.). C<POST> requests can
provide a UTF-8-encoded JSON text as HTTP request body, or the empty
string to indicate no body is used.

For the callback version, the callback gets passed the HTTP status code
(200 for every successful request), and the value of the C<value> key in
the JSON response object as second argument.

=item $wd->get_ ($uri, $cb->($status, $value))



( run in 0.434 second using v1.01-cache-2.11-cpan-b61123c0432 )