AnyEvent-WebDriver
view release on metacpan or search on metacpan
Same examples using aliases provided by this module:
$element = $wd->find_element (css => "body a");
$element = $wd->find_element (link => "Click Here For Porn");
$element = $wd->find_element (substr => "orn");
$element = $wd->find_element (tag => "input");
$elements = $wd->find_elements ($locator_strategy, $selector)
As above, but returns an arrayref of all found element objects.
$element = $wd->find_element_from_element ($element, $locator_strategy,
$selector)
Like "find_element", but looks only inside the specified $element.
$elements = $wd->find_elements_from_element ($element,
$locator_strategy, $selector)
Like "find_elements", but looks only inside the specified $element.
my $head = $wd->find_element ("tag name" => "head");
my $links = $wd->find_elements_from_element ($head, "tag name", "link");
$element = $wd->get_active_element
Returns the active element.
ELEMENT STATE
$bool = $wd->is_element_selected
Returns whether the given input or option element is selected or
not.
$string = $wd->get_element_attribute ($element, $name)
Returns the value of the given attribute.
$string = $wd->get_element_property ($element, $name)
Returns the value of the given property.
$string = $wd->get_element_css_value ($element, $name)
Returns the value of the given CSS value.
$string = $wd->get_element_text ($element)
Returns the (rendered) text content of the given element.
$string = $wd->get_element_tag_name ($element)
Returns the tag of the given element.
$rect = $wd->get_element_rect ($element)
Returns the element rect(angle) of the given element.
$bool = $wd->is_element_enabled
Returns whether the element is enabled or not.
ELEMENT INTERACTION
$wd->element_click ($element)
Clicks the given element.
$wd->element_clear ($element)
Clear the contents of the given element.
$wd->element_send_keys ($element, $text)
Sends the given text as key events to the given element. Key input
state can be cleared by embedding "\x{e000}" in $text. Presumably,
you can embed modifiers using their unicode codepoints, but the
specification is less than clear to mein this area.
DOCUMENT HANDLING
$source = $wd->get_page_source
Returns the (HTML/XML) page source of the current document.
$results = $wd->execute_script ($javascript, $args)
Synchronously execute the given script with given arguments and
return its results ($args can be "undef" if no arguments are
wanted/needed).
$ten = $wd->execute_script ("return arguments[0]+arguments[1]", [3, 7]);
$results = $wd->execute_async_script ($javascript, $args)
Similar to "execute_script", but doesn't wait for script to return,
but instead waits for the script to call its last argument, which is
added to $args automatically.
$twenty = $wd->execute_async_script ("arguments[0](20)", undef);
COOKIES
$cookies = $wd->get_all_cookies
Returns all cookies, as an arrayref of hashrefs.
# google surely sets a lot of cookies without my consent
$wd->navigate_to ("http://google.com");
use Data::Dump;
ddx $wd->get_all_cookies;
$cookie = $wd->get_named_cookie ($name)
Returns a single cookie as a hashref.
$wd->add_cookie ($cookie)
Adds the given cookie hashref.
$wd->delete_cookie ($name)
Delete the named cookie.
$wd->delete_all_cookies
Delete all cookies.
ACTIONS
$wd->perform_actions ($actions)
Perform the given actions (an arrayref of action specifications
simulating user activity, or an "AnyEvent::WebDriver::Actions"
object). For further details, read the spec or the section "ACTION
LISTS", below.
An example to get you started (see the next example for a mostly
equivalent example using the "AnyEvent::WebDriver::Actions" helper
API):
$wd->navigate_to ("https://duckduckgo.com/html");
my $input = $wd->find_element ("css selector", 'input[type="text"]');
$wd->perform_actions ([
{
id => "myfatfinger",
type => "pointer",
pointerType => "touch",
actions => [
Example: switch default keyboard source to "kbd1", assuming it is of
"key" class.
$al->source ("kbd1");
$al = $al->pause ($duration)
Creates a pause with the given duration. Makes sure that time
progresses in any case, even when $duration is 0.
$al = $al->pointer_down ($button, $source)
$al = $al->pointer_up ($button, $source)
Press or release the given button. $button defaults to 0.
$al = $al->click ($button, $source)
Convenience function that creates a button press and release action
without any delay between them. $button defaults to 0.
$al = $al->doubleclick ($button, $source)
Convenience function that creates two button press and release
action pairs in a row, with no unnecessary delay between them.
$button defaults to 0.
$al = $al->move ($origin, $x, $y, $duration, $source)
Moves a pointer to the given position, relative to origin (either
"viewport", "pointer" or an element object. The coordinates will be
truncated to integer values.
$al = $al->cancel ($source)
Executes a pointer cancel action.
$al = $al->key_down ($key, $source)
$al = $al->key_up ($key, $source)
Press or release the given key.
$al = $al->key ($key, $source)
Peess and release the given key in one go, without unnecessary
delay.
A special syntax, "{keyname}" can be used for special keys - all the
special key names from the second table in section 17.4.2
<https://www.w3.org/TR/webdriver1/#keyboard-actions> of the
WebDriver recommendation can be used - prefix with "Shift-Space". to
get the shifted version, as in "Shift-"
Example: press and release "a".
$al->key ("a");
Example: press and release the "Enter" key:
$al->key ("\x{e007}");
Example: press and release the "enter" key using the special key
name syntax:
$al->key ("{Enter}");
$al = $al->type ($string, $source)
Convenience method to simulate a series of key press and release
events for the keys in $string, one pair per extended unicode
grapheme cluster. There is no syntax for special keys, everything
will be typed "as-is" if possible.
$al->perform ($wd)
Finalises and compiles the list, if not done yet, and calls
"$wd->perform" with it.
If $wd is undef, and the action list was created using the
"$wd->actions" method, then perform it against that WebDriver
object.
There is no underscore variant - call the "perform_actions_" method
with the action object instead.
$al->perform_release ($wd)
Exactly like "perform", but additionally call "release_actions"
afterwards.
($actions, $duration) = $al->compile
Finalises and compiles the list, if not done yet, and returns an
actions object suitable for calls to "$wd->perform_actions". When
called in list context, additionally returns the total duration of
the action list.
Since building large action lists can take nontrivial amounts of
time, it can make sense to build an action list only once and then
perform it multiple times.
No additional actions must be added after compiling an action list.
EVENT BASED API
This module wouldn't be a good AnyEvent citizen if it didn't have a true
event-based API.
In fact, the simplified API, as documented above, is emulated via the
event-based API and an "AUTOLOAD" function that automatically provides
blocking wrappers around the callback-based API.
Every method documented in the "SIMPLIFIED API" section has an
equivalent event-based method that is formed by appending a underscore
("_") to the method name, and appending a callback to the argument list
(mnemonic: the underscore indicates the "the action is not yet finished"
after the call returns).
For example, instead of a blocking calls to "new_session", "navigate_to"
and "back", you can make a callback-based ones:
my $cv = AE::cv;
$wd->new_session ({}, sub {
my ($status, $value) = @_,
die "error $value->{error}" if $status ne "200";
$wd->navigate_to_ ("http://www.nethype.de", sub {
$wd->back_ (sub {
print "all done\n";
$cv->send;
});
( run in 0.503 second using v1.01-cache-2.11-cpan-39bf76dae61 )