DEV Community

Cover image for Testing Emails with Selenium
Klaus for Endtest

Posted on

Testing Emails with Selenium

Selenium is widely used, but it's a little known fact that you can use it to test anything built with HTML and CSS, including Chrome Extensions and Emails.

We'll create a simple test for the Register page from Swit and then we'll test the email that we receive.

I'll also be making some comparisons between Selenium and Endtest:

Selenium can only interact with HTML elements if they're in a browser.

This means that we'll have to open our email in a browser.

Luckily, anyone can use the Endtest Mailbox for free.

The way it works is similar to a disposable email service.
Simply send your email(s) to a username@endtest.io email address.

The username can be anything you choose.
For instance, you can choose something like jim32328@endtest.io.

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

Just add the email parameter in the URL, like this:
https://endtest.io/mailbox?email=jim32328@endtest.io

Please note that the emails will be deleted after 2 hours.

But for now, let's get back the Register page from Swit:

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("Klaus")
lastName.send_keys("Werner")
email.send_keys("klaus123@endtest.io")
password.send_keys("Password123")
confirmPassword.send_keys("Password123")
submitButton.click()

And here is the result:

Now, if we want to check that email, we have to go to:
https://endtest.io/mailbox?email=klaus123@endtest.io

And we'll be able to open our email:

We just have to write that part with Selenium:

driver.get("https://endtest.io/mailbox?email=klaus123@endtest.io")
email = driver.find_element_by_class_name("email_item")
email.click()

Since you're planning to run that test multiple times, you should generate a random email and store it in a variable and then append it to the Endtest Mailbox URL.

If you're looking for a better Selenium alternative, switch to Endtest.

Top comments (0)