NAME
Selenium::Remote::Driver - Perl Client for Selenium Remote Driver
VERSION
version 1.45
SYNOPSIS
use Selenium::Remote::Driver;
my $driver = Selenium::Remote::Driver->new;
$driver->get('http://www.google.com');
print $driver->get_title();
$driver->quit();
DESCRIPTION
Selenium is a test tool that allows you to write automated web application UI tests in any programming language against any HTTP website using any mainstream JavaScript-enabled browser. This module is an implementation of the client for the Remote driver that Selenium provides. You can find bindings for other languages at this location:
https://www.seleniumhq.org/download/
This module sends commands directly to the Server using HTTP. Using this module together with the Selenium Server, you can automatically control any supported browser. To use this module, you need to have already downloaded and started the Selenium Server (Selenium Server is a Java application).
USAGE
Without Standalone Server
As of v0.25, it's possible to use this module without a standalone server - that is, you would not need the JRE or the JDK to run your Selenium tests. See Selenium::Chrome, Selenium::PhantomJS, Selenium::Edge, Selenium::InternetExplorer,and Selenium::Firefox for details. If you'd like additional browsers besides these, give us a holler over in Github.
Remote Driver Response
Selenium::Remote::Driver uses the JsonWireProtocol And the WC3 WebDriver Protocol to communicate with the Selenium Server. If an error occurs while executing the command then the server sends back an HTTP error code with a JSON encoded reponse that indicates the precise Response Error Code. The module will then croak with the error message associated with this code. If no error occurred, then the subroutine called will return the value sent back from the server (if a return value was sent).
So a rule of thumb while invoking methods on the driver is if the method did not croak when called, then you can safely assume the command was successful even if nothing was returned by the method.
WebElement
Selenium Webdriver represents all the HTML elements as WebElement, which is in turn represented by Selenium::Remote::WebElement module. So any method that deals with WebElements will return and/or expect WebElement object. The POD for that module describes all the methods that perform various actions on the WebElements like click, submit etc.
To interact with any WebElement you have to first "find" it, read the POD for find_element or find_elements for further info. Once you find the required element then you can perform various actions. If you don't call find_* method first, all your further actions will fail for that element. Finally, just remember that you don't have to instantiate WebElement objects at all - they will be automatically created when you use the find_* methods.
A sub-class of Selenium::Remote::WebElement may be used instead of Selenium::Remote::WebElement, by providing that class name as an option the constructor:
my $driver = Selenium::Remote::Driver->new( webelement_class => ... );
For example, a testing-subclass may extend the web-element object with testing methods.
LWP Read Timeout errors
It's possible to make Selenium calls that take longer than the default LWP::UserAgent timeout. For example, setting the asynchronous script timeout greater than the LWP::UserAgent timeout and then executing a long running asynchronous snippet of javascript will immediately trigger an error like:
Error while executing command: executeAsyncScript: Server returned
error message read timeout at...
You can get around this by configuring LWP's timeout value, either by constructing your own LWP and passing it in to ::Driver during instantiation
my $timeout_ua = LWP::UserAgent->new;
$timeout_ua->timeout(360); # this value is in seconds!
my $d = Selenium::Remote::Driver->new( ua => $timeout_ua );
or by configuring the timeout on the fly as necessary:
use feature qw/say/;
use Selenium::Remote::Driver;
my $d = Selenium::Remote::Driver->new;
say $d->ua->timeout; # 180 seconds is the default
$d->ua->timeout(2); # LWP wants seconds, not milliseconds!
$d->set_timeout('script', 1000); # S::R::D wants milliseconds!
# Async scripts only return when the callback is invoked. Since there
# is no callback here, Selenium will block for the entire duration of
# the async timeout script. This will hit Selenium's async script
# timeout before hitting LWP::UserAgent's read timeout
$d->execute_async_script('return "hello"');
$d->quit;
TESTING
If are writing automated tests using this module, you may be interested in Test::Selenium::Remote::Driver which is also included in this distribution. It includes convenience testing methods for many of the selenum methods available here.
Your other option is to use this module in conjunction with your choice of testing modules, like Test::Spec or Test::More as you please.
WC3 WEBDRIVER COMPATIBILITY
WC3 Webdriver is a constantly evolving standard, so some things may or may not work at any given time.
Furthermore, out of date drivers probably identify as WD3, while only implementing a few methods and retaining JSONWire functionality. One way of dealing with this is setting:
$driver->{is_wd3} = 0
Of course, this will prevent access of any new WC3 methods, but will probably make your tests pass until your browser's driver gets it's act together.
There are also some JSONWire behaviors that we emulate in methods, such as Selenium::Remote::WebElement::get_attribute. You can get around that by passing an extra flag to the sub, or setting:
$driver->{emulate_jsonwire} = 0;
When in WC3 Webdriver mode.
FINDERS
This constant is a hashref map of the old element finder aliases from wd2 to wd3.
use Data::Dumper;
print Dumper($Selenium::Remote::Driver::FINDERS);
WC3 WEBDRIVER CURRENT STATUS
That said, the following 'sanity tests' in the at/ (acceptance test) directory of the module passed on the following versions:
- Selenium Server: 3.8.1 - all tests
- geckodriver: 0.19.1 - at/sanity.test, at/firefox.test (Selenium::Firefox)
- chromedriver: 2.35 - at/sanity-chrome.test, at/chrome.test (Selenium::Chrome)
- edgedriver: 5.16299 - at/sanity-edge.test
- InternetExplorerDriver : 3.8.1 - at/sanity-ie.test (be sure to enable 'allow local files to run active content in your 'advanced settings' pane)
These tests are intended to be run directly against a working selenium server on the local host with said drivers configured.
If you are curious as to what 'works and does not' on your driver versions (and a few other quirks), it is strongly encouraged you look at where the test calls the methods you are interested in.
While other browsers/drivers (especially legacy ones) likely work fine as well, any new browser/driver will likely have problems if it's not listed above.
There is also a 'legacy.test' file available to run against old browsers/selenium (2.x servers, pre geckodriver). This should only be used to verify backwards-compatibility has not been broken.
Firefox Notes
If you are intending to pass extra_capabilities to firefox on a WD3 enabled server with geckodriver, you MUST do the following:
$Selenium::Remote::Driver::FORCE_WD3=1;
This is because the gecko driver prefers legacy capabilities, both of which are normally passed for compatibility reasons.
Chrome Notes
Use the option goog:chromeOptions instead of chromeOptions, if you are intending to pass extra_capabilities on a WD3 enabled server with chromedriver enabled.
https://sites.google.com/a/chromium.org/chromedriver/capabilities
Also, if you instantiate the object in WC3 mode (which is the default), the remote driver will throw exceptions you have no choice but to catch, rather than falling back to JSONWire methods where applicable like geckodriver does.
As of chrome 75 (and it's appropriate driver versions), the WC3 spec has finally been implemented. As such, to use chrome older than this, you will have to manually force on JSONWire mode:
$Selenium::Remote::Driver::FORCE_WD2=1;
Notes on Running Selenium at Scale via selenium.jar
When running many, many tests in parallel you can eventually reach resource exhaustion. You have to instruct the Selenium JAR to do some cleanup to avoid explosions:
Inside of your selenium server's node.json (running a grid), you would put in the following:
"configuration" : { "cleanUpCycle":2000 } Or run the selenium jar with the -cleanupCycle parameter. Of course use whatever # of seconds is appropriate to your situation.
CONSTRUCTOR
new
Dies if communication with the selenium server cannot be established.
Input: (all optional)
Desired capabilities - HASH - Following options are accepted:
- remote_server_addr - <string> - IP or FQDN of the Webdriver server machine. Default: 'localhost'
- port - <string> - Port on which the Webdriver server is listening. Default: 4444
- browser_name - <string> - desired browser string: {phantomjs|firefox|internet explorer|MicrosoftEdge|safari|htmlunit|iphone|chrome}
- version - <string> - desired browser version number
- platform - <string> - desired platform: {WINDOWS|XP|VISTA|MAC|LINUX|UNIX|ANY}
- accept_ssl_certs - <boolean> - whether SSL certs should be accepted, default is true.
- firefox_profile - Profile - Use Selenium::Firefox::Profile to create a Firefox profile for the browser to use.
- javascript - <boolean> - Whether or not to use Javascript. You probably won't disable this, as you would be using WWW::Mechanize instead. Default: True
- auto_close - <boolean> - Whether to automatically close the browser session on the server when the object goes out of scope. Default: False.
- default_finder - <string> - Default method by which to evaluate selectors. Default: 'xpath'
- session_id - <string> - Provide a Session ID to highjack a browser session on the remote server. Useful for micro-optimizers. Default: undef
- pageLoadStrategy - STRING - OPTIONAL, 'normal|eager|none'. default 'normal'. WebDriver3 only.
- extra_capabilities - HASH - Any other extra capabilities. Accepted keys will vary by browser. If firefox_profile is passed, the args (or profile) key will be overwritten, depending on how it was passed.
- debug - BOOL - Turn Debug mode on from the start if true, rather than having to call debug_on().
On WebDriver3 the 'extra_capabilities' will be automatically converted into the parameter needed by your browser. For example, extra_capabilities is passed to the server as the moz:firefoxOptions parameter.
You can also specify some options in the constructor hash that are not part of the browser-related desired capabilities.
- auto_close - <boolean> - whether driver should end session on remote server on close.
- base_url - <string> - OPTIONAL, base url for the website Selenium acts on. This can save you from repeating the domain in every call to $driver->get()
- default_finder - <string> - choose default finder used for find_element* {class|class_name|css|id|link|link_text|name|partial_link_text|tag_name|xpath}
- inner_window_size - <aref[Int]> - An array ref [ height, width ] that the browser window should use as its initial size immediately after instantiation
- error_handler - CODEREF - A CODEREF that we will call in event of any exceptions. See "error_handler" for more details.
- webelement_class - <string> - sub-class of Selenium::Remote::WebElement if you wish to use an alternate WebElement class.
- ua - LWP::UserAgent instance - if you wish to use a specific $ua, like from Test::LWP::UserAgent
- proxy - HASH - Proxy configuration with the following keys:
-
- proxyType - <string> - REQUIRED, Possible values are:
-
direct - A direct connection - no proxy in use, manual - Manual proxy settings configured, e.g. setting a proxy for HTTP, a proxy for FTP, etc, pac - Proxy autoconfiguration from a URL, autodetect - proxy autodetection, probably with WPAD, system - Use system settings
- proxyAutoconfigUrl - <string> - REQUIRED if proxyType is 'pac', ignored otherwise. Expected format: http://hostname.com:1234/pacfile or file:///path/to/pacfile
- ftpProxy - <string> - OPTIONAL, ignored if proxyType is not 'manual'. Expected format: hostname.com:1234
- httpProxy - <string> - OPTIONAL, ignored if proxyType is not 'manual'. Expected format: hostname.com:1234
- sslProxy - <string> - OPTIONAL, ignored if proxyType is not 'manual'. Expected format: hostname.com:1234
- socksProxy - <string> - OPTIONAL, ignored if proxyType is not 'manual'. Expected format: hostname.com:1234. WebDriver 3 only.
- socksVersion - <int> - OPTIONAL, ignored if proxyType is not 'manual'. WebDriver 3 only.
- noProxy - <ARRAY> - OPTIONAL, list of URLs to bypass the proxy for. WebDriver3 only.
- firefox_profile - <string> - Base64 encoded ZIP file of a firefox profile directory, for use when you don't want/need Selenium::Firefox::Profile.
Output:
Selenium::Remote::Driver object
Usage:
my $driver = Selenium::Remote::Driver->new;
#or
my $driver = Selenium::Remote::Driver->new('browser_name' => 'firefox',
'platform' => 'MAC');
#or (for Firefox 47 or lower on Selenium 3+)
my $driver = Selenium::Remote::Driver->new('browser_name' => 'firefox',
'platform' => 'MAC',
'extra_capabilities' => {
'marionette' => \0,
});
#or
my $driver = Selenium::Remote::Driver->new('remote_server_addr' => '10.10.1.1',
'port' => '2222',
'auto_close' => 0);
#or
my $driver = Selenium::Remote::Driver->new('browser_name' =>'chrome',
'extra_capabilities' => {
'goog:chromeOptions' => {
'args' => [
'window-size=1260,960',
'incognito'
],
'prefs' => {
'session' => {
'restore_on_startup' => 4,
'urls_to_restore_on_startup' => [
'http://www.google.com',
'http://docs.seleniumhq.org'
]},
'first_run_tabs' => [
'http://www.google.com',
'http://docs.seleniumhq.org'
]
}
}
});
#or
my $driver = Selenium::Remote::Driver->new('proxy' => {'proxyType' => 'manual', 'httpProxy' => 'myproxy.com:1234'});
#or
my $driver = Selenium::Remote::Driver->new('default_finder' => 'css');
error_handler
clear_error_handler
OPTIONAL constructor arg & associated setter/clearer: if you wish to install your own error handler, you may pass a code ref in to error_handler
during instantiation like follows:
my $driver = Selenium::Remote::Driver->new(
error_handler => sub { print $_[1]; croak 'goodbye'; }
);
Additionally, you can set and/or clear it at any time on an already-instantiated driver:
# later, change the error handler to something else
$driver->error_handler( sub { print $_[1]; croak 'hello'; } );
# stop handling errors manually and use the default S:R:D behavior
# (we will croak about the exception)
$driver->clear_error_handler;
Your error handler will receive three arguments: the first argument is the $driver
object itself, and the second argument is the exception message and stack trace in one multiline string. The final argument(s) are the argument array to the command just executed.
N.B.: If you set your own error handler, you are entirely responsible for handling webdriver exceptions, _including_ croaking behavior. That is, when you set an error handler, we will no longer croak on Webdriver exceptions - it's up to you to do so. For consistency with the standard S:R:D behavior, we recommend your error handler also croak when it's done, especially since your test shouldn't be running into unexpected errors. Catching specific or desired errors in your error handler makes sense, but not croaking at all can leave you in unfamiliar territory. Reaching an unexpected exception might mean your test has gone off the rails, and the further your test gets from the source of the of the exception, the harder it will be to debug.
N.B.: Four methods will still croak on their own: "find_element", "find_elements", "find_child_element", and "find_child_elements". If these methods throw a Webdriver Exception, your error handler _will still be_ invoked inside an eval
, and then they'll croak with their own error message that indicates the locator and strategy used. So, your strategies for avoiding exceptions when finding elements do not change (either use find_elements and check the returned array size, wrap your calls to find_element* in an eval
, or use the parameterized versions find_element_*).
new_from_caps
Description:
For experienced users who want complete control over the desired
capabilities, use this alternative constructor along with the
C<desired_capabilities> hash key in the init hash. Unlike "new",
this constructor will not assume any defaults for your desired
capabilities.
This alternate constructor IGNORES all other browser-related
desiredCapability options; the only options that will be respected
are those that are NOT part of the Capabilities JSON Object as
described in the Json Wire Protocol.
Input:
The only respected keys in the input hash are:
desired_capabilities - HASHREF - defaults to {}
remote_server_addr - STRING - defaults to localhost
port - INTEGER - defaults to 4444
default_finder - STRING - defaults to xpath
webelement_class - STRING - defaults to Selenium::Remote::WebElement
auto_close - BOOLEAN - defaults to 1
error_handler - CODEREF - defaults to croaking on exceptions
Except for C<desired_capabilities>, these keys perform exactly the
same as listed in the regular "new" constructor.
The hashref you pass in as desired_capabilities only gets json
encoded before being passed to the Selenium server; no default
options of any sort will be added.
This means you must handle normalization and casing of the input
options (like "browser_name" vs "browserName") and take care of
things like encoding the firefox profile if applicable. More
information about the desired capabilities object is available on
the Selenium wiki:
https://code.google.com/p/selenium/wiki/JsonWireProtocol#Capabilities_JSON_Object
Output:
Remote Driver object
Usage:
my $driver = Selenium::Remote::Driver->new_from_caps(
'desired_capabilities' => {'browserName' => 'firefox'}
);
The above would generate a POST to the webdriver server at
localhost:4444 with the exact payload of '{"desiredCapabilities":
{"browserName": "firefox" }}'.
METHODS
new_session (extra_capabilities)
Make a new session on the server. Called by new(), not intended for regular use.
Occaisonally handy for recovering from brower crashes.
DANGER DANGER DANGER
This will throw away your old session if you have not closed it!
DANGER DANGER DANGER
new_desired_session(capabilities)
Basically the same as new_session, but with caps. Sort of an analog to new_from_caps.
is_webdriver_3
Print whether the server (or browser) thinks it's implemented webdriver 3. If this returns true, webdriver 3 methods will be used in the case an action exists in Selenium::Remote::Spec for the method you are trying to call. If a method you are calling has no webdriver 3 equivalent (or browser extension), the legacy commands implemented in Selenium::Remote::Commands will be used.
Note how I said *thinks* above. In the case you want to force usage of legacy methods, set $driver->{is_wd3} to work around various browser issues.
debug_on
Description:
Turns on debugging mode and the driver will print extra info like request
and response to stdout. Useful, when you want to see what is being sent to
the server & what response you are getting back.
Usage:
$driver->debug_on;
debug_off
Description:
Turns off the debugging mode.
Usage:
$driver->debug_off;
get_sessions
Description:
Returns a list of the currently active sessions. Each session will be
returned as an array of Hashes with the following keys:
'id' : The session ID
'capabilities: An object describing session's capabilities
Output:
Array of Hashes
Usage:
print Dumper $driver->get_sessions();
status
Description:
Query the server's current status. All server implementations
should return two basic objects describing the server's current
platform and when the server was built.
Output:
Hash ref
Usage:
print Dumper $driver->status;
get_alert_text
Description:
Gets the text of the currently displayed JavaScript alert(), confirm()
or prompt() dialog.
Example
my $string = $driver->get_alert_text;
send_keys_to_active_element
Description:
Send a sequence of key strokes to the active element. This command is
similar to the send keys command in every aspect except the implicit
termination: The modifiers are not released at the end of the call.
Rather, the state of the modifier keys is kept between calls, so mouse
interactions can be performed while modifier keys are depressed.
Compatibility:
On webdriver 3 servers, don't use this to send modifier keys; use send_modifier instead.
Input: 1
Required:
{ARRAY | STRING} - Array of strings or a string.
Usage:
$driver->send_keys_to_active_element('abcd', 'efg');
$driver->send_keys_to_active_element('hijk');
or
# include the WDKeys module
use Selenium::Remote::WDKeys;
$driver->send_keys_to_active_element(KEYS->{'space'}, KEYS->{'enter'});
send_keys_to_alert
Synonymous with send_keys_to_prompt
send_keys_to_prompt
Description:
Sends keystrokes to a JavaScript prompt() dialog.
Input:
{string} keys to send
Example:
$driver->send_keys_to_prompt('hello world');
or
ok($driver->get_alert_text eq 'Please Input your name','prompt appears');
$driver->send_keys_to_alert("Larry Wall");
$driver->accept_alert;
accept_alert
Description:
Accepts the currently displayed alert dialog. Usually, this is
equivalent to clicking the 'OK' button in the dialog.
Example:
$driver->accept_alert;
dismiss_alert
Description:
Dismisses the currently displayed alert dialog. For comfirm()
and prompt() dialogs, this is equivalent to clicking the
'Cancel' button. For alert() dialogs, this is equivalent to
clicking the 'OK' button.
Example:
$driver->dismiss_alert;
general_action
Provide an 'actions definition' hash to make webdriver use input devices. Given the spec for the structure of this data is 'non normative', it is left as an exercise to the reader what that means as to how to use this function.
That said, it seems most of the data looks something like this:
$driver->general_action( actions => [{
type => 'pointer|key|none|somethingElseSuperSpecialDefinedByYourBrowserDriver',
id => MUST be mouse|key|none|other. And by 'other' I mean anything else. The first 3 are 'special' in that they are used in the global actions queue.
If you want say, another mouse action to execute in parallel to other mouse actions (to simulate multi-touch, for example), call your action 'otherMouseAction' or something.
parameters => {
someOption => "basically these are global parameters used by all steps in the forthcoming "action chain".
},
actions => [
{
type => "keyUp|KeyDown if key, pointerUp|pointerDown|pointerMove|pointerCancel if pointer, pause if any type",
key => A raw keycode or character from the keyboard if this is a key event,
duration => how many 'ticks' this action should take, you probably want this to be 0 all of the time unless you are evading Software debounce.
button => what number button if you are using a pointer (this sounds terribly like it might be re-purposed to be a joypad in the future sometime)
origin => Point of Origin if moving a pointer around
x => unit vector to travel along x-axis if pointerMove event
y => unit vector to travel along y-axis if pointerMove event
},
...
]
},
...
]
)
Only available on WebDriver3 capable selenium servers.
If you have called any legacy shim, such as mouse_move_to_location() previously, your actions passed will be appended to the existing actions queue. Called with no arguments, it simply executes the existing action queue.
If you are looking for pre-baked action chains that aren't currently part of Selenium::Remote::Driver, consider Selenium::ActionChains, which is shipped with this distribution instead.
COMPATIBILITY
Like most places, the WC3 standard is openly ignored by the driver binaries. Generally an "actions" object will only accept:
{ type => ..., value => ... }
When using the direct drivers (E.G. Selenium::Chrome, Selenium::Firefox). This is not documented anywhere but here, as far as I can tell.
release_general_action
Nukes *all* input device state (modifier key up/down, pointer button up/down, pointer location, and other device state) from orbit. Call if you forget to do a *Up event in your provided action chains, or just to save time.
Also clears the current actions queue.
Only available on WebDriver3 capable selenium servers.
mouse_move_to_location
Description:
Move the mouse by an offset of the specificed element. If no
element is specified, the move is relative to the current mouse
cursor. If an element is provided but no offset, the mouse will be
moved to the center of the element. If the element is not visible,
it will be scrolled into view.
Compatibility:
Due to limitations in the Webdriver 3 API, mouse movements have to be executed 'lazily' e.g. only right before a click() event occurs.
This is because there is no longer any persistent mouse location state; mouse movements are now totally atomic.
This has several problematic aspects; for one, I can't think of a way to both hover an element and then do another action relying on the element staying hover()ed,
Aside from using javascript workarounds.
Output:
STRING -
Usage:
# element - the element to move to. If not specified or is null, the offset is relative to current position of the mouse.
# xoffset - X offset to move to, relative to the top-left corner of the element. If not specified, the mouse will move to the middle of the element.
# yoffset - Y offset to move to, relative to the top-left corner of the element. If not specified, the mouse will move to the middle of the element.
print $driver->mouse_move_to_location(element => e, xoffset => x, yoffset => y);
move_to
Synonymous with mouse_move_to_location
get_capabilities
Description:
Retrieve the capabilities of the specified session.
Output:
HASH of all the capabilities.
Usage:
my $capab = $driver->get_capabilities();
print Dumper($capab);
get_timeouts
Description:
Get the currently configured values (ms) for the page load, script and implicit timeouts.
Compatibility:
Only available on WebDriver3 enabled selenium servers.
Usage:
$driver->get_timeouts();
set_timeout
Description:
Configure the amount of time that a particular type of operation can execute
for before they are aborted and a |Timeout| error is returned to the client.
Input:
type - <STRING> - The type of operation to set the timeout for.
Valid values are:
"script" : for script timeouts,
"implicit" : for modifying the implicit wait timeout
"page load" : for setting a page load timeout.
ms - <NUMBER> - The amount of time, in milliseconds, that time-limited
commands are permitted to run.
Usage:
$driver->set_timeout('script', 1000);
set_async_script_timeout
Description:
Set the amount of time, in milliseconds, that asynchronous scripts executed
by execute_async_script() are permitted to run before they are
aborted and a |Timeout| error is returned to the client.
Input:
ms - <NUMBER> - The amount of time, in milliseconds, that time-limited
commands are permitted to run.
Usage:
$driver->set_async_script_timeout(1000);
set_implicit_wait_timeout
Description:
Set the amount of time the driver should wait when searching for elements.
When searching for a single element, the driver will poll the page until
an element is found or the timeout expires, whichever occurs first.
When searching for multiple elements, the driver should poll the page until
at least one element is found or the timeout expires, at which point it
will return an empty list. If this method is never called, the driver will
default to an implicit wait of 0ms.
This is exactly equivalent to calling L</set_timeout> with a type
arg of C<"implicit">.
Input:
Time in milliseconds.
Output:
Server Response Hash with no data returned back from the server.
Usage:
$driver->set_implicit_wait_timeout(10);
pause
Description:
Pause execution for a specified interval of milliseconds.
Usage:
$driver->pause(10000); # 10 second delay
$driver->pause(); # 1 second delay default
DEPRECATED: consider using Time::HiRes instead.
close
Description:
Close the current window.
Usage:
$driver->close();
or
#close a popup window
my $handles = $driver->get_window_handles;
$driver->switch_to_window($handles->[1]);
$driver->close();
$driver->switch_to_window($handles->[0]);
quit
Description:
DELETE the session, closing open browsers. We will try to call
this on our down when we get destroyed, but in the event that we
are demolished during global destruction, we will not be able to
close the browser. For your own unattended and/or complicated
tests, we recommend explicitly calling quit to make sure you're
not leaving orphan browsers around.
Note that as a Moo class, we use a subroutine called DEMOLISH that
takes the place of DESTROY; for more information, see
https://metacpan.org/pod/Moo#DEMOLISH.
Usage:
$driver->quit();
get_current_window_handle
Description:
Retrieve the current window handle.
Output:
STRING - the window handle
Usage:
print $driver->get_current_window_handle();
get_window_handles
Description:
Retrieve the list of window handles used in the session.
Output:
ARRAY of STRING - list of the window handles
Usage:
print Dumper $driver->get_window_handles;
or
# get popup, close, then back
my $handles = $driver->get_window_handles;
$driver->switch_to_window($handles->[1]);
$driver->close;
$driver->switch_to_window($handles->[0]);
get_window_size
Description:
Retrieve the window size
Compatibility:
The ability to get the size of arbitrary handles by passing input only exists in WebDriver2.
You will have to switch to the window first going forward.
Input:
STRING - <optional> - window handle (default is 'current' window)
Output:
HASH - containing keys 'height' & 'width'
Usage:
my $window_size = $driver->get_window_size();
print $window_size->{'height'}, $window_size->{'width'};
get_window_position
Description:
Retrieve the window position
Compatibility:
The ability to get the size of arbitrary handles by passing input only exists in WebDriver2.
You will have to switch to the window first going forward.
Input:
STRING - <optional> - window handle (default is 'current' window)
Output:
HASH - containing keys 'x' & 'y'
Usage:
my $window_size = $driver->get_window_position();
print $window_size->{'x'}, $window_size->('y');
get_current_url
Description:
Retrieve the url of the current page
Output:
STRING - url
Usage:
print $driver->get_current_url();
navigate
Description:
Navigate to a given url. This is same as get() method.
Input:
STRING - url
Usage:
$driver->navigate('http://www.google.com');
get
Description:
Navigate to a given url
Input:
STRING - url
Usage:
$driver->get('http://www.google.com');
get_title
Description:
Get the current page title
Output:
STRING - Page title
Usage:
print $driver->get_title();
go_back
Description:
Equivalent to hitting the back button on the browser.
Usage:
$driver->go_back();
go_forward
Description:
Equivalent to hitting the forward button on the browser.
Usage:
$driver->go_forward();
refresh
Description:
Reload the current page.
Usage:
$driver->refresh();
has_javascript
Description:
returns true if javascript is enabled in the driver.
Compatibility:
Can't be false on WebDriver 3.
Usage:
if ($driver->has_javascript) { ...; }
execute_async_script
Description:
Inject a snippet of JavaScript into the page for execution in the context
of the currently selected frame. The executed script is assumed to be
asynchronous and must signal that is done by invoking the provided
callback, which is always provided as the final argument to the function.
The value to this callback will be returned to the client.
Asynchronous script commands may not span page loads. If an unload event
is fired while waiting for a script result, an error should be returned
to the client.
Input: 2 (1 optional)
Required:
STRING - Javascript to execute on the page
Optional:
ARRAY - list of arguments that need to be passed to the script.
Output:
{*} - Varied, depending on the type of result expected back from the script.
Usage:
my $script = q{
var arg1 = arguments[0];
var callback = arguments[arguments.length-1];
var elem = window.document.findElementById(arg1);
callback(elem);
};
my $elem = $driver->execute_async_script($script,'myid');
$elem->click;
execute_script
Description:
Inject a snippet of JavaScript into the page and return its result.
WebElements that should be passed to the script as an argument should be
specified in the arguments array as WebElement object. Likewise,
any WebElements in the script result will be returned as WebElement object.
Input: 2 (1 optional)
Required:
STRING - Javascript to execute on the page
Optional:
ARRAY - list of arguments that need to be passed to the script.
Output:
{*} - Varied, depending on the type of result expected back from the script.
Usage:
my $script = q{
var arg1 = arguments[0];
var elem = window.document.findElementById(arg1);
return elem;
};
my $elem = $driver->execute_script($script,'myid');
$elem->click;
screenshot
Description:
Get a screenshot of the current page as a base64 encoded image.
Optionally pass {'full' => 1} as argument to take a full screenshot and not
only the viewport. (Works only with firefox and geckodriver >= 0.24.0)
Output:
STRING - base64 encoded image
Usage:
print $driver->screenshot();
print $driver->screenshot({'full' => 1});
To conveniently write the screenshot to a file, see "capture_screenshot".
capture_screenshot
Description:
Capture a screenshot and save as a PNG to provided file name.
(The method is compatible with the WWW::Selenium method of the same name)
Optionally pass {'full' => 1} as second argument to take a full screenshot
and not only the viewport. (Works only with firefox and geckodriver >= 0.24.0)
Output:
TRUE - (Screenshot is written to file)
Usage:
$driver->capture_screenshot($filename);
$driver->capture_screenshot($filename, {'full' => 1});
available_engines
Description:
List all available engines on the machine. To use an engine, it has to be present in this list.
Compatibility:
Does not appear to be available on Webdriver3 enabled selenium servers.
Output:
{Array.<string>} A list of available engines
Usage:
print Dumper $driver->available_engines;
switch_to_frame
Description:
Change focus to another frame on the page. If the frame ID is null, the
server will switch to the page's default content. You can also switch to a
WebElement, for e.g. you can find an iframe using find_element & then
provide that as an input to this method. Also see e.g.
Input: 1
Required:
{STRING | NUMBER | NULL | WebElement} - ID of the frame which can be one of the three
mentioned.
Usage:
$driver->switch_to_frame('frame_1');
or
$driver->switch_to_frame($driver->find_element('iframe', 'tag_name'));
COMPATIBILITY
Chromedriver will vomit if you pass anything but a webElement, so you probably should do that from now on.
switch_to_parent_frame
Webdriver 3 equivalent of calling switch_to_frame with no arguments (e.g. NULL frame). This is actually called in that case, supposing you are using WD3 capable servers now.
switch_to_window
Description:
Change focus to another window. The window to change focus to may
be specified by its server assigned window handle, or by the value
of the page's window.name attribute.
If you wish to use the window name as the target, you'll need to
have set C<window.name> on the page either in app code or via
L</execute_script>, or pass a name as the second argument to the
C<window.open()> function when opening the new window. Note that
the window name used here has nothing to do with the window title,
or the C<< <title> >> element on the page.
Otherwise, use L</get_window_handles> and select a
Webdriver-generated handle from the output of that function.
Input: 1
Required:
STRING - Window handle or the Window name
Usage:
$driver->switch_to_window('MY Homepage');
or
# close a popup window and switch back
my $handles = $driver->get_window_handles;
$driver->switch_to_window($handles->[1]);
$driver->close;
$driver->switch_to_window($handles->[0]);
set_window_position
Description:
Set the position (on screen) where you want your browser to be displayed.
Compatibility:
In webDriver 3 enabled selenium servers, you may only operate on the focused window.
As such, the window handle argument below will be ignored in this context.
Input:
INT - x co-ordinate
INT - y co-ordinate
STRING - <optional> - window handle (default is 'current' window)
Output:
BOOLEAN - Success or failure
Usage:
$driver->set_window_position(50, 50);
set_window_size
Description:
Set the size of the browser window
Compatibility:
In webDriver 3 enabled selenium servers, you may only operate on the focused window.
As such, the window handle argument below will be ignored in this context.
Input:
INT - height of the window
INT - width of the window
STRING - <optional> - window handle (default is 'current' window)
Output:
BOOLEAN - Success or failure
Usage:
$driver->set_window_size(640, 480);
maximize_window
Description:
Maximizes the browser window
Compatibility:
In webDriver 3 enabled selenium servers, you may only operate on the focused window.
As such, the window handle argument below will be ignored in this context.
Also, on chromedriver maximize is actually just setting the window size to the screen's
available height and width.
Input:
STRING - <optional> - window handle (default is 'current' window)
Output:
BOOLEAN - Success or failure
Usage:
$driver->maximize_window();
minimize_window
Description:
Minimizes the currently focused browser window (webdriver3 only)
Output:
BOOLEAN - Success or failure
Usage:
$driver->minimize_window();
fullscreen_window
Description:
Fullscreens the currently focused browser window (webdriver3 only)
Output:
BOOLEAN - Success or failure
Usage:
$driver->fullscreen_window();
get_all_cookies
Description:
Retrieve all cookies visible to the current page. Each cookie will be
returned as a HASH reference with the following keys & their value types:
'name' - STRING
'value' - STRING
'path' - STRING
'domain' - STRING
'secure' - BOOLEAN
Output:
ARRAY of HASHES - list of all the cookie hashes
Usage:
print Dumper($driver->get_all_cookies());
add_cookie
Description:
Set a cookie on the domain.
Input: 2 (4 optional)
Required:
'name' - STRING
'value' - STRING
Optional:
'path' - STRING
'domain' - STRING
'secure' - BOOLEAN - default false.
'httponly' - BOOLEAN - default false.
'expiry' - TIME_T - default 20 years in the future
Usage:
$driver->add_cookie('foo', 'bar', '/', '.google.com', 0, 1)
delete_all_cookies
Description:
Delete all cookies visible to the current page.
Usage:
$driver->delete_all_cookies();
get_cookie_named
Basically get only the cookie with the provided name. Probably preferable to pick it out of the list unless you expect a *really* long list.
Input:
Cookie Name - STRING
Returns cookie definition hash, much like the elements in get_all_cookies();
Compatibility:
Only available on webdriver3 enabled selenium servers.
delete_cookie_named
Description:
Delete the cookie with the given name. This command will be a no-op if there
is no such cookie visible to the current page.
Input: 1
Required:
STRING - name of cookie to delete
Usage:
$driver->delete_cookie_named('foo');
get_page_source
Description:
Get the current page source.
Output:
STRING - The page source.
Usage:
print $driver->get_page_source();
find_element
Description:
Search for an element on the page, starting from the document
root. The located element will be returned as a WebElement
object. If the element cannot be found, we will CROAK, killing
your script. If you wish for a warning instead, use the
parameterized version of the finders:
find_element_by_class
find_element_by_class_name
find_element_by_css
find_element_by_id
find_element_by_link
find_element_by_link_text
find_element_by_name
find_element_by_partial_link_text
find_element_by_tag_name
find_element_by_xpath
These functions all take a single STRING argument: the locator
search target of the element you want. If the element is found, we
will receive a WebElement. Otherwise, we will return 0. Note that
invoking methods on 0 will of course kill your script.
Input: 2 (1 optional)
Required:
STRING - The search target.
Optional:
STRING - Locator scheme to use to search the element, available schemes:
{class, class_name, css, id, link, link_text, partial_link_text,
tag_name, name, xpath}
Defaults to 'xpath' if not configured global during instantiation.
Output:
Selenium::Remote::WebElement - WebElement Object
(This could be a subclass of L<Selenium::Remote::WebElement> if C<webelement_class> was set.
Usage:
$driver->find_element("//input[\@name='q']");
find_elements
Description:
Search for multiple elements on the page, starting from the document root.
The located elements will be returned as an array of WebElement object.
Input: 2 (1 optional)
Required:
STRING - The search target.
Optional:
STRING - Locator scheme to use to search the element, available schemes:
{class, class_name, css, id, link, link_text, partial_link_text,
tag_name, name, xpath}
Defaults to 'xpath' if not configured global during instantiation.
Output:
ARRAY or ARRAYREF of WebElement Objects
Usage:
$driver->find_elements("//input");
find_child_element
Description:
Search for an element on the page, starting from the identified element. The
located element will be returned as a WebElement object.
Input: 3 (1 optional)
Required:
Selenium::Remote::WebElement - WebElement object from where you want to
start searching.
STRING - The search target. (Do not use a double whack('//')
in an xpath to search for a child element
ex: '//option[@id="something"]'
instead use a dot whack ('./')
ex: './option[@id="something"]')
Optional:
STRING - Locator scheme to use to search the element, available schemes:
{class, class_name, css, id, link, link_text, partial_link_text,
tag_name, name, xpath}
Defaults to 'xpath' if not configured global during instantiation.
Output:
WebElement Object
Usage:
my $elem1 = $driver->find_element("//select[\@name='ned']");
# note the usage of ./ when searching for a child element instead of //
my $child = $driver->find_child_element($elem1, "./option[\@value='es_ar']");
find_child_elements
Description:
Search for multiple element on the page, starting from the identified
element. The located elements will be returned as an array of WebElement
objects.
Input: 3 (1 optional)
Required:
Selenium::Remote::WebElement - WebElement object from where you want to
start searching.
STRING - The search target.
Optional:
STRING - Locator scheme to use to search the element, available schemes:
{class, class_name, css, id, link, link_text, partial_link_text,
tag_name, name, xpath}
Defaults to 'xpath' if not configured global during instantiation.
Output:
ARRAY of WebElement Objects.
Usage:
my $elem1 = $driver->find_element("//select[\@name='ned']");
# note the usage of ./ when searching for a child element instead of //
my $child = $driver->find_child_elements($elem1, "./option");
find_element_by_class
See "find_element".
find_element_by_class_name
See "find_element".
find_element_by_css
See "find_element".
find_element_by_id
See "find_element".
find_element_by_link
See "find_element".
find_element_by_link_text
See "find_element".
find_element_by_name
See "find_element".
find_element_by_partial_link_text
See "find_element".
find_element_by_tag_name
See "find_element".
find_element_by_xpath
See "find_element".
get_active_element
Description:
Get the element on the page that currently has focus.. The located element
will be returned as a WebElement object.
Output:
WebElement Object
Usage:
$driver->get_active_element();
cache_status
Description:
Get the status of the html5 application cache.
Usage:
print $driver->cache_status;
Output:
<number> - Status code for application cache: {UNCACHED = 0, IDLE = 1, CHECKING = 2, DOWNLOADING = 3, UPDATE_READY = 4, OBSOLETE = 5}
set_geolocation
Description:
Set the current geographic location - note that your driver must
implement this endpoint, or else it will crash your session. At the
very least, it works in v2.12 of Chromedriver.
Input:
Required:
HASH: A hash with key C<location> whose value is a Location hashref. See
usage section for example.
Usage:
$driver->set_geolocation( location => {
latitude => 40.714353,
longitude => -74.005973,
altitude => 0.056747
});
Output:
BOOLEAN - success or failure
get_geolocation
Description:
Get the current geographic location. Note that your webdriver must
implement this endpoint - otherwise, it will crash your session. At
the time of release, we couldn't get this to work on the desktop
FirefoxDriver or desktop Chromedriver.
Usage:
print $driver->get_geolocation;
Output:
{ latitude: number, longitude: number, altitude: number } - The current geo location.
get_log
Description:
Get the log for a given log type. Log buffer is reset after each request.
Input:
Required:
<STRING> - Type of log to retrieve:
{client|driver|browser|server}. There may be others available; see
get_log_types for a full list for your driver.
Usage:
$driver->get_log( $log_type );
Output:
<ARRAY|ARRAYREF> - An array of log entries since the most recent request.
get_log_types
Description:
Get available log types. By default, every driver should have client,
driver, browser, and server types, but there may be more available,
depending on your driver.
Usage:
my @types = $driver->get_log_types;
$driver->get_log($types[0]);
Output:
<ARRAYREF> - The list of log types.
set_orientation
Description:
Set the browser orientation.
Input:
Required:
<STRING> - Orientation {LANDSCAPE|PORTRAIT}
Usage:
$driver->set_orientation( $orientation );
Output:
BOOLEAN - success or failure
get_orientation
Description:
Get the current browser orientation. Returns either LANDSCAPE|PORTRAIT.
Usage:
print $driver->get_orientation;
Output:
<STRING> - your orientation.
send_modifier
Description:
Send an event to the active element to depress or release a modifier key.
Input: 2
Required:
value - String - The modifier key event to be sent. This key must be one 'Ctrl','Shift','Alt',' or 'Command'/'Meta' as defined by the send keys command
isdown - Boolean/String - Whether to generate a key down or key up
Usage:
$driver->send_modifier('Alt','down');
$elem->send_keys('c');
$driver->send_modifier('Alt','up');
or
$driver->send_modifier('Alt',1);
$elem->send_keys('c');
$driver->send_modifier('Alt',0);
compare_elements
Description:
Test if two element IDs refer to the same DOM element.
Input: 2
Required:
Selenium::Remote::WebElement - WebElement Object
Selenium::Remote::WebElement - WebElement Object
Output:
BOOLEAN
Usage:
$driver->compare_elements($elem_obj1, $elem_obj2);
click
Description:
Click any mouse button (at the coordinates set by the last moveto command).
Input:
button - any one of 'LEFT'/0 'MIDDLE'/1 'RIGHT'/2
defaults to 'LEFT'
queue - (optional) queue the click, rather than executing it. WD3 only.
Usage:
$driver->click('LEFT');
$driver->click(1); #MIDDLE
$driver->click('RIGHT');
$driver->click; #Defaults to left
double_click
Description:
Double-clicks at the current mouse coordinates (set by moveto).
Compatibility:
On webdriver3 enabled servers, you can double click arbitrary mouse buttons.
Usage:
$driver->double_click(button);
button_down
Description:
Click and hold the left mouse button (at the coordinates set by the
last moveto command). Note that the next mouse-related command that
should follow is buttonup . Any other mouse command (such as click
or another call to buttondown) will yield undefined behaviour.
Compatibility:
On WebDriver 3 enabled servers, all this does is queue a button down action.
You will either have to call general_action() to perform the queue, or an action like click() which also clears the queue.
Usage:
$self->button_down;
button_up
Description:
Releases the mouse button previously held (where the mouse is
currently at). Must be called once for every buttondown command
issued. See the note in click and buttondown about implications of
out-of-order commands.
Compatibility:
On WebDriver 3 enabled servers, all this does is queue a button down action.
You will either have to call general_action() to perform the queue, or an action like click() which also clears the queue.
Usage:
$self->button_up;
upload_file
Description:
Upload a file from the local machine to the selenium server
machine. That file then can be used for testing file upload on web
forms. Returns the remote-server's path to the file.
Passing raw data as an argument past the filename will upload
that rather than the file's contents.
When passing raw data, be advised that it expects a zipped
and then base64 encoded version of a single file.
Multiple files and/or directories are not supported by the remote server.
Usage:
my $remote_fname = $driver->upload_file( $fname );
my $element = $driver->find_element( '//input[@id="file"]' );
$element->send_keys( $remote_fname );
get_text
Description:
Get the text of a particular element. Wrapper around L</find_element>
Usage:
$text = $driver->get_text("//div[\@name='q']");
get_body
Description:
Get the current text for the whole body. If you want the entire raw HTML instead,
See L</get_page_source>.
Usage:
$body_text = $driver->get_body();
get_path
Description:
Get the path part of the current browser location.
Usage:
$path = $driver->get_path();
get_user_agent
Description:
Convenience method to get the user agent string, according to the
browser's value for window.navigator.userAgent.
Usage:
$user_agent = $driver->get_user_agent()
set_inner_window_size
Description:
Set the inner window size by closing the current window and
reopening the current page in a new window. This can be useful
when using browsers to mock as mobile devices.
This sub will be fired automatically if you set the
C<inner_window_size> hash key option during instantiation.
Input:
INT - height of the window
INT - width of the window
Output:
BOOLEAN - Success or failure
Usage:
$driver->set_inner_window_size(640, 480)
get_local_storage_item
Description:
Get the value of a local storage item specified by the given key.
Input: 1
Required:
STRING - name of the key to be retrieved
Output:
STRING - value of the local storage item
Usage:
$driver->get_local_storage_item('key')
delete_local_storage_item
Description:
Get the value of a local storage item specified by the given key.
Input: 1
Required
STRING - name of the key to be deleted
Usage:
$driver->delete_local_storage_item('key')
AUTHORS
Current Maintainers:
George S. Baugh <george@troglodyne.net>
Previous maintainers:
Daniel Gempesaw <gempesaw@gmail.com>
Emmanuel Peroumalnaïk <peroumalnaik.emmanuel@gmail.com>
Luke Closs <cpan@5thplane.com>
Mark Stosberg <mark@stosberg.com>
Original authors:
Aditya Ivaturi <ivaturi@gmail.com>
CONTRIBUTORS
Allen Lew <allen@alew.org>
A.MacLeay <a.macleay@gmail.com>
Andy Jack <andyjack@users.noreply.github.com>
Andy Jack <github@veracity.ca>
Bas Bloemsaat <bas@bloemsaat.com>
bernhard <Bernhard.Schmalhofer@gmx.de>
Blake GH <blake@mobiusconsortium.org>
Brian Horakh <brianh@zoovy.com>
Charles Howes <charles.howes@globalrelay.net>
Chris Davies <FMQA@users.noreply.github.com>
Daniel Fackrell <dfackrell@bluehost.com>
Dave Rolsky <autarch@urth.org>
Dmitry Karasik <dmitry@karasik.eu.org>
Doug Bell <doug@preaction.me>
Dylan Streb <dylan.streb@oneil.com>
Eric Johnson <eric.git@iijo.org>
Gabor Szabo <gabor@szabgab.com>
George S. Baugh <george.b@cpanel.net>
Gerhard Jungwirth <gjungwirth@sipwise.com>
Gordon Child <gordon@gordonchild.com>
GreatFlamingFoo <greatflamingfoo@gmail.com>
Ivan Kurmanov <duraley@gmail.com>
Joe Higton <draxil@gmail.com>
Jon Hermansen <jon.hermansen@gmail.com>
Keita Sugama <sugama@jamadam.com>
Ken Swanson <kswanson@genome.wustl.edu>
lembark <lembark@wrkhors.com>
Luke Closs <lukec@users.noreply.github.com>
Martin Gruner <martin.gruner@otrs.com>
Matthew Spahr <matthew.spahr@cpanel.net>
Max O'Cull <maxattax97@gmail.com>
Michael Prokop <mprokop@sipwise.com>
mk654321 <kosmichal@gmail.com>
Peter Mottram (SysPete) <peter@sysnix.com>
Phil Kania <phil@vivox.com>
Phil Mitchell <phil.mitchell@pobox.com>
Prateek Goyal <prateek.goyal5@gmail.com>
Richard Sailer <richard@weltraumpflege.org>
Robert Utter <utter.robert@gmail.com>
rouzier <rouzier@gmail.com>
Tetsuya Tatsumi <ttatsumi@ra2.so-net.ne.jp>
Tod Hagan <42418406+tod222@users.noreply.github.com>
Tom Hukins <tom@eborcom.com>
Vangelis Katsikaros <vangelis@adzuna.com>
Vangelis Katsikaros <vkatsikaros@gmail.com>
Vishwanath Janmanchi <jvishwanath@gmail.com>
Viťas Strádal <vitas@matfyz.cz>
Yuki Kimoto <kimoto.yuki@gmail.com>
Yves Lavoie <ylavoie@yveslavoie.com>
COPYRIGHT AND LICENSE
Copyright (c) 2010-2011 Aditya Ivaturi, Gordon Child
Copyright (c) 2014-2017 Daniel Gempesaw
Copyright (c) 2018-2021 George S. Baugh
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.