Importing Steps Into Your Test

In the previous tutorial on tests we opened https://www.google.com/ and then we performed a search. The Google search form is shown on the homepage which is why we first verify that we’re at the homepage before performing a search.

Searching is not the only action we can perform on the homepage. We could have plenty of tests that take the form:

  • verify that we’re at the homepage
  • do something

By defining the verify that we’re at the homepage step independently, we can use the step within any test without needing to repeatedly redefine the step.

Defining and Importing a Step

This is the same as the first step from the test tutorial placed into its own file.

# examples/step/google-assert-open-literal.yml
assertions:
  - $page.url is "https://www.google.com"
  - $page.title is "Google"

Here we import and use the step within our test.

# examples/test/google-import-assert-open.yml
config:
  browser: chrome
  url: https://www.google.com

imports:
  steps:
    google_assert_open: "../step/google-assert-open-literal.yml"

"verify Google is open":
  use: google_assert_open

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

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

Imports are defined in the imports section within the steps section. We will see later that you can import more than just steps.

A step import has an import name (google_open) and an import path (step/google-assert-open-literal.yml).

The import name is something you choose. It can be anything you like. We use the import name to refer to the step later within the test An import name that is concise and which makes sense later in the context of the test is a good idea.

The import path tells us where to look, relative to the test, to find the step to be imported. Here we defined the step in a file at step/google-assert-open-literal.yml relative to our test.

Referencing an Imported Step

Compare our previous test that defined the step directly to how we are now using the imported step.

Previously

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

Now

"verify Google is open":
  use: google_assert_open

Extending an Imported Step

An imported step can have additional assertions applied. As actions always come before assertions and as an imported step will have assertions defined, an imported step cannot have additional actions.

# examples/test/google-import-assert-open-additional-assertions.yml
config:
  browser: chrome
  url: https://www.google.com

imports:
  steps:
    google_assert_open: "../step/google-assert-open-literal.yml"

"verify Google is open":
  use: google_assert_open

  assertions:
    - $page.title excludes "Error"

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

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

That’s starting to look quite nice, except I feel that those identifiers (such as .gLFyf.gsfi) are going to get messy if we keep having to write them out in the middle of each test that needs them.

Next? Page models!