MailSlurp is a free Python SDK published on PYPI that lets you create real email addresses on demand. You can use it with Robot Framework to test user sign-up and email verification in real applications. View the example code on GitHub.
Introduction to Robotframework
Robot is a hugely popular integration testing framework with support for Python and Java. You can use Robot to test apps, software, websites and more. This tutorial will show you how to test a website sign-up process using real email addresses with MailSlurp and Robot.
What is Robot?
Robot is a little different to most other Python test frameworks like PyTest: it is keyword-driven or acceptance-test driven. That means you write tests using keywords instead of Python code. It is a bit like Cucumber from Ruby. Test files are written in
files using a custom syntax. We recommend finding a plugin for your editor that supports the robot file type to make testing easier.
What do tests look like?
Tests in RobotFramework are keyword driven and resemble sentences.
An example might look like so:
NOTE: in Robot whitespace matters! Separate keywords and arguments with a tab or several whitespaces so that Robot can tell where keywords begin and end.
Every keyword or sentence we write inside a robot file needs to be defined by a plugin or by ourselves using a custom plugin. We will do this to integrate MailSlurp.
For this tutorial we will test the sign-up process of a real application hosted at playground.mailslurp.com. This is a demonstration React App that uses AWS Firebase for authentication. After entering an email address and password during sign-up the user receives a confirmation code.
The confirmation code must be extracted from the email and entered into the confirmation form to proceed. Once confirmed a user can log in and see a welcome page with a picture of a friendly dog.
First, let's setup a project using Python3.
MailSlurp and Robot can be found on PyPI. Create a
file and include the following dependencies:
Notice we have included robotframework, the robot selenium plugin, MailSlurp and the webdriver manager. Selenium is used for browser testing to open the MailSlurp playground and fill out the login forms. The webdrivermanager is used to download a webdriver to automate browser testing.
Install the dependencies like so:
Selenium needs a webdriver for firefox (or your browser of choice) to automate it. Let's use webdrivermanager to download
Then move the driver to the user
To setup a Robot test we need to create a few files.
Create a MailSlurp plugin file
To create email addresses during testing we need to include a plugin file called
in the root directory.
Inside the file include the following (we will explain it later):
This is how you define a plugin in Robot so that you can create custom test keywords. Now we need to include it.
Create a resource file and include MailSlurp
For Robot tests we need to define our keywords. Typically that involves a
Add one that contains the following code:
Above we defined library settings for Selenium and our custom functions. We also defined variables for use throughout our tests. Take note of the server url: https://playground.mailslurp.com hosts the functioning React web app that allows sign-up and user confirmation via email verification codes.
Include your API KEY
section we included two libraries: the Selenium Plugin and our custom MailSlurp file.
file we created and passed the
variable to the class constructor:
MailSlurp is free to use but you need to sign-up to create an API KEY. When running your robot tests set the variable on the command line with the
Defining test methods
In Robot you define keywords and then use them to describe acceptance tests. Use the
file to define the actions you want to test. For our demonstration app we can define these actions below:
Let's break that down a bit.
Querying pages with Selenium
The selenium plugin adds many keywords to our test suite. We can call them from the resource file to query the playground webapp. We can use css style selectors (xpath) in selenium like so:
This would find and click an HTML element that matches:
The MailSlurp playground app uses
attributes to allow for easy acceptance testing that doesn't rely on constant class or ID DOM attributes.
Calling custom Python functions
You may be thinking "where is the actual code in all this?". Let's get to that. You can call custom functions by including the library like we did in the
file. Note: the file and class name must be the same. Robot translates method names to keywords by replacing
underscores with spaces. So the
function defining in
The MailSlurp create inbox function creates a real email address and returns an inbox entity with an ID and an email address.
The code inside
that makes this function and its return object available in tests is this:
This is the custom robot syntax and it assigns the result of the
function to a local variable before returning it. Inside a test we can call it like this:
Now that we can call MailSlurp and Selenium keywords let us write a test for the full sign up process.
Writing Robot tests
Create a file called
. Add the settings header to include our
file so we can access all the keywords.
Our application flow
Next we need to test the user sign up and verification process. To recap, the process we wish to test is:
- create a unique email address for the test
- sign up for the playground app using the email address
- receive and extract a confirmation code via email
- submit the verification code and confirm user
- sign in to the app and view the happy dog
Based on the custom functions in
and the keywords defined in
we can write a test for the above functionality with the following statements:
Two important functions in this test are:
function is a unique MailSlurp feature that lets you wait for incoming emails and return the content. We then combine that with a regular expression pattern match to extract the confirmation code from an email of the pattern
. In this case the
regular expression is a capture group for the 6 digits that are used as the confirmation code. The
returns this value for use in the next steps of the test.
Testing authentication end-to-end
Running the full test suite with
will create a new email address ending in
, sign up for the playground app, receive the email verification code, confirm the account and log into the app. For full usage see the Python documentation or the example code on GitHub.
Robot Framework and MailSlurp enabled acceptance testing of complex applications by generating real test email accounts. You can test user authentication and sign-up flows, lost password or email verification. See the MailSlurp docs for more information and good luck.