Using MailFlat with Robot Framework
Wrap the Python client in a small keyword library and your suites get New Inbox and Wait For Otp as ordinary Robot keywords.
How it fits together
Robot Framework talks to Python, and our Python client is two calls, so the library file is short. Once it exists, the suite reads like every other Robot suite.
The keyword library
One Python file, three keywords.
# MailFlatLibrary.py
import os, uuid
from mailflat import MailFlat
class MailFlatLibrary:
def __init__(self):
self._mf = MailFlat(api_key=os.environ["MAILFLAT_API_KEY"])
self._inbox = None
def new_inbox(self):
self._inbox = self._mf.create(
prefix=f"robot-{uuid.uuid4().hex[:8]}", retention_hours=2
)
return self._inbox.address
def wait_for_otp(self, timeout=60):
return self._inbox.wait_for_otp(timeout=int(timeout))
def delete_inbox(self):
if self._inbox:
self._inbox.delete()
self._inbox = None
The suite
Robot converts method names to keywords: new_inbox becomes New Inbox.
*** Settings ***
Library SeleniumLibrary
Library MailFlatLibrary
Test Teardown Delete Inbox
*** Test Cases ***
User Can Sign Up With A Real Code
${address}= New Inbox
Go To https://staging.example.com/signup
Input Text id:email ${address}
Click Button id:submit
${otp}= Wait For Otp 60
Input Text id:code ${otp}
Location Should Contain /dashboard
Worth knowing
Library scope defaults to per-suite. Use ROBOT_LIBRARY_SCOPE = 'TEST' if you want a fresh inbox per test case.
See also
Python
The official Python client wraps the REST API in two calls: open an inbox, wait for the code. Works in pytest, unittest, Robot Framework or a plain script.
One-time codesMailFlat pulls the verification code out of the message for you and hands it over as a field, so your test never runs a regex against an email body.