Understanding Actions and Assertions

The previous tutorial on tests showed how a test brings a collection of steps together to examine a full user journey.

Here’s the test we previously created for testing Google search:

# examples/test/google-search-query-literal.yml
config:
  browser: chrome
  url: https://www.google.com

"verify Google is open":
  assertions:
    - $page.url is "https://www.google.com"
    - $page.title is "Google"

"query 'example'":
  actions:
    - set $".gLFyf.gsfi" to "example"
    - click $".FPdoLc.VlcLAe input[name=btnK]"

  assertions:
    - $page.title is "example - Google Search"

In this tutorial section, we’ll look at how actions and assertions are formed and what we can do with them.

Actions

An action is something that an end user can do in a browser, such as click on link, enter text into a field or submit a form. Each specific thing you can do within a browser when using a web page is an action.

We can click elements, set element values or submit elements, as well as take a few more types of action.

All actions take the form of a verb (click, set, submit) followed in many cases by the element on which to apply the action and, possibly, some additional arguments (such as when setting an element value).

Each test starts with a set of actions to get the browser into an expected state.

Action Examples

- click $".sign-in-form .submit-button"
- click $".listed-item":1
- click $imported_page_model.elements.element_name
- click $elements.element_name
- set $"#sign-in-form .username" to "user@example.com"
- set $"#sign-in-form .username" to "\"user@example.com\"" # (literal double quotes)
- set $imported_page_model.elements.username to "user@example.com"
- set $elements.element_name to "user@example.com"
- set $elements.element_name to $data.field_name
- set $".selector" to $page.title # odd but valid
- set $"//foo" to $page.url       # also odd but valid
- set $".input1" to $".input2"    # also odd but valid
- submit $"#sign-in-form"
- submit $imported_page_model.elements.submit_button
- submit $elements.element_name
- wait 1
- wait 15
- wait $data.duration
- wait $env.DURATION
- wait-for $"#asychronously-loaded-content"
- wait-for $imported_page_model.elements.delayed_element_name
- wait-for $elements.delayed_element_name
- reload
- forward
- back

Action Verb List

Technical Reference: Action Verbs

Assertions

Actions get the browser into an expected state. Assertions then verify that the state is as expected.

In plain English, we might say “Verify that the page title is “Google”. A basil assertion for this reads as $page.title is "Google".

If an assertion is found to be incorrect (if the page <title> is not the word “Google”) your test will have failed.

Assertion Examples

- $".selector" is "Hello!"
- $".banner .image".src is "http://example.com/images/banner.png"
- $page.title is "Homepage"
- $page.title is "\"Homepage\"" # (literal double quotes)
- $imported_page_model.elements.sign_in_button is "Sign in"
- $elements.element_name is $page.url # odd but valid
- $elements.element_name is $data.expected_element_value
- $elements.element_name is $env.KEY
- $".heading" is-not "Error"
- $".user-container .status".data-status is-not "active"
- $page.title is-not "Homepage"
- $page.title is-not "\"Homepage\"" (literal double quotes)
- $imported_page_model.elements.sign_in_button is-not "Sign in"
- $elements.element_name is-not $page.url # odd but valid
- $elements.element_name is-not $data.expected_element_value
- $elements.element_name is-not $env.KEY
- $".heading" includes "welcome"
- $".heading".title includes "welcome"
- $page.title includes "page"
- $imported_page_model.elements.sign_in_button includes "Sign"
- $elements.element_name includes $page.url # odd but valid
- $elements.element_name includes $data.key
- $elements.element_name includes $env.KEY
- $".heading" includes "error"
- $".heading".title includes "error"
- $page.title excludes "error"
- $imported_page_model.elements.sign_in_button excludes "Log"
- $elements.element_name excludes $page.url # odd but valid
- $elements.element_name excludes $data.key
- $elements.element_name excludes $env.KEY
- $".form .profile" matches "/.*/"
- $".form":action matches "/\/login\//"
- $page.title matches "/homepage$/i"
- $imported_page_model.elements.sign_in_button matches "/^Sign/"
- $elements.element_name matches $data.regex
- $elements.element_name matches $env.REGEX # odd way to go about it but valid
- $".selector" exists
- $".profile":data-image exists
- $"#logo" exists
- $imported_page_model.elements.sign_in_button exists
- $element_name exists
- $".username-field":disabled not-exists
- $"#logo" not-exists
- $imported_page_model.elements.sign_in_button not-exists
- $element_name not-exists

Assertion Operator List

Technical Reference: Assertion Operations