limo.api
The core API wrapper around selenium webdrivers
*default-interval*
dynamic
*default-timeout*
dynamic
The default timeout in milliseconds until limo gives up attempting to try an action.
Defaults to 15 seconds (15000).
This value is used for wait-* set of functions which most other function calls
rely upon.
The default value is generous enough to try and cover a variety of machine
speeds, but you may find value in tweaking this parameter when checking for a
negative state (eg - verifying that a checkbox isn't checked).
*driver*
dynamic
The implied selenium WebDriver instance to use when invoking functions with api calls.
All API functions can explicitly accept the WebDriver as the first argument.
Otherwise if that argument is excluded, then this dynamically bounded var is
used instead.
Example:
> (limo.api/click "#button")
;; becomes
> (limo.api/click *driver* "#button")
Defaults to `nil`. Use [[set-driver!]] as a way to quickly set this variable.
active-window
(active-window)(active-window driver)
Returns the window id (string) of the current window that the driver is focused on.
The active-window is the window where (switch-to-window (active-window)) is a no-op.
See [[switch-to-window]] to switch focused window.
See [[all-windows]] to list all window ids
See [[active-window]] to get the current window id
IMMEDIATE:
This function is considered immediate, and does not poll using [[wait-until]]
or [[wait-for]]. Thus, it is unaffected by [[*default-timeout*]].
all-windows
(all-windows)(all-windows driver)
Returns a sequence of all window ids (strings) that refer to specific browser
windows the driver controls.
IMMEDIATE:
This function is considered immediate, and does not poll using [[wait-until]]
or [[wait-for]]. Thus, it is unaffected by [[*default-timeout*]].
allow-backspace?
(allow-backspace? e)
Returns a true if the given element can handle backspace keypresses.
Hitting backspace on anything except selects, radios, checkboxes will cause
the browser to go back to the previous page.
attribute
(attribute selector-or-element attr)(attribute driver selector-or-element attr)
Returns an element's attribute value for a given attribute name.
by
(by s)
Creates a Selenium By instance (aka - a selenium element search query) to
find an HTML element.
In general, you shouldn't probably be calling this function directly. All limo
functions call `element` which internally calls this function to find an
element.
This function accepts either a String, Selenium WebElement or a map containing
one of the following keys to indicate how to find a DOM element:
- :css or :css-selector => search by CSS selector
- :id => search by element ID attribute
- :xpath => search by element xpath
- :tag-name => search by element tag name (eg - p, h1, div)
- :link-text => search by anchor text
- :partial-link-text => search by anchor text containing some text
- :name => search by name attribute (eg - input fields)
- :class-name => search by a css class name the element has
Examples:
;; return By instance to query by CSS
> (by "h1")
;; Implies
> (by {:css "h1"})
clear-fields
(clear-fields fields)
click
(click selector-or-element)(click driver selector-or-element)
Clicks on a given element.
click-when-visible
(click-when-visible selector)
Clicks on a given element, but makes sure it's visible before doing so.
contains-text?
(contains-text? selector-or-element expected-substr)(contains-text? driver selector-or-element expected-substr)
Returns true if the element has innerText contains a given value. The comparison is
case-insensitive and will timeout if a match does not occur.
current-url
(current-url)(current-url driver)
Returns the current url the browser is on.
IMMEDIATE:
This function is considered immediate, and does not poll using [[wait-until]]
or [[wait-for]]. Thus, it is unaffected by [[*default-timeout*]].
current-url-contains?
(current-url-contains? substr)
Returns true if the current url contains some text
delete-all-cookies
(delete-all-cookies)(delete-all-cookies driver)
Deletes all cookies associated with the page the browser is currently on.
There is no way in Selenium's APIs to clear all cookies in a driver without
re-creating the driver. If you wish to reuse a driver, you must navigate to
every domain and call this function to clear cookies.
IMMEDIATE:
This function is considered immediate, and does not poll using [[wait-until]]
or [[wait-for]]. Thus, it is unaffected by [[*default-timeout*]].
element
(element selector-or-element)(element driver selector-or-element)
Returns a selenium WebElement of a selector that [[by]] accepts.
If selector-or-element is an [[element?]], then the value is simply returned.
IMMEDIATE:
This function is considered immediate, and does not poll using [[wait-until]]
or [[wait-for]]. Thus, it is unaffected by [[*default-timeout*]].
element-matches
(element-matches selector-or-element pred)(element-matches driver selector-or-element pred)
element?
(element? e)
Helper function. A predicate that indicates if the given value is a selenium WebElement instance.
elements
(elements selector-or-elements)(elements driver selector-or-elements)
Returns a sequence of WebElement instances that match the selector that
[[by]] accepts.
If selector-or-element is an [[element?]], then the value is simply returned.
IMMEDIATE:
This function is considered immediate, and does not poll using [[wait-until]]
or [[wait-for]]. Thus, it is unaffected by [[*default-timeout*]].
execute-script
(execute-script driver js & js-args)
Evaluates a given javascript string on the page.
Generally you want to control most of your interacts via the supported browser
operations, but sometimes it's needed to run some javascript on the page
directly - like activating a verbose logging mode, or forcing one into a
specific A/B test.
Parameters:
- `driver` is the selenium driver to use
- `js` is the javascript string to eval on the page.
- `js-args` is the variadic arguments of values to pass into the eval
function. These values are limited to what can be translated to javascript,
which are the following:
- numbers
- booleans
- strings
- WebElements (objects returned via [[element]])
- lists of any above types
Note:
The javascript script string is executed in anonymous closure like this:
(function(){ $JS })($JS-ARGS);
which means you'll need to use `arguments` in `js` to access arguments pass
through from clojure to javascript.
Returns:
The return value is whatever the return value of the javascript eval
expression, which are also constrainted to the same times that can be
translated as a js-args value.
IMMEDIATE:
This function is considered immediate, and does not poll using [[wait-until]]
or [[wait-for]]. Thus, it is unaffected by [[*default-timeout*]].
exists?
(exists? selector-or-element)(exists? driver selector-or-element)
Returns a boolean indicates if the given selector (that [[by]] accepts)
matches an element that is not the current page the selenium browser is
viewing.
IMMEDIATE:
This function is considered immediate, and does not poll using [[wait-until]]
or [[wait-for]]. Thus, it is unaffected by [[*default-timeout*]].
has-class
(has-class q class)
Returns a true if a given element has a class on it.
implicit-wait
(implicit-wait timeout)(implicit-wait driver timeout)
Sets the driver's implicit wait interval. The implicit wait interval is poll
interval is how much a browser will wait.
There are two kinds of waits:
- The test suite polls & waits (see [[*default-interval*]],
[[*default-timeout*]], [[wait-until]], [[wait-for]], [[wait-until-clickable]],
[[wait-for-else]]). Here the test suite / limo is responsible for polling
and asking the browser to see if an element exists.
- The browser waits for an element to appear. This is what [[implicit-wait]] configures.
For example, if we ask the browser to click on #button, but it isn't
immediately available, the browser will use the implicit-wait value to
internally wait up to the given time until returning an element not found
error.
Read Selenium's explaination of waits for another perspective of the same thing:
http://www.seleniumhq.org/docs/04_webdriver_advanced.jsp#explicit-and-implicit-waits
in-new-window
macro
(in-new-window opts action do-body)(in-new-window driver {:keys [auto-close?]} action do-body)
Creates a temporary new browser window that `do-body` runs inside.
input-text
Alias to [[send-keys]]. Sends keypresses to a given element. Types on a given input field.
Characters can be strings or vector of strings.
invisible?
(invisible? selector-or-element)(invisible? driver selector-or-element)
Returns true if the given element is invisible.
normalize-fields
(normalize-fields fields)
Converts all string values that indicate typing text into functions
num-elements=
(num-elements= selector-or-element expected-count)(num-elements= driver selector-or-element expected-count)
options
(options selector-or-element)(options driver selector-or-element)
Returns a sequence of all form value and visible text for a given drop-down
quit
(quit)(quit driver)
Closes the driver, which implies closing all browser windows the driver has created.
IMMEDIATE:
This function is considered immediate, and does not poll using [[wait-until]]
or [[wait-for]]. Thus, it is unaffected by [[*default-timeout*]].
read-json-logs!
(read-json-logs! log-type-kw)(read-json-logs! driver log-type-kw)
Identical read-logs!, but parses the message body as JSON.
NOTE: the same limitations as read-logs! applies: that is, that the browser
may discard the log information after the request to retrive the logs occurs.
This is known to be useful with Chrome's performance logs to get network and
rendering information. Chrome's performance log data is encoded in JSON.
read-json-logs! is pretty low-level in comparison to most of the other limo apis.
Considering using with-stolen-performance-logs!
read-logs!
(read-logs! log-type-kw)(read-logs! driver log-type-kw)
Retrieves logs of a given type from the browser being control by selenium.
NOTE: The browser may discard the log information after the request to retrive
the logs occurs. This means multiple calls to readonly-logs! can return different
results.
> (count (read-logs!)) => 5
> (count (read-logs!)) => 0
read-logs! is pretty low-level in comparison to most of the other limo apis.
Considering using with-stolen-performance-logs!
refresh
(refresh)(refresh driver)
Refreshes/Reloads the current page the browser is on.
IMMEDIATE:
This function is considered immediate, and does not poll using [[wait-until]]
or [[wait-for]]. Thus, it is unaffected by [[*default-timeout*]].
screenshot
(screenshot name)(screenshot name dir-f)
select-by-text
(select-by-text selector-or-element value)(select-by-text driver selector-or-element value)
Selects a given option in a drop-down element by the user-visible text on the element.
Useful if you know you want to select a given option that is visible on screen
and its value changes more often that its display text.
select-by-value
(select-by-value selector-or-element value)(select-by-value driver selector-or-element value)
Selects a given option in a drop-down element by the server-provided value of the element.
Useful if you know you want to select a given option that has a constant
value, but may change its user-visible text more often.
selected?
(selected? selector-or-element)(selected? driver selector-or-element)
Returns true if the given element is selected (eg - checkbox)
send-keys
(send-keys selector-or-element s)(send-keys driver selector-or-element s)
Sends keypresses to a given element. Types on a given input field.
Characters can be strings or vector of strings.
set-checkbox
(set-checkbox selector checked?)
Sets a checkbox element to the given state (true = check, false = unchecked)
set-driver!
(set-driver! d)
Sets the current implied active selenium WebDriver ([[*driver*]]).
Note: (set-driver! nil) is a no-op.
submit
Alias to [[click]]. Typically reads nice when referring to submit buttons.
switch-to-frame
(switch-to-frame frame-element)(switch-to-frame driver frame-element)
Changes the driver's DOM queries to target a given frame or iframe.
Drivers do no walk through child frames/iframes' DOM elements. This function
allows all subsequent calls (eg - [[element]]) will target elements inside
that frame.
See [[switch-to-main-page]] to restore querying against the page elements.
See [[switch-to-window]] to query against windows.
switch-to-main-page
(switch-to-main-page)(switch-to-main-page driver)
Changes the driver's DOM queries to target the main page body.
Drivers do no walk through child frames/iframes' DOM elements. This function
allows all subsequent calls (eg - [[element]]) will target elements directly
on the page.
See [[switch-to-frame]] to query against iframes / frames.
See [[switch-to-window]] to query against windows.
IMMEDIATE:
This function is considered immediate, and does not poll using [[wait-until]]
or [[wait-for]]. Thus, it is unaffected by [[*default-timeout*]].
switch-to-window
(switch-to-window window-handle)(switch-to-window driver window-handle)
Changes the driver's DOM queries to target a given browser window.
See [[switch-to-frame]] to query against iframes / frames.
See [[all-windows]] to list all window ids
See [[active-window]] to get the current window id
tag
(tag selector-or-element)(tag driver selector-or-element)
Returns an element's html tag name.
take-screenshot
(take-screenshot)(take-screenshot format)(take-screenshot format destination)(take-screenshot driver format destination)
text
(text selector-or-element)(text driver selector-or-element)
Returns an element's innerText.
text=
(text= selector-or-element expected-value)(text= driver selector-or-element expected-value)
Returns true if the element has innerText of a given value. The comparison is
case-insensitive and will timeout if a match does not occur.
to
(to url)(to driver url)
Navigates to a given url. As if one types on the address bar.
IMMEDIATE:
This function is considered immediate, and does not poll using [[wait-until]]
or [[wait-for]]. Thus, it is unaffected by [[*default-timeout*]].
toggle
Alias to [[click]]. Typically reads nice when referring to checkboxes
value
(value selector-or-element)(value driver selector-or-element)
Returns the input's value of a given input element
value=
(value= selector-or-element expected-value)(value= driver selector-or-element expected-value)
Returns true if the element has a value of a given string. The comparison is
case-insensitive and will timeout if a match does not occur.
visible?
(visible? selector-or-element)(visible? driver selector-or-element)
Returns true if the given element is visible?
wait-for
macro
(wait-for driver narration & body)
A specialized version of wait-until that includes narration (printing to stdout) the action that is taken.
wait-for-else
macro
(wait-for-else driver narration default-value & body)
Like wait-for, but has a default return value if the waiting predicate fails.
wait-until
(wait-until pred)(wait-until pred timeout)(wait-until pred timeout interval)(wait-until driver pred timeout interval)
Runs a given predicate pred repeatedly until a timeout occurs or pred returns
a truthy value.
This is usually called by other limo APIs unless IMMEDIATE is indicated in the
docs.
Parameters:
- `pred` can return a value, and that becomes the return value of wait-until.
- `timeout` in the time in milliseconds to wait for. Uses
[[*default-timeout*]] if not explicitly specified.
- `interval` is the time in milliseconds between calling `pred`. Defaults to
0 because [[implicit-wait]] is probably what you want to set.
- `driver` is an alternative WebDriver instance to use other than [[*driver*]].
Waits & polls:
There are 3-values that dicate polling in Limo: timeout interval, sleep
interval, and browser wait interval.
- Timeout interval is the maximum time in Selenium (& Limo) which an action
can take in its entirety.
- Sleep interval is the Thead.sleep call Selenium calls inbetween calls to
`pred` returning a falsy value.
- Browser wait interval (aka - ImplicitlyWait) is the maximum time the browser
will wait for an element that matches to appear.
In our experience, the browser wait interval is usually what keeps actions
held. An action like "click this button" relies on the button existing
before it can be clicked.
StaleElementReferenceException are captured and indicate a falsy return
value of `pred`. Other exceptions will be rethrown.
wait-until-clickable
(wait-until-clickable selector)(wait-until-clickable driver selector timeout)
A specialized version of wait-until that waits until an element is clickable.
window-resize
(window-resize dimensions-map)(window-resize driver {:keys [width height], :as dimensions-map})
Resizes the current window to the given dimensions.
IMMEDIATE:
This function is considered immediate, and does not poll using [[wait-until]]
or [[wait-for]]. Thus, it is unaffected by [[*default-timeout*]].
window-size
(window-size)(window-size driver)
Returns the current window's size
IMMEDIATE:
This function is considered immediate, and does not poll using [[wait-until]]
or [[wait-for]]. Thus, it is unaffected by [[*default-timeout*]].
with-window-size
macro
(with-window-size new-size & body)
with-window-size*
(with-window-size* new-size actions)