WebDriver.Session
This module runs a browser session. Use these functions to drive the browser. The ‘name’ parameter is always the session name atom that is set when you start the session.
Example Session:
iex(1)> config = %WebDriver.Config{browser: :phantomjs, name: :test_browser}
%WebDriver.Config{browser: :phantomjs, name: :test_browser, root_url: ""}
iex(2)> WebDriver.start_browser config
{:ok, #PID<0.133.0>}
iex(3)> WebDriver.start_session :test_browser, :session_name
{:ok, #PID<0.137.0>}
iex(4)> WebDriver.Session.url :session_name, "http://www.google.com"
{:ok,
%WebDriver.Protocol.Response{request: %WebDriver.Protocol.Request{body: "{"url":"http://www.google.com"}",
headers: ["Content-Type": "application/json;charset=UTF-8",
"Content-Length": 31], method: :POST,
url: "http://localhost:53094/wd/hub/session/1f44bb00-2698-11e4-8216-49b0aee6bf1f/url"},
session_id: "1f44bb00-2698-11e4-8216-49b0aee6bf1f", status: 0, value: %{}}}
iex(5)> WebDriver.Session.url :session_name
"http://www.google.com.au/?gfe_rd=cr&ei=ao7xU7TGNs3C8gednYCYBQ"
iex(6)> WebDriver.stop_session :session_name
:ok
iex(7)> WebDriver.stop_browser :test_browser
:ok
iex(8)>
Summary
active_element(name) | Get the element on the page that currently has focus |
back(name) | Navigate back in the browser history |
close_window(name) | Closes the current window |
cookies(name) | Retreive all the cookies associated with the current page |
delete_cookie(name, cookie_name) | Delete the cookie with the given name |
delete_cookies(name) | Delete all cookies for the current page |
element(name, using, value) | Retreive an element from the page using the specified search strategy. Returns the first element found that fits the search criteria |
element(name, using, value, start_element) | Retreive an element from the page starting from the specified element using the specified search strategy |
elements(name, using, value) | Retreive all elements from the page using the specified search strategy |
elements(name, using, value, start_element) | Retreive all elements starting from the specified element using the specified search strategy |
execute(name, script, args \\ []) | Execute Javascript in the browser and return the result |
execute_async(name, script, args \\ []) | Execute Javascript asynchronously in the browser and return the result |
forward(name) | Navigate forward in the browser history |
frame(name, id) | Change the frame that has focus in the current window. https://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/frame |
javascript_enabled?(name) | True if javascript is enabled for this session |
keys(name, value) | Send a list of keystrokes to the currently active element.
If you want to send non-printable keystrokes you must call
|
maximize_window(name, window_handle \\ "current") | Maximise the specified window. Use “current” or simply do not specify a handle to maximise the current window |
negotiated_capabilities(name) | Returns the negotiated capabilities of the current session |
orientation(name) | Get the current browser screen orientation.
Only works on browsers with the |
orientation(name, screen_orientation) | Set the current browser screen orientation https://code.google.com/p/selenium/wiki/JsonWireProtocol#POST_/session/:sessionId/orientation |
refresh(name) | Refresh the current page in the browser |
rotatable?(name) | True if this session supports device rotation |
screenshot(name) | Get a PNG screenshot of the current page |
session(name) | Get details about the current session on the WebDriver server |
sessions(name) | List all the sessions on the WebDriver browser |
set_async_script_timeout(name, ms) | Set the script timeout for the session |
set_cookie(name, cookie) | Set a cookie for the current page |
set_cookie(name, cookie_name, value, path, domain, expiry \\ in_one_hour()) | |
set_implicit_wait_timeout(name, ms) | Set the implicit wait timeout for the session |
set_timeout(name, type, ms) | Set timeouts on the server. Parameters must include the type of timeout and the length in milliseconds (ms). Valid types are “script” and “implicit” |
source(name) | Retreive the curent page source |
start_link(state, name) | Starts the session |
start_session(name, desired_capabilities \\ %{}) | Start a session with the desired capabilities on the browser. This is automatically called when the Session server starts, but in some cases you may want to stop and restart sessions without stopping and restarting the Session server |
status(name) | Returns the status of the WebDriver server |
stop(name) | Stop the session |
stop_session(name) | Stop the session with the given name. Does not stop the Erlang server, just sends a request to the WebDriver server to stop the session |
takes_screenshot?(name) | True if this session can take screenshots |
title(name) | Retreive the current page title |
url(name) | Get the current page URL |
url(name, url) | Navigate to the specified url |
window(name, window_handle) | Change the focus to another window |
window_handle(name) | Get the handle of the current window. A window handle is an opaque reference used for various window related functions |
window_handles(name) | Get all the window handles associated with the browser. Returns a list of window handles |
window_size(name) | Retreive the window size. If a window handle is not specified it retreives the current window |
window_size(name, window_handle, width, height) | Set the window size |
Functions
Get the element on the page that currently has focus.
https://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/element/active
Navigate back in the browser history.
https://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/back
Closes the current window.
https://code.google.com/p/selenium/wiki/JsonWireProtocol#DELETE_/session/:sessionId/window
Retreive all the cookies associated with the current page.
https://code.google.com/p/selenium/wiki/JsonWireProtocol#GET_/session/:sessionId/cookie
Delete the cookie with the given name.
https://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/cookie/:name
Delete all cookies for the current page.
https://code.google.com/p/selenium/wiki/JsonWireProtocol#DELETE_/session/:sessionId/cookie
Retreive an element from the page using the specified search strategy. Returns the first element found that fits the search criteria.
The return value is an element reference that can be used by functions in the WebDriver.Element module for further queries.
https://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/element
The parameter ‘using’ must be a valid search strategy. The parameter ‘value’ is a string to search for.
Valid search strategies are:
* :class - Search for an element with the given class attribute.
* :class_name - alias for :class
* :css - Search for an element using a CSS selector.
* :id - Find an element with the given id attribute.
* :name - Find an element with the given name attribute.
* :link - Find an link element containing the given text.
* :partial_link - Find a link element containing a superset of the given text.
* :tag - Find a HTML tag of the given type.
* :xpath - Use [XPath](http://www.w3.org/TR/xpath/) to search for an element.
Parameters [using :: atom, value :: String]
Returns: A (WebDriver.Element)[/WebDriver.Element.html] struct.
## Examples
iex(12)> WebDriver.Session.element :test, :css, "img.logo"
%WebDriver.Element{id: ":wdc:1373691496542", session: :test}
iex(13)> WebDriver.Session.element :test, :id, "branding"
%WebDriver.Element{id: ":wdc:1373691496543", session: :test}
Retreive an element from the page starting from the specified element using the specified search strategy.
https://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/element/:id/element
See WebDriver.Session.element/3 for details on search strategies.
Parameters: [using :: atom, value :: String, start_element :: WebDriver.Element.Reference]
Retreive all elements from the page using the specified search strategy.
https://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/element
See element/3 for details on the parameters used. Returns a list of Element structs.
Retreive all elements starting from the specified element using the specified search strategy.
https://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/element/:id/elements
See element/3 for details on the parameters used. The start_element parameter must be a WebDriver.Element struct. Returns a list of Element structs.
Execute Javascript in the browser and return the result.
https://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/execute
Example:
```
iex> WebDriver.Session.execute :test, "return 2+2;"
4
iex> WebDriver.Session.execute :test, "return arguments[0] * arguments[1];", [5,3]
15
```
Parameters: [script :: String, args :: List]
Returns: The Javascript return value, which may be a number,
string, list or object (map).
Execute Javascript asynchronously in the browser and return the result.
https://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/execute_async
Parameters: [script :: String, args :: List]
Returns: The Javascript return value, which may be a number,
string, list or object (map).
Navigate forward in the browser history.
https://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/forward
Change the frame that has focus in the current window. https://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/frame
Parameters: %{id: string | number | :null | WebElement}
Send a list of keystrokes to the currently active element.
If you want to send non-printable keystrokes you must call
Webdriver.Keys.key(key_symbol)
to convert it properly.
https://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/keys
Parameters: [ value :: String ]
Maximise the specified window. Use “current” or simply do not specify a handle to maximise the current window.
Returns the negotiated capabilities of the current session.
Get the current browser screen orientation.
Only works on browsers with the rotatable
capability.
https://code.google.com/p/selenium/wiki/JsonWireProtocol#GET_/session/:sessionId/orientation
Set the current browser screen orientation https://code.google.com/p/selenium/wiki/JsonWireProtocol#POST_/session/:sessionId/orientation
Parameters: [screen_orientation :: atom]
Screen orientaton can be either :portrait or :landscape.
Refresh the current page in the browser.
https://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/refresh
Get a PNG screenshot of the current page.
https://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/screenshot
Returns: PngImage :: Binary
Get details about the current session on the WebDriver server.
https://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId
Returns a capability record.
List all the sessions on the WebDriver browser.
https://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId
Returns a list of session ids.
Set the script timeout for the session.
Parameters: [ms :: number]
Set a cookie for the current page.
https://code.google.com/p/selenium/wiki/JsonWireProtocol#POST_/session/:sessionId/cookie
Parameters: [cookie :: object]
Set the implicit wait timeout for the session.
Parameters: [ms :: number]
Set timeouts on the server. Parameters must include the type of timeout and the length in milliseconds (ms). Valid types are “script” and “implicit”
Parameters type: “script”|”implicit”, ms: number
Retreive the curent page source.
https://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/source
Start a session with the desired capabilities on the browser. This is automatically called when the Session server starts, but in some cases you may want to stop and restart sessions without stopping and restarting the Session server.
Parameters:
name : The session server process to start the session on.
desired_capabilities: Capability
Returns the status of the WebDriver server.
https://code.google.com/p/selenium/wiki/JsonWireProtocol#/status
Stop the session with the given name. Does not stop the Erlang server, just sends a request to the WebDriver server to stop the session.
Retreive the current page title.
https://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/title
Get the current page URL
https://code.google.com/p/selenium/wiki/JsonWireProtocol#GET_/session/:sessionId/url
Returns: url :: String
Navigate to the specified url.
https://code.google.com/p/selenium/wiki/JsonWireProtocol#POST_/session/:sessionId/url
Note that this may take some time and calling other functions too soon may fail if the implicit wait is not set at a high enough value.
## Examples
iex> WebDriver.Session.url :session
"about:blank"
iex> WebDriver.Session.url :session, "http://www.google.com.au/"
iex> WebDriver.Session.url :session
"http://www.google.com.au/"
Parameters [url :: String]
Change the focus to another window.
https://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/window
The window may be specified by the server assigned window handle or the value of it’s name attribute.
Parameters: %{window_handle : string}
Get the handle of the current window. A window handle is an opaque reference used for various window related functions.
https://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/window_handle
Returns: window_handle :: String
Get all the window handles associated with the browser. Returns a list of window handles.
https://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/window_handles
Returns: window_handles :: List[String]
Retreive the window size. If a window handle is not specified it retreives the current window.
Returns: %{height: number, width: number}
Set the window size.
NOTE: Firefox can only do manipulation on the currently focussed window. You MUST pass the window_handle parameter as “current” for Firefox or you will get an error response.
Parameters: [height :: number, width :: number]