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.
Here we implement this by sending mail:
- In python Libsite-packages folder(e.g.,C:Python27Libsite-packages ), create folder ‘GmailLibrary’.
-
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
30import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email import Encoders
import os
class (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() -
Design ‘init.py’
1
2
3
4
5from sendemail import SendEmailUtility
__version__ = '1.0'
class EmailLibrary(SendEmailUtility):
ROBOT_LIBRARY_SCOPE = 'GLOBAL' -
Import the library ‘MailLibrary’ in Robot Framework
- Check keyword in ‘Keyword List’
- Create case to send mail
- 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.
近期评论