# 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](https://docs.puppetry.app/examples). 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:

![Targets addressed in registration flow](https://2843146877-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LdyKJ-zzeS2hB7DF04J%2F-LhteCGb4i-6eVd5g1K4%2F-LhteUTFL_yWl4Pyi3uz%2Fsnippet-targets.png?alt=media\&token=44f1d13b-f6c4-4ccb-9189-4730c9537b2a)

Now we can create a test case:

![User registration test case](https://2843146877-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LdyKJ-zzeS2hB7DF04J%2F-LhGU71FOcPX6hRctbd6%2F-LhGUcsP7hjgq2C9Xab6%2FP2-snippets-register.png?alt=media\&token=4cc92e60-875d-4eeb-af3c-a985c6eed7ce)

Here the description of test steps:

* we assign a dynamic template variable `TEST_EMAIL` by using template expression [counter()](https://docs.puppetry.app/template), 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](https://docs.puppetry.app/template)
* 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

![](https://2843146877-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LdyKJ-zzeS2hB7DF04J%2F-LhKU_LZzyxyKXdMSl-f%2F-LhKUbZcNfulFziTZuDj%2FP2-main-menu-snippets.png?alt=media\&token=5fb235dc-4f9c-4846-bf37-9fa32c26eb29)

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

We start by defining targets for the [login form](https://docs.puppetry.app/examples)&#x20;

![Snippet's targets](https://2843146877-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LdyKJ-zzeS2hB7DF04J%2F-LhteCGb4i-6eVd5g1K4%2F-LhteYkX9-Nb9nNCI6h9%2Fsnippet-targets2.png?alt=media\&token=6fd31410-6a25-40f0-8a74-07abfd13b16f)

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

![Snippet's body](https://2843146877-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LdyKJ-zzeS2hB7DF04J%2F-LhteCGb4i-6eVd5g1K4%2F-LhtebqfeI2B6isv0EOI%2Fsnippets-snippets.png?alt=media\&token=0f687dde-ffb1-478e-8eff-7fd0d0189030)

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

![](https://2843146877-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LdyKJ-zzeS2hB7DF04J%2F-LhKU_LZzyxyKXdMSl-f%2F-LhKVU37KbBdLz2SP7SG%2FP2-snippets-suie-file.png?alt=media\&token=70d460f4-b3ad-4ee9-aca7-3ed1b144228e)

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

![](https://2843146877-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LdyKJ-zzeS2hB7DF04J%2F-LhKU_LZzyxyKXdMSl-f%2F-LhKVVvC2SI2kU6Pa939%2Fp2-snippets-add-ref-btn.png?alt=media\&token=7b843504-708b-49df-9b92-cb4c080a6c31)

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

![Adding a reference](https://2843146877-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LdyKJ-zzeS2hB7DF04J%2F-LhGYA1WLhhQZqjRlXEU%2F-LhGZ-kUmnE3TXWm0nrD%2FP2-snippets-add-ref-modal.PNG?alt=media\&token=3305f83c-011a-4cee-ab52-855579fe8222)

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

![User gets logged in test case](https://2843146877-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LdyKJ-zzeS2hB7DF04J%2F-LhK542sUxJ3mUC1qCd4%2F-LhKAOnFenEsOFa_5FUa%2FP2-snippets-login-test-case.png?alt=media\&token=c708eeaa-7447-4b13-8299-816253fe0dba)

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).&#x20;

## 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:

![](https://2843146877-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LdyKJ-zzeS2hB7DF04J%2F-LhKU_LZzyxyKXdMSl-f%2F-LhKW2lYViomIr_L-PRF%2FP2-snippets-ref-edit-btn.png?alt=media\&token=722a053d-b928-453d-a7aa-1329029435ba)

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:

![Reference local variables ](https://2843146877-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LdyKJ-zzeS2hB7DF04J%2F-LhKFoeWup1WVV7VYwCL%2F-LhKIiuqBr6Nma4MhKdk%2FP2-snippets-edit-ref-modal.png?alt=media\&token=c3efb37d-8a31-428e-9eb9-acb38d1c2c31)

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.

