zoukankan      html  css  js  c++  java
  • Send Email in Robot Framework Python Using Gmail

    转载自:http://seleniummaster.com/sitecontent/index.php/selenium-robot-framework-menu/selenium-robot-framework-python-menu/228-send-email-in-robot-framework-python-using-gmail

    When using Robot Framework Python, some customized test result log files or report files need to be emailed to QA engineers. You can create custom Gmail Email Library to send email with attachment or no attachment.  

    Step 1:  create a folder named "GmailEmailLibrary" under C:Python27Libsite-packages (assuming that you have installed python at the root of C: drive)

    C:Python27Libsite-packagesGmailEmailLibrary

    Step 2: write following codes in the file "gmailsendemail.py" and "__init__.py"

    gmailsendemail.py

    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_with_attachment(self,from_user,from_password,to, subject, text, attach):
           msg = MIMEMultipart()
        
           msg['From'] = from_user
           msg['To'] = to
           msg['Subject'] = subject
    
           msg.attach(MIMEText(text))
    
           part = MIMEBase('application', 'octet-stream')
           part.set_payload(open(attach, 'rb').read())
           Encoders.encode_base64(part)
           part.add_header('Content-Disposition',
                   'attachment; filename="%s"' % os.path.basename(attach))
           msg.attach(part)
    
           mailServer = smtplib.SMTP("smtp.gmail.com", 587)
           mailServer.ehlo()
           mailServer.starttls()
           mailServer.ehlo()
           mailServer.login(from_user, from_password)
           mailServer.sendmail(from_user, to, msg.as_string())
           # Should be mailServer.quit(), but that crashes...
           mailServer.close()
    
        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("smtp.gmail.com", 587)
               mailServer.ehlo()
               mailServer.starttls()
               mailServer.ehlo()
               mailServer.login(from_user, from_password)
               mailServer.sendmail(from_user, to, msg.as_string())
               # Should be mailServer.quit(), but that crashes...
               mailServer.close()

    __init__.py

    from gmailsendemail import SendEmailUtility
    __version__ = '1.0'
    
    class GmailEmailLibrary(SendEmailUtility):
    
           
        ROBOT_LIBRARY_SCOPE = 'GLOBAL'

    Step 3: create a test project using RIDE in robot framework as shown below. Make sure that you import the library "GmailEmailLibrary". 

    Step 4: create the test cases "Send Email Has Attachment Test" and "Send Email No Attachment Test".  See the text version below. 

    *** Settings ***
    Library           GmailEmailLibrary
    
    *** Test Cases ***
    Send Email Has Attachment Test
        Send Mail With Attachment    
     services@seleniummaster.com    **********    
     test@test.com    Python Email Test    This is python test    test.txt
    
    Send Email No Attachment Test
        Send Mail No Attachment    
     services@seleniummaster.com    **********    
     test@test.com    This is a test, this is a test    Test is in progress

    Run the tests. The two test cases will pass

  • 相关阅读:
    异常详细信息: System.InvalidCastException: 对象不能从 DBNull 转换为其他类型——的解决方法
    .net显示今天农历的代码!
    在GridView中设置日期格式
    安装VS 2008 SP1后,智能提示变为英文的补丁发布
    SQL server无法执行查询,因为一些文件丢失或未注册等问题的解决
    SQL Server 2000企业管理器中MMC无法创建管理单元的解决方法
    FILTER:progid:DXImageTransform.Microsoft.Gradient使用
    [jQuery] event.stopPropagation()报错
    得到系统当前的dpi设置值
    VS2008 当前不会命中断点,还没有为该文档加载任何符号
  • 原文地址:https://www.cnblogs.com/zwingblog/p/6322187.html
Copyright © 2011-2022 走看看