DEV Community

Cover image for How to test emails with Selenium
Liviu Lupei
Liviu Lupei

Posted on

How to test emails with Selenium

I work as a Software Engineer at Endtest.

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.

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

Let's see an example.

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

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()
Enter fullscreen mode Exit fullscreen mode

And that code would lead us to this screen:

Test Email Selenium

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

And we'll be able to open our email:

Endtest Mailbox

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

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

driver.get("https://endtest.io/mailbox?email=jim32328@endtest-mail.io")
email = driver.find_element_by_class_name("email_item")
email.click()
Enter fullscreen mode Exit fullscreen mode

More details are available in the How to test Emails chapter from our Documentation.

Top comments (8)

Collapse
 
dinsmoredesign profile image
Derek D • Edited

How does your service handle rendering emails? The hardest part about designing emails (IMO), is making sure they actually look the same between services (rendering is often wildly different between Outlook, Outlook web, Gmail, Yahoo, Zoho, etc, not to mention, mobile). Unless you're sending huge emails out with lots of content (which seems like a bad idea) or just need to test that the email was actually sent, I don't know how you'd write tests against these that didn't require still sending it to multiple other services?

Collapse
 
liviufromendtest profile image
Liviu Lupei

Hi Derek, our Endtest Mailbox service renders the HTML of the emails in a web page. You're able to access that web page with any browser or mobile device that you want.
You can also click on any element from that email, either manually or in an automated test.
This helps users ensure that their emails are sent and that certain flows are working (such as clicking the link in an email to confirm an account).
Opening that email with different email clients is not a use case that we currently support.

Collapse
 
elgatov profile image
elgatov

Seems like a great application. Currently we are using a free disposable email service but it seems the email are taking a long time to reach the inbox. How fast is your service in delivering emails?

Thank you!

Collapse
 
liviufromendtest profile image
Liviu Lupei

Our service does not deliver emails, our service receives emails and gives you access to them.

Collapse
 
felipegadelha profile image
Felipe Gadelha

Good morning, I would like to know if you have any implementation to receive via mobile sms?

Collapse
 
liviufromendtest profile image
Liviu Lupei

Hi Felipe, currently we do not have such a functionality.
It's in our backlog and you will see it in production in the near future.

Collapse
 
felipegadelha profile image
Felipe Gadelha

Thank you very much, this will help a lot

Thread Thread
 
liviufromendtest profile image
Liviu Lupei

Hi Felipe,
Forgot about this comment, we released the functionality to receive and read SMS messages 2 weeks ago.
Details here