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. Let's say we have a test case "user registers and activates the account" where Puppetry visits forum register page, fills out the registration form and submits it. So we have a bran new user in the system and can login to run test flows for an authorized user. However to get the initial state in the beginning of every test group we need to repeat and repeat the login flow (visit login page, fill out the form, submit). Why not to make a snippet and reuse it?

Creating Login Snippet

Next we press Project / 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