Snippets

Snippets are reusable test cases in scope of project. So you can create a snippet and refer to it in your test suites. For example, to login a user we need to perform multiple actions: visit login form, fill it out and submit. Instead of adding these test steps to every test case for logged in user we can simply create once a snippet and then refer to it from many test cases. Besides, to every reference we can assign a set of template variables that can be addressed in the body of the snippet. In other words, we can execute the same snippet, but giving it a different input every time.

Using Snippets by Example

In order to illustrate the concept we are going to create a simple test case for an imaginary ACME forum app. The test plan would be to register a user, sign in with the newly created account and check the user is really logged in.

Creating Registration Test Case

Let's start with user registration flow. First we need to define our target elements to fill out the registration form and the following GDPR one:

Now we can create a test case:

Here the description of test steps:

  • we assign a dynamic template variable TEST_EMAIL by using template expression counter(), meaning with every test run will get a new unique email address like joe+test1@gmail.com, joe+test2@gmail.com and so on

  • we navigate to register page (given that we defined a template variable BASE_URL)

  • we fill in email field from TEST_EMAIL variable

  • we fill in username input with random dummy name generated by faker template expression

  • we fill in password and password confirmation fields from earlier defined TEST_PASSWORD template variable

  • we click submit button

  • we wait until form submitted and the page with GDPR form loaded

  • we make a screenshot

  • we tick on consent checkboxes

  • we click submit button

So we have registered the user. The next test case we can simply place next ot the first one. But think ahead - we my have multiple flows for logged-in state and which one we will need to start with login procedure. So we rather create a snippet for login and reuse it in future test cases

Creating Login Snippet

Next we press Snippets in the main menu

Snippets are quite similar to suites. They have local targets and test cases.

We start by defining targets for the login form

Now we just into Snippets tab and create the test case:

As we are done with the snippet we can navigate back to the suite:

We create an test case "user gets logged in" and click under it to the Add a reference button

In the following modal window we select our recently created snippet:

After extending the test case with other test steps it looks like that:

During the test run first all the test steps of the snippet are executed, so user gets logged in. We wait until the page is ready (login form doesn't contain .categories selector, but landing page does). Now we can assert that page header changed (has #user-header-name).

Running Snippets with Diverse Input

In the example above we used TEST_EMAIL template variable defined during registration flow. But imagine that we have a number of already registered accounts (let's say one is inactive, one is active, one is privileged) and we want sequencely log in with each one and assert that the application responds as intended. What we can do is reusing "user logs in" snippet, but with diverse emails. Click on the Edit action next to the reference test step:

You get a modal window that we know from the example below. Click to Local Template Variables to expand the template editing interface and add TEST_EMAIL variable:

Now you clone the reference and edit it again. Remove TEST_EMAIL and add it again with a new value. As you run the tests the references will execute the snippet with the values we assigned.

Last updated