View Source Wallaby.Query (wallaby v0.30.9)

Provides the query DSL.

Queries are used to locate and retrieve DOM elements from a browser (see Wallaby.Browser). You create queries like so:

Query.css(".some-css")
Query.xpath(".//input")

Form elements

There are several custom finders for locating form elements. Each of these allows finding by their name, id text, or label text. This allows for more robust querying and decouples the query from presentation selectors like CSS classes.

Query.text_field("My Name")
Query.checkbox("Checkbox")
Query.select("A Fancy Select Box")

Query Options

All of the query operations accept the following options:

  • :count - The number of elements that should be found or :any (default: 1).
    • If a :minimum or :maximum is specified, it defaults to nil.
  • :minimum - The minimum number of elements that should be found, or nil (default: nil).
  • :maximum - The maximum number of elements that should be found, or nil (default: nil).
  • :visible - Determines if the query should return only visible elements (default: true).
  • :selected - Determines if the query should return only selected elements (default: :any).
  • :text - Text that should be found inside the element (default: nil).
  • :at - The position (a number or :all) of the element to return if multiple elements satisfy the query. (default: :all)

Query options can also be set via functions by the same names:

Query.css(".names")
|> Query.visible(true)
|> Query.count(3)

Re-using queries

It is often convenient to re-use queries. The easiest way is to use module attributes:

@name_field Query.text_field("User Name")
@submit_button Query.button("Save")

If the queries need to be dynamic then you should create a module that encapsulates the queries as functions:

defmodule TodoListPage do
  def todo_list do
    Query.css(".todo-list")
  end

  def todos(count) do
    Query.css(".todo", count: count)
  end
end

What does my query do?

Wanna check out what exactly your query will do? Look no further than Wallaby.Query.compile/1 - it takes a query and returns the CSS or xpath query that will be sent to the driver:

iex> Wallaby.Query.compile Wallaby.Query.text("my text")
{:xpath, ".//*[contains(normalize-space(text()), \"my text\")]"}

So, whenever you're not sure whatever a specific query will do just compile it to get all the details!

Summary

Functions

Updates a query's at option.

Checks if the provided attribute, value pair is contained anywhere.

Looks for a button (literal button or input type button, submit, image or reset) where the provided selector is the id, name, value, alt or title of the button.

Looks for a checkbox where the provided selector is the id, name or placeholder of the checkbox itself or alternatively the id or the text of the label.

Compiles a query into CSS or xpath so its ready to be sent to the driver

Updates a query's count.

Literally queries for the CSS selector you provide.

Checks if the data attribute is contained anywhere.

Looks for a file input where the selector is the id or name of the file input itself or the id or text of the label.

Looks for a text input field where the provided selector is the id, name or placeholder of the text field itself or alternatively the id or the text of the label.

Looks for a link where the selector is the id, link text, title of the link itself or the alt of an image child node.

Looks for an option that contains the given text.

Looks for a radio button where the provided selector is the id, name or placeholder of the radio button itself or alternatively the id or the text of the label.

Looks for a select box where the provided selector is the id or name of the select box itself or alternatively the id or the text of the label.

Updates a query's selected option.

This function can be used in one of two ways.

Checks if the provided value is contained anywhere.

Updates a query's visibility (visible if true, hidden if false).

Literally queries for the xpath selector you provide.

Types

Link to this type

attribute_key_value_pair()

View Source
@type attribute_key_value_pair() :: {String.t(), String.t()}
@type compiled() :: {:xpath | :css, String.t()}
@type conditions() :: [
  count: non_neg_integer() | :any | nil,
  minimum: non_neg_integer() | nil,
  maximum: non_neg_integer() | nil,
  text: String.t() | nil,
  visible: boolean() | :any,
  selected: boolean() | :any,
  at: non_neg_integer() | :all
]
@type html_validation() :: :bad_label | :button_type | nil
@type method() ::
  :css
  | :xpath
  | :link
  | :button
  | :fillable_field
  | :checkbox
  | :radio_button
  | :option
  | :select
  | :file_field
  | :attribute
@type opts() :: list()
@type result() :: [Wallaby.Element.t()]
@type selector() :: String.t() | attribute_key_value_pair()
@type t() :: %Wallaby.Query{
  conditions: conditions(),
  html_validation: html_validation(),
  method: method(),
  result: result(),
  selector: selector()
}

Functions

Updates a query's at option.

Example

  Query.css(".names")
  |> Query.at(3)
Link to this function

attribute(name, value, opts \\ [])

View Source

Checks if the provided attribute, value pair is contained anywhere.

Link to this function

button(selector, opts \\ [])

View Source

Looks for a button (literal button or input type button, submit, image or reset) where the provided selector is the id, name, value, alt or title of the button.

Link to this function

checkbox(selector, opts \\ [])

View Source

Looks for a checkbox where the provided selector is the id, name or placeholder of the checkbox itself or alternatively the id or the text of the label.

@spec compile(t()) :: compiled()

Compiles a query into CSS or xpath so its ready to be sent to the driver

iex> Wallaby.Query.compile Wallaby.Query.text("my text")
{:xpath, ".//*[contains(normalize-space(text()), \"my text\")]"}
iex> Wallaby.Query.compile Wallaby.Query.css("#some-id")
{:css, "#some-id"}

Updates a query's count.

The count specifies how many elements you expect to be present within the scope of the query and can be any number greater than zero or :any.

Example

# Exactly 2 elements
Query.css(".names > li")
|> Query.count(2)

# Any number of elements
Query.css(".names > li")
|> Query.count(:any)
Link to this function

css(selector, opts \\ [])

View Source

Literally queries for the CSS selector you provide.

Link to this function

data(name, selector, opts \\ [])

View Source

Checks if the data attribute is contained anywhere.

Link to this function

file_field(selector, opts \\ [])

View Source

Looks for a file input where the selector is the id or name of the file input itself or the id or text of the label.

Link to this function

fillable_field(selector, opts \\ [])

View Source

Looks for a text input field where the provided selector is the id, name or placeholder of the text field itself or alternatively the id or the text of the label.

Link to this function

link(selector, opts \\ [])

View Source

Looks for a link where the selector is the id, link text, title of the link itself or the alt of an image child node.

Link to this function

matches_count?(query, count)

View Source
Link to this function

option(selector, opts \\ [])

View Source

Looks for an option that contains the given text.

Link to this function

radio_button(selector, opts \\ [])

View Source

Looks for a radio button where the provided selector is the id, name or placeholder of the radio button itself or alternatively the id or the text of the label.

Link to this function

select(selector, opts \\ [])

View Source

Looks for a select box where the provided selector is the id or name of the select box itself or alternatively the id or the text of the label.

Updates a query's selected option.

Examples

  Query.css("#select-dropdown")
  |> Query.selected(true)

  Query.css("#select-dropdown")
  |> Query.selected(false)
Link to this function

specific_element_requested(query)

View Source
Link to this function

text(query_or_selector, value_or_opts \\ [])

View Source

This function can be used in one of two ways.

The first is by providing a selector and possible options. This generates a query that checks if the provided text is contained anywhere.

Example

  Query.text("Submit", count: 1)

The second is by providing an existing query and a value to set as the text option.

Note that the text you're querying for must appear in a single element. To assert on text that appears in multiple (potentially nested) elements, rather than using assert_has/2 in combination with Query.text/2, use assert_text/{2,3} directly.

Examples

Querying the text of a specific element

  submit_button = Query.css("#submit-button")

  update_button = submit_button |> Query.text("Update")
  create_button = submit_button |> Query.text("Create")

Asserting on the text of a single element

```
submit_button = Query.css("#submit-button")
assert_has(session, Query.text(submit_button, "Create"))
```

Asserting on the text of nested elements

HTML:

```
<div id="unread-notifications">
  Unread messages:
  <strong>1 message</strong>
</div>
```

Test:

```
assert_text(session, "Unread messages: 1 message")

notifications_block = Query.css("#unread-notifications")
# It would *not* work to query as: Query.text(notifications_block, "Unread messages: 1 message")
assert_has(session, Query.text(notifications_block, "Unread messages:"))
```
Link to this function

text_field(selector, opts \\ [])

View Source

See Wallaby.Query.fillable_field/2.

Link to this function

value(selector, opts \\ [])

View Source

Checks if the provided value is contained anywhere.

Updates a query's visibility (visible if true, hidden if false).

Examples

  Query.css("#modal")
  |> Query.visible(true)

  Query.css("#modal")
  |> Query.visible(false)
Link to this function

xpath(selector, opts \\ [])

View Source

Literally queries for the xpath selector you provide.