转载自: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. 20162328蔡文琛week02

    学号 20162328 <程序设计与数据结构>第2周学习总结 教材学习内容总结 这周学习了课本中的第二章内容,比起第一章,本章难度有略微底稿,从刚开始的显示字符转变为简单的加减乘除运算,经 ...

  2. 算法与数据结构实验题 6.3 search

    ★实验任务 可怜的 Bibi 刚刚回到家,就发现自己的手机丢了,现在他决定回头去搜索 自己的手机. 现在我们假设 Bibi 的家位于一棵二叉树的根部.在 Bibi 的心中,每个节点 都有一个权值 x, ...

  3. iOS AVAudioPlayer播放音频时声音太小

    iOS AVAudioPlayer播放音频时声音太小 //引入AVFoundation类库,设置播放模式就可以了 do { try AVAudioSession.sharedInstance().ov ...

  4. thrift多平台安装

    thrift支持多语言的RPC,一直都想深入学习了解thrift,最近有空,就上网查了些资料,学习了一下,对它的使用有了一些了解.本篇是写thrift的安装,使用方法会另起一篇来写. 本文使用thri ...

  5. Web界面和Winform界面生成,代码生成工具

    在上面一篇随笔<代码生成工具之界面快速生成>介绍了代码生成工具Database2Sharp的界面生成操作,其中介绍了Web界面(包括列表界面.内容显示.内容编辑界面的生成,另外还介绍了Wi ...

  6. vi/sed等遵循的搜索正则语法

    转自:http://blog.csdn.net/lanxinju/article/details/5731843 一.查找 查找命令 /pattern<Enter> :向下查找patter ...

  7. VBA练习-复杂一点

    '日期添加 Sub addDate(d) Dim rg As Range, dd As Date d = Split(d, ) d = Replace(d, ".", " ...

  8. 2018 杭电多校1 - Distinct Values

    题目链接 Problem Description Chiaki has an array of n positive integers. You are told some facts about t ...

  9. P1825 [USACO11OPEN]玉米田迷宫Corn Maze

    题目描述 This past fall, Farmer John took the cows to visit a corn maze. But this wasn't just any corn m ...

  10. html的body内标签之超链接

    一,先来个简单的练练手,target="_blank" 的作用是在新的tab中打开页面.href 是超链接的意思. <!DOCTYPE html> <html l ...