How to test emails with Selenium
Tags: testing, webdev, html, css
Published: 2020-07-10 15:29:51


I work as a Software Engineer at [Endtest](https://endtest.io).

It's easy to forget to include email testing in your test automation workflow. 

I'm going to show you how to test emails.


## Emails are important.

Most applications send automatic emails to users.

For example, you get an email when you sign up on a website and you usually need to click on the confirmation link from that email.

You might also get transactional emails when you perform certain actions on a service.

## Testing emails is important.

Like any responsible developer, you probably have some automated tests that verify if your web application is working as expected. 

You should also verify if the emails sent by your web application look good and work as expected.

Think of this nightmare scenario: 

Something breaks in your app and the email confirmation no longer works for users who want to sign up.

This is a preventable type of disaster.

## Emails are basically just HTML and CSS.

This makes it easy to test them with Selenium or any other testing tool. 

And not just visually. 

We can easily click on links and other elements from the email.

## The wrong way.

It would be difficult to rely on a single email address. 

Even if we would have steps to delete that email address from our application, it would still be inconvenient. 

And we cannot use our work email or our Gmail account. Since they tend to have 2FA, reCAPTCHAs and other tricky obstacles. 

## The right way.

We can use a disposable email service, as long it's reliable and secure. 

## The smart way.

We built a free email testing service called [Endtest Mailbox](https://endtest.io/mailbox).

Anyone can use it and you don't have to sign up. 

## But how does it work?

The way it works is similar to a disposable email service.

Simply send your email(s) to a username@endtest-mail.io email address.

The username can be anything you choose and you don't have to register it anywhere.

For instance, you can choose something like jim32328@endtest-mail.io.

You can access the Inbox for that email address on this page.

Just add the email parameter in the URL, like this:

[https://endtest.io/mailbox?email=jim32328@endtest-mail.io](https://endtest.io/mailbox?email=jim32328@endtest-mail.io)


## Let's see an example.

Let's write some Python + Selenium code that will perform a sign up on a website. 

```python
driver.get("https://swit.io/register")

firstName = driver.find_element_by_id("firstName")
lastName = driver.find_element_by_id("lastName")
email = driver.find_element_by_id("id")
password = driver.find_element_by_id("password")
confirmPassword = driver.find_element_by_id("confirmPassword")
submitButton = driver.find_element_by_class_name("button--important")

firstName.send_keys("Jim")
lastName.send_keys("Carrey")
email.send_keys("jim32328@endtest-mail.io")
password.send_keys("Password123")
confirmPassword.send_keys("Password123")
submitButton.click()
```

And that code would lead us to this screen:

![Test Email Selenium](https://dev-to-uploads.s3.amazonaws.com/i/34o1lqdh03dyokgnsho9.png)

Now, we can check that email by going to this URL:
[https://endtest.io/mailbox?email=jim32328@endtest-mail.io](https://endtest.io/mailbox?email=jim32328@endtest-mail.io)

And we'll be able to open our email:

![Endtest Mailbox](https://dev-to-uploads.s3.amazonaws.com/i/8yzzwws9yr35fuo5kstc.png)

And you can use any testing tool, browser or mobile device to access it.

Here is how you would access it with Python + Selenium:

```python
driver.get("https://endtest.io/mailbox?email=jim32328@endtest-mail.io")
email = driver.find_element_by_class_name("email_item")
email.click()
```

More details are available in the [How to test Emails](https://endtest.io/guides/docs/how-to-test-emails/) chapter from our [Documentation](https://endtest.io/guides/docs/how-to-create-web-tests/).