# How to test email and SMS with Robot Framework Learn how to test email and SMS messages in Robot Framework, using Mailosaur. ## What is Robot Framework? Robot Framework is a Python-based open source automation framework. It can be used for any kind of QA test automation. Robot Framework is in active use by many industry-leading companies as part of their software development lifecycle. As you are reading an article about testing email and SMS in Robot Framework, you probably already know how to use Robot Framework, however, if you are new to it, we would recommend reading the [Robot Framework website and documentation](https://robotframework.org) to get started. ## What is Mailosaur? Mailosaur is a service that allows you to capture and test email and SMS messages. This is crucial for products and websites that send out emails (such as password resets, account setup, marketing emails, etc.) or SMS messages (such as multi-factor authentication, verification, etc.). With Mailosaur, you get an unlimited supply of test email addresses, plus virtual SMTP servers, test SMS numbers to work with, and more - all accessible within your Robot Framework test suite. ## 1. Install the Mailosaur Python library As Robot Framework is Python-based, you can use our [official Python library](https://github.com/mailosaur/mailosaur-python) to quickly start testing email and SMS messages. ### Installation Install the Mailosaur Python library with pip: ```bash pip install --upgrade mailosaur ``` Create a Python file to contain the code you want to expose to Robot Framework, then import the library into it. The value for `YOUR_API_KEY` is covered in the next step ([creating an account](#2-2-create-an-account)): ```python from mailosaur import MailosaurClient mailosaur = MailosaurClient("YOUR_API_KEY") ``` ### API Reference This library is powered by the [Mailosaur API](/docs/api). You can easily check out the API itself by looking at our [API reference documentation](/docs/api) or via our Postman or Insomnia collections: ![Postman](https://app.getpostman.com/run-collection/6961255-6cc72dff-f576-451a-9023-b82dec84f95d?action=collection%2Ffork&collection-url=entityId%3D6961255-6cc72dff-f576-451a-9023-b82dec84f95d%26entityType%3Dcollection%26workspaceId%3D386a4af1-4293-4197-8f40-0eb49f831325) ![Insomnia](https://insomnia.rest/run/?label=Mailosaur&uri=https%3A%2F%2Fmailosaur.com%2Finsomnia.json) ## 2. Create an account Create a [free trial account](/app/signup) for Mailosaur, via the website. Once you have this, open your inbox from the **Inboxes** section of the left-hand navigation, then click the **Settings** tab. You need the following values: - **Inbox ID** - Every [inbox](/docs/inboxes) has a unique ID. You need this ID whenever you interact with an inbox via the API. - **Inbox Domain** - Every inbox has its own domain name. You'll need this to send email to your inbox. - **API Key** - Get one from the [API Keys](/app/keys) page. You can create an API key per inbox (recommended), or an account-level API key to use across your whole account. [Learn more about API keys](/docs/managing-your-account/api-keys). ## 3. Test email addresses with Mailosaur Mailosaur gives you an **unlimited number of test email addresses** - with no setup or code required! Here’s how it works: - When you create an account, you are given an inbox. - Every inbox has its own **Inbox Domain** name (e.g. `abc123.mailosaur.net`) - Any email address that ends with `@{YOUR_SERVER_DOMAIN}` will work with Mailosaur without any special setup. For example: - `build-423@abc123.mailosaur.net` - `john.smith@abc123.mailosaur.net` - `rAnDoM63423@abc123.mailosaur.net` - You can create more inboxes when you need them. Each one will have its own domain name. _**Can’t use test email addresses?** You can also [use SMTP to test email](/docs/email-testing/sending-to-mailosaur#0-sending-directly-via-smtp). By connecting your product or website to Mailosaur via SMTP, Mailosaur will catch all email your application sends, regardless of the email address._ ## 4. Find an email ### Create a helper class First, we need to create a helper class, which we’ll name `Mailosaur.py`. This file will let you write helper methods that expose the functionality of the Mailosaur Python library as keywords within your [Robot Framework](https://robotframework.org) tests: ```python from mailosaur import MailosaurClient from mailosaur.models import SearchCriteria class Mailosaur(object): def __init__(self): self.client = MailosaurClient("YOUR_API_KEY") def find_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) ``` ### Use the helper class in a Robot Framework test spec Now we can use the helper methods we created above, in our Robot Framework tests. Simply include `Mailosaur.py` as a Library: ```text *** Settings *** Library SeleniumLibrary Library Mailosaur.py *** Variables *** ${URL} https://example.mailosaur.com/password-reset ${SERVER_ID} abc123 # From your inbox's Settings tab ${SERVER_DOMAIN} abc123.mailosaur.net # From your inbox's Settings tab *** Test Cases *** Test Password Reset Request Password Reset *** Keywords *** Request Password Reset ${TEST_EMAIL_ADDRESS}= some-random-string@${SERVER_DOMAIN} Open Browser ${URL} Input Text name:email ${TEST_EMAIL_ADDRESS} Click Button css:button[type="submit"] ${MESSAGE}= Mailosaur.find_email ${SERVER_ID} ${TEST_EMAIL_ADDRESS} Mailosaur.subject_should_equal ${MESSAGE} Thanks for joining! [Teardown] Close Browser ``` ## Find an SMS message **Important:** Trial accounts do not automatically have SMS access. Please contact our support team to enable a trial of SMS functionality. If your account has [SMS testing](/docs/sms-testing) enabled, you can reserve phone numbers to test with, then use the Mailosaur API in a very similar way to when testing email: ```python # ... criteria = SearchCriteria() criteria.sent_to = "4471235554444" return self.client.messages.get(server_id, criteria) ``` ## Testing plain text content Most emails, and all SMS messages, should have a plain text body. Mailosaur exposes this content via the `text.body` property on an email or SMS message: ```python def message_text_contains(self, message, expected): assert(expected in message.text.body) ``` ### Extracting verification codes from plain text You may have an email or SMS message that contains an account verification code, or some other one-time passcode. You can extract content like this using a simple regex. Here is how to extract a 6-digit numeric code: ```python print(message.text.body) # "Your access code is 243546." match = re.search("([0-9]{6})", message.text.body) print(match.group()) # "243546" ``` [Read more](/docs/automation/text-content) ## Testing HTML content Most emails also have an HTML body, as well as the plain text content. You can access HTML content in a very similar way to plain text: ```python print(message.html.body) # "`) and area (``) elements and makes these available via the `html.links` array. Each link has a text property, representing the display text of the hyperlink within the body, and an href property containing the target URL: ```python def count_html_links(self, message, expected): # Passes if message has the expected number of links assert(expected in len(message.html.links)) # Accessing the HTML link content first_link = message.html.links[0] print(first_link.text) # "Google Search" print(first_link.href) # "https://www.google.com/" ``` **Important:** To ensure you always have valid emails, Mailosaur only extracts links that have been correctly marked up with `` or `` tags. ### Links in plain text (including SMS messages) Mailosaur auto-detects links in plain text content too, which is especially useful for SMS testing: ```python def count_text_links(self, message, expected): # Passes if message has the expected number of links assert(expected in len(message.text.links)) # Accessing the HTML link content first_link = message.text.links[0] print(first_link.href) # "https://www.google.com/" ``` ## Working with attachments If your email includes attachments, you can access these via the attachments property: ```python # How many attachments? print(len(message.attachments)) # 2 ``` Each attachment contains metadata on the file name and content type: ```python first_attachment = message.attachments[0] print(first_attachment.file_name) # "contract.pdf" print(first_attachment.content_type) # "application/pdf" ``` The `length` property returns the size of the attached file (in bytes): ```python first_attachment = message.attachments[0] print(first_attachment.length) # 4028 ``` ## Working with images and web beacons The `html.images` property of a message contains an array of images found within the HTML content of an email. The length of this array corresponds to the number of images found within an email: ```python # How many images in the email? print(len(message.html.images)) # 1 ``` ### Remotely-hosted images Emails will often contain many images that are hosted elsewhere, such as on your website or product. It is recommended to check that these images are accessible by your recipients. All images should have an alternative text description, which can be checked using the `alt` attribute. ```python image = message.html.images[0] print(image.alt) # "Hot air balloon" ``` ### Triggering web beacons A web beacon is a small image that can be used to track whether an email has been opened by a recipient. Because a web beacon is simply another form of remotely-hosted image, you can use the `src` attribute to navigate to that address with Robot Framework. Alternatively you can use the `requests` library to make a direct HTTP request: ```python import requests # ... const image = message.html.images[0] print(image.src) # "https://example.com/s.png?abc123" # Make an HTTP call to trigger the web beacon response = requests.get(image.src) print(response.status_code) # 200 ``` ## Spam checking You can perform a [SpamAssassin](https://spamassassin.apache.org) check against an email. The structure returned matches the [spam test object](/docs/api#0-perform-a-spam-test): ```python result = mailosaur.analysis.spam(message.id) print(result.score) # 0.5 for r in result.spam_filter_results.spam_assassin: print(r.rule) print(r.description) print(r.score) ```