# Testing Transactional Emails

Transactional email is a sort of email that require an action from the receiver side like in signup confirmation emails, password resets, purchase notifications and others. In order to automate testing of user flows involving transactional emails we need to be able to request the inbox contents from the testing tool. That can be achieved by using an email server with REST API or a specialized service such as [Sendgrid](https://sendgrid.com), [Mailgun](https://www.mailgun.com), [Email Yak](http://www.emailyak.com/), [Postmark](http://www.postmarkapp.com). These services are normally quite expensive, so alternatively we can develop a bridge one, which uses IMAP or, let’s say, Google API to access a public email server. Just to give it a try we can even go with Restmail.net, which is free and requires no registration or set up.

Puppetry provides `page.assignVarRemotely` command, which polls a given email server REST API until the desired email received (ot timeout). It retrieves the parsed the parsed value (e.g. activation link) and assigns it to a dynamic template variable accessible in all following test steps. The command can be configured to parse the received content.

## Testing Signup Flow&#x20;

To get a better grip on the idea we are going to create a simplified test project for[ imaginary ACME forum](https://docs.puppetry.app/examples) built with [NodeBB](https://nodebb.org/). So the test scenario would be to type in required fields on the registration form, submit the form and use the  link sent by email to activate the account.

### Template Variables

Let's open Puppetry, create a new project and navigate to **Settings**. There we define our template variables:

![Signup flow variables](https://3461068122-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LdyKJ-zzeS2hB7DF04J%2F-LhtfEKT-u_TUBPmM2IX%2F-LhtfPKB-6ZBLvS8949r%2Freg-template-variables.png?alt=media\&token=8e6202c1-24bb-4688-b32b-dd054523e011)

What we need are `BASE_URL` (<http://localhost:4567/> NodeBB default one for development environment), which depends on testing environment and `TEST_PASSWORD` (fixture password used for registration) that once defined we can reuse across the project.&#x20;

### Test Targets

Now we can define the test targets for the flow:

![Signup flow targets](https://3461068122-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LdyKJ-zzeS2hB7DF04J%2F-LhFEDCZwQGqzvvEc16s%2F-LhFGUUw7VfzW1AYDcdT%2FP2-Reg-targets.png?alt=media\&token=9c72ef3a-4130-432b-aeec-16f7426c4530)

The registration form ([see screenshot](https://docs.puppetry.app/examples)) has email, username, password, password confirmation inputs and submit button. We introduce these elements into Puppetry as `REG_EMAIL_INPUT`, `REG_USERNAME_INPUT`, `REG_PASSWORD_INPUT`, `REG_CON_PASSWORD_INPUT`, `REG_SUBMIT_BTN`. After submitting the form the application brings us to the GDPR form ([see screenshot](https://docs.puppetry.app/examples)), where we need to tick on consent checkboxes and click on Register button.  Those elements we refer as `REG_AGREE_EMAIL_CHECKBOX`, `REG_AGREE_DATA_CHECKBOX`, `REG_REGISTER_BTN`.

### Test Case

![Signup test case, part 1](https://3461068122-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LdyKJ-zzeS2hB7DF04J%2F-LiILAgBfTj5ZPpPINrB%2F-LiILIXkJiqd6Dv66BbD%2Fsignup-test-case-1.png?alt=media\&token=6f20d276-74d5-4980-b79a-caeba554d66a)

First we need to ensure that we do no register an existing user. So we need to use a unique email address. That we achieve by dynamically  assigning a template variable `TEST_EMAIL` that will accept during test run iterating values like <test1@acme.com>, <test2@acme.com> and so on.

Next we use page.goto to navigate to the signup page.

We type in email address generated in `TEST_EMAIL`.

We type in username, dynamically built with faker expression.

We type in password and password confirmation by using `TEST_PASSWORD`  template variable, which we defined above.&#x20;

Finally we click on Submit button.

![Signup test case, part 2](https://3461068122-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LdyKJ-zzeS2hB7DF04J%2F-LiILAgBfTj5ZPpPINrB%2F-LiINM6rGWy49-Hlz_M8%2Fsignup-test-case-2.png?alt=media\&token=782234db-99de-4a12-9f11-4089571d8ae7)

That is supposed to bring us to the next page with the GDPR form.

So we wait until the page is ready

We make a screenshot.

We tick on the consent checkboxes.

We click on Register button.&#x20;

We wait until the sent email arrives to the inbox and assign the parsed activation link to template variable `ACTIVATION_LINK`.

The we follow the link and make a new screenshot.&#x20;

### Remote Service

Now we can proceed with any of the following examples

{% content-ref url="testing-emails/example-with-restmail.net" %}
[example-with-restmail.net](https://docs.puppetry.app/testing-techniques/testing-emails/example-with-restmail.net)
{% endcontent-ref %}

{% content-ref url="testing-emails/example-with-..." %}
[example-with-...](https://docs.puppetry.app/testing-techniques/testing-emails/example-with-...)
{% endcontent-ref %}

{% content-ref url="testing-emails/example-with-imap-bridge" %}
[example-with-imap-bridge](https://docs.puppetry.app/testing-techniques/testing-emails/example-with-imap-bridge)
{% endcontent-ref %}

{% content-ref url="testing-emails/example-with-google-api" %}
[example-with-google-api](https://docs.puppetry.app/testing-techniques/testing-emails/example-with-google-api)
{% endcontent-ref %}
