mailslurp-examples - python3-robotframework

https://github.com/mailslurp/examples

Table of Contents

python3-robotframework/test.robot

*** Settings ***
Documentation     Test user sign-up with MailSlurp and Robot
Resource          resource.robot

*** Test Cases ***
Sign Up With New Email Address
    ${inbox}    Create Email Address
    Open Browser To Home Page
    Go To SignUp Page
    Input Email       ${inbox.email_address}
    Input Password    ${TEST_PASSWORD}
    Submit Credentials
    Confirm Page Should Be Open
    ${code}    Wait For Confirmation Code   ${inbox.id}
    Log To Console      ${code} extacted
    Input Confirmation      ${code}
    Submit Confirmation
    SignIn Page Should Be Open
    Input Username    ${inbox.email_address}
    Input Password    ${TEST_PASSWORD}
    Submit Login
    User Page Should Be Open
    [Teardown]    Close Browser

python3-robotframework/resource.robot

# set up the tests
*** Settings ***
Documentation     Define keywords and variables and import MailSlurp functions
Library           SeleniumLibrary
Library           ./MailSlurp.py    ${MAILSLURP_API_KEY}

# define variables and default values
*** Variables ***
${MAILSLURP_API_KEY}    PUT_YOUR_KEY_HERE
${SERVER}               playground.mailslurp.com
${BROWSER}              Firefox
${DELAY}                0
${PLAYGROUND URL}       https://${SERVER}/
${TEST_PASSWORD}        test-password

# see https://robotframework.org/SeleniumLibrary/SeleniumLibrary.html for selenium keywords
*** Keywords ***
Create Email Address
    ${inbox}    Create Inbox
    [Return]    ${inbox}

Wait For Confirmation Code
    [Arguments]     ${inbox_id}
    ${email}    Wait For Latest Email   ${inbox_id}
    ${code}     Extract Email Content   ${email.body}
    [Return]    ${code}

Open Browser To Home Page
    Open Browser    ${PLAYGROUND URL}    ${BROWSER}
    Maximize Browser Window
    Set Selenium Speed    ${DELAY}
    Home Page Should Be Open

Home Page Should Be Open
    Title Should Be    React App

SignUp Page Should Be Open
    Wait Until Element Contains     //*[@data-test="sign-up-header-section"]//span   Sign Up

Go To SignUp Page
    Go To    ${PLAYGROUND URL}
    Home Page Should Be Open
    Click Element   //a[@data-test="sign-in-create-account-link"]
    SignUp Page Should Be Open

Input Email
    [Arguments]    ${username}
    Input Text     //*[@name="email"]    ${username}

Input Username
    [Arguments]    ${username}
    Input Text     //*[@name="username"]    ${username}

Input Password
    [Arguments]    ${password}
    Input Text    //*[@name="password"]    ${password}

Input Confirmation
    [Arguments]    ${code}
    Input Text    //*[@name="code"]    ${code}

Submit Confirmation
    Click Button    //button[@data-test="confirm-sign-up-confirm-button"]

Submit Credentials
    Click Button    //button[@data-test="sign-up-create-account-button"]

Submit Login
    Click Button    //button[@data-test="sign-in-sign-in-button"]

Confirm Page Should Be Open
    Wait Until Element Contains     //*[@data-test="confirm-sign-up-header-section"]//span   Confirm

SignIn Page Should Be Open
    Wait Until Element Contains     //*[@data-test="sign-in-header-section"]//span   Sign in to your account

User Page Should Be Open
    Wait Until Page Contains    Welcome

python3-robotframework/requirements.txt

robotframework >= 4.0.0
robotframework-seleniumlibrary >= 5.1.3
mailslurp-client >= 15.0.1
webdrivermanager >= 0.10.0

python3-robotframework/README.md

# Robot Framework Email Testing
Use MailSlurp and Selenium to test user sign-up using real email addresses.

python3-robotframework/Makefile

-include ../.env

install:
	pip3 install -r requirements.txt

# download geckodriver
browser:
	python3 -m webdrivermanager firefox -d . -l SKIP
	sudo mv gecko/v*/gecko*/geckodriver /usr/bin/geckodriver

test: install
	python3 -m robot --variable MAILSLURP_API_KEY:$(API_KEY) --outputdir results test.robot

report:
	firefox results/report.html

python3-robotframework/MailSlurp.py

import mailslurp_client
import re

# define a library to use mailslurp functions in robot test
class MailSlurp(object):
    ROBOT_LIBRARY_VERSION = '1.0.0'
    ROBOT_LIBRARY_SCOPE = 'GLOBAL'

    # configure the mailslurp client using your API KEY
    def __init__(self, api_key):
        self.configuration = mailslurp_client.Configuration()
        self.configuration.api_key['x-api-key'] = api_key

    # function for creating an email address returns an inbox with an id and email_address
    def create_inbox(self):
        with mailslurp_client.ApiClient(self.configuration) as api_client:
            # create an inbox using the inbox controller
            api_instance = mailslurp_client.InboxControllerApi(api_client)
            inbox = api_instance.create_inbox()
            return inbox

    def wait_for_latest_email(self, inbox_id):
        with mailslurp_client.ApiClient(self.configuration) as api_client:
            # create an inbox using the inbox controller
            api_instance = mailslurp_client.WaitForControllerApi(api_client)
            email = api_instance.wait_for_latest_email(inbox_id=inbox_id, timeout=60000, unread_only=True)
            return email

    def extract_email_content(self, email_body):
        regex = 'Your Demo verification code is ([0-9]{6})'
        pattern = re.compile(regex)
        matches = pattern.match(email_body)
        content = matches.group(1)
        return content