AnyEvent-WebDriver
view release on metacpan or search on metacpan
WebDriver.pm view on Meta::CPAN
(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)
Starts using the given session, as identified by
C<$sessionid>. C<$capabilities> should be the original session
capabilities, although the current version of this module does not make
any use of it.
The C<$sessionid> is stored in C<< $wd->{sid} >> (and could be fetched
form there for later use), while the capabilities are stored in C<<
$wd->{capabilities} >>.
=cut
sub save_session {
my ($self) = @_;
$json->encode ([1, $self->{sid}, $self->{capabilities}]);
}
sub load_session {
my ($self, $session) = @_;
$session = $json->decode ($session);
$session->[0] == 1
or Carp::croak "AnyEvent::WebDriver::load_session: session corrupted or from different version";
$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
alternative.
The method names are pretty much taken directly from the W3C WebDriver
specification, e.g. the request documented in the "Get All Cookies"
section is implemented via the C<get_all_cookies> method.
The order is the same as in the WebDriver draft at the time of this
writing, and only minimal massaging is done to request parameters and
results.
=head3 SESSIONS
=over
=cut
=item $wd->new_session ({ key => value... })
Try to connect to the WebDriver and initialize a new session with a
"new session" command, passing the given key-value pairs as value
(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" },
},
firstMatch => [
{
browserName => "firefox",
"moz:firefoxOptions" => {
binary => "firefox/firefox",
args => ["-devtools", "-headless"],
prefs => {
"dom.webnotifications.enabled" => \0,
"dom.push.enabled" => \0,
"dom.disable_beforeunload" => \1,
"browser.link.open_newwindow" => 3,
"browser.link.open_newwindow.restrictions" => 0,
"dom.popup_allowed_events" => "",
"dom.disable_open_during_load" => \1,
},
},
},
{
browserName => "chrome",
"goog:chromeOptions" => {
binary => "/bin/chromium",
args => ["--no-sandbox", "--headless"],
( run in 1.826 second using v1.01-cache-2.11-cpan-39bf76dae61 )