转载自: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:\Python27\Lib\site-packages (assuming that you have installed python at the root of C: drive)

C:\Python27\Lib\site-packages\GmailEmailLibrary

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

Send Email in Robot Framework Python Using Gmail的更多相关文章

  1. 关于《自动化测试实战宝典:Robot Framework + Python从小工到专家》

    受新冠疫情影响,笔者被“困”在湖北老家七十余天,于4月1号(愚人节)这天,终于返回到广州.当前国内疫情基本已趋于平稳,但全球疫情整体势态仍在持续疯涨,累计确诊病例已近80万人.祈祷这场全球性灾难能尽早 ...

  2. Robot Framework+python的安装,配置,环境搭建(纯白篇)

    弄了大半天 终于把-Robot Framework-弄好了,总是一个发现问题,一个一个去解决的过程,只是时间嘛,咳咳咳咳 言归正传 第一. 记住了 Robot Framework 的库 只支持 pyt ...

  3. 【转】robot framework + python实现http接口自动化测试框架

    前言 下周即将展开一个http接口测试的需求,刚刚完成的java类接口测试工作中,由于之前犯懒,没有提前搭建好自动化回归测试框架,以至于后期rd每修改一个bug,经常导致之前没有问题的case又产生了 ...

  4. robot framework + python实现http接口自动化测试框架

    https://www.jianshu.com/p/6d1e8cb90e7d 前言 下周即将展开一个http接口测试的需求,刚刚完成的java类接口测试工作中,由于之前犯懒,没有提前搭建好自动化回归测 ...

  5. Robot framework+python安装使用图解版

    一.安装包 1.Python2.7(一切的基础,切记安装目录不能有中文不能有空格) 1)python2.7:(python环境):python-2.7.msi 2)setuptools(python包 ...

  6. 用Robot Framework+python来测试基于socket通讯的C/S系统(网络游戏)

    项目终于换了方案,改用socket来实现而不是之前的http了,所以测试工具就不能用以前的了,因为测试人手少,逼不得已的必须要挖掘更多的自动化方案来弥补.于是先研究了下python的socket解决方 ...

  7. Robot Framework环境搭建(问题总结)

    Robot Framework+python+wxpython+robotframework-ride+library环境搭建问题总结 因为robotframework的兼容性问题要求很严格,小编在环 ...

  8. Robot Framework通过Python SMTP进行email收发测试。

    工作中需要对发送的邮件进行过滤,方法基本属于ACL控制,即查看“源/目的”邮件地址,邮件标题,邮件正文,邮件附件等进行过滤. 所以需要先模拟一下用Python能否达到邮件Client,Server的功 ...

  9. robot framework用python扩展编写自定义library

    我的utils.py文件 #!/usr/bin/env python #-*- coding:utf8 -*- __version__ = '0.1' import sys reload(sys) s ...

随机推荐

  1. springBoot定制内嵌的Tomcat

    此篇仅介绍配置方式,详细配置内容百度查阅 工程结构: 可以通过配置的方式设置参数,如下 application.properties #配置tomcat端口 # server.port= 8888 # ...

  2. 用select模拟一个socket server成型版

    1.你往output里面放什么,下次循环就出什么.  2. 1.服务器端:实现了收和发的分开进行 import select,socket,queue server=socket.socket() s ...

  3. 【题解】洛谷P4735最大异或和

    学习了一下可持久化trie的有关姿势~其实还挺好理解的,代码也短小精悍.重点在于查询某个历史版本的trie树上的某条边是否存在,同样我们转化到维护前缀和来实现.同可持久化线段树一样,我们为了节省空间继 ...

  4. [洛谷P3950]部落冲突

    题目大意:给你一棵树,有$3$个操作: $Q\;p\;q:$询问$p,q$是否连通 $C\;p\;q:$把$p->q$这条边割断 $U\;x:$恢复第$x$次操作二 题解:可以在割断时把这条边赋 ...

  5. 【Ubuntu】编写一个c语言代码

    安装 sudo apt-get  build-depgcc coding:http://www.cnblogs.com/zero1665/archive/2009/11/03/1595510.html ...

  6. ssm框架pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  7. Android 字母导航条实现

    在Activity中进行功能的实现,需要用到第三方jar包:pinyin4j.jar,此jar包用于将汉字转换为汉语拼音. 首先,设置右侧边栏索引列表(A-Z),并且设置列表点击,Touch事件,点击 ...

  8. BZOJ2653:middle——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=2653 Description 一个长度为n的序列a,设其排过序之后为b,其中位数定义为b[n/2], ...

  9. 洛谷 P3521 [POI2011]ROT-Tree Rotations 解题报告

    P3521 [POI2011]ROT-Tree Rotations 题意:递归给出给一棵\(n(1≤n≤200000)\)个叶子的二叉树,可以交换每个点的左右子树,要求前序遍历叶子的逆序对最少. 大体 ...

  10. 数据库sharding,横向扩展

    学习资料如下: http://www.cnblogs.com/skyme/p/3459765.html http://my.oschina.net/anthonyyau/blog/307165 htt ...