How to test email and SMS with Robot Framework

Learn how to test email and SMS messages in Robot Framework, using Mailosaur.

Before you begin

If you don’t already have one, create a Mailosaur account.

Make sure you know how to send email to Mailosaur first. Once you have this working, you’re ready to start testing!

How it works

Using Mailosaur’s official Python client library you can create a helper library for your Robot Framework tests.

Step 1 - Get started with Python

Before doing anything with Robot Framework, follow the steps shown in the Python guide to ensure you can connect to Mailosaur and retreive messages.

Step 2 - Create your Robot Framework helper library

Depending on what you want to test, you can create your own helper methods.

  1. Create a file called mailosaur_helper.py.
  2. Add the following code to that file
  3. In your Robot Framework test, do this…

Step 1: Create a helper class

First, we need to create a helper class, which we’ll name Mailosaur.py. Having this file allows us to write helper methods, which exposes the functionality of the official Mailosaur client library as keywords within Robot Framework tests:

from mailosaur import MailosaurClient
from mailosaur.models import SearchCriteria

class Mailosaur(object):
    def __init__(self, api_key):
        self.client = MailosaurClient(api_key)

    def generate_email_address(self, server_id):
        return self.client.servers.generate_email_address(server_id)

    def get_email(self, server_id, email_address):
        criteria = SearchCriteria()
        criteria.sent_to = email_address
        return self.client.messages.get(server_id, criteria)

    def subject_should_equal(self, message, expected):
        assert(message.subject == expected)

You can of course continue to write as many helper methods as you need for your test suite.

Step 2: Use Mailosaur in your Robot Framework tests

Now we can use the helper methods we created above in our Robot Framework tests. Simply include Mailosaur.py as a Library, being sure to pass in your API key:

*** Settings ***
Library           SeleniumLibrary
Library           Mailosaur.py        <<API_KEY>>

*** Variables ***
${SERVER_ID}					<<SERVER_ID>>
${SIGNUP_URL}					https://yoursite.com/signup/

*** Test Cases ***
Test Sign Up Email
    Sign Up New User

*** Keywords ***
Sign Up New User
    ${RANDOM_EMAIL}=     Mailosaur.generate_email_address  ${SERVER ID}
    Open Browser  ${SIGNUP URL}
    Input Text    name:name    Jane Doe
    Input Text    name:email    ${RANDOM_EMAIL}
    Input Text    name:password  Password123!
    Click Button    css:.btn-primary
    ${MESSAGE}=     Mailosaur.fetch_email   ${SERVER ID}    ${RANDOM_EMAIL}
    Mailosaur.subject_should_equal    ${MESSAGE}      Thanks for joining!
    [Teardown]    Close Browser

Common test cases

Now that you have a working project that fetches an email, you can use this to begin writing tests for various pieces of functionality.

Check out these guides on the most common test cases: