Robot Framework-Establish Customize Library Through Implement Send Email Function

When using Robot Framework, yes, it’s fast and easy to use, but for some special scenario, Robot Framework didn’t provide relevant library and keyword, such as send email.
We can implement this through keyword Evaluate, but it’s maybe not appropriate way. so it’s better for us to encapsulate system level library and keyword.

Implementation

Here we implement this by sending mail:

  1. In python Lib\site-packages folder(e.g.,C:\Python27\Lib\site-packages ), create folder ‘GmailLibrary’.
  2. Design send mail library ‘sendmail.py’

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    import smtplib
    from email.MIMEMultipart import MIMEMultipart
    from email.MIMEBase import MIMEBase
    from email.MIMEText import MIMEText
    from email import Encoders
    import os

    class SendEmailUtility(object):

    ROBOT_LIBRARY_SCOPE = 'Global'

    def __init__(self):
    print 'send email utility'

    def send_mail_no_attachment(self,from_user,from_password,to, subject, text):
    msg = MIMEMultipart()

    msg['From'] = from_user
    msg['To'] = to
    msg['Subject'] = subject

    msg.attach(MIMEText(text))

    mailServer = smtplib.SMTP('outlook.office365.com')
    mailServer.ehlo()
    mailServer.starttls()
    mailServer.ehlo()
    mailServer.login(from_user, from_password)
    mailServer.sendmail(from_user, to, msg.as_string())
    mailServer.close()
  3. Design ‘init.py’

    1
    2
    3
    4
    5
    from sendemail import SendEmailUtility
    __version__ = '1.0'

    class EmailLibrary(SendEmailUtility):
    ROBOT_LIBRARY_SCOPE = 'GLOBAL'
  4. Import the library ‘MailLibrary’ in Robot Framework

  5. Check keyword in ‘Keyword List’
  6. Create case to send mail
  7. Mail send result

Postscript

We can implement everything from this way, but the important thing is you need to know python, even a little:)

Use the tool, but not subject to the tool.

唐胡璐 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
分享创造价值,您的支持将鼓励我继续前行!