RobotFramework在Mac平台安装小结、邮件发送功能及jenkins结果展示配置

Mac平台下安装RobotFramework的步骤

需要注意的是:Python最好下载2.7.15版本,pip最好安装使用9.0.1版本的,防止出现类似SSL版本不支持的问题。

 

安装步骤:

  1. 需要安装pip模块,可以到官网:https://pypi.org/project/pip/去安装。

pip最好下载9.0.1版本的。

  1. sudo pip install robotframework.
  2. Sudo pip install –U robotframework-ride==2.0a1

最麻烦的是安装wxPython。

mac平台下安装homebrew。(Homebrew是一款Mac OS平台下的软件包的管理工具,拥有安装、卸载、更新、查看、搜索等很多实用的功能)。

安装homebrew:/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

 

首先查看一下自己的python引用是哪个路径:which python

扫描二维码关注公众号,回复: 5112238 查看本文章

如果你的路径是/usr/bin/python,一定要改成/usr/local/bin/python!我在这里已经踩过坑了。

如何修改:命令行执行open –e .bash_profile

将原来的python环境注释掉,修改成alias python="/usr/local/bin/python"。

安装wxPython:

我是参看这个链接完成的环境配置:https://blog.csdn.net/whackw/article/details/51879658

下载wxPython:https://sourceforge.net/projects/wxpython/files/wxPython/

或者下载它的最新版本:https://sourceforge.net/projects/wxpython/files/latest/download?source=files

我下载的是wxPython3.0-osx-3.0.2.0-cocoa-py2.7.dmg。

下载完成后双击打开。然后在pkg文件上右击选择显示包内容,打开Contents/Resources。

解压缩

 

解压缩之后找到usr/local/lib,将它下边的wxPython-3.0.2.0这个文件夹拷贝到系统的usr/local/lib文件夹下。

 

将Contents/Resources下的postflight文件复制到一个位置,然后cd到这个文件所在的目录下,执行:

sudo ./postflight。

 

执行成功后它会进行安装:

安装完成后再在cmd下

执行ride.py就可以了。

不会再出现:

wxPython not found.

You need to install wxPython 2.8.12.1 or 3.0.2 or newer with unicode support to run RIDE.wxPython can be downloaded from http://sourceforge.net/projects/wxpython/files/wxPython/

 

RobotFramework的相关学习文档:

https://blog.csdn.net/tulituqi  虫师robotframwork

http://franz-see.github.io/Robotframework-Database-Library/ 数据库官网

http://bulkan.github.io/robotframework-requests/ 关键字库

http://robotframework.org/robotframework/latest/libraries/Collections.html collection

 

 

 

安装必要的library库:

pip install robotframework-requests

 

 

注意:Mac平台下的RobotFramework的site-packages在/Library/Python/2.7/site-packages这个路径下。

而使用pip安装的包全部保存在/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages这里。

所以在命令行使用ride.py启动RIDE后,在导入Library时不能进行识别导入的包。

所以,在使用pip安装包时,需要使用-t 选项来指定位置。

例如:pip install –t /Library/Python/2.7/site-packages robotframework-requests

 

RobotFramework扩展发送结果邮件功能

  1. 写一个发邮件的模块。保存成sendEmail.py,复制到$pythonpath/Lib/site-packages/robot目录下。

注意,修改mail_user为自己的发件QQ邮箱,mail_pass设置为授权码,不会的请百度。有更复杂的需求自行扩展该文件。

#!/usr/bin/python

# -*- coding: UTF-8 -*-

 

import smtplib

from email.mime.text import MIMEText

import email.MIMEMultipart

from email.header import Header

import os

import mimetypes

 

def send_email(receivers= ['********@qq.com'], file_names=[], test_result=0):

    # 第三方 SMTP 服务

    print receivers

    mail_host="smtp.qq.com"  #设置服务器

    mail_user="*******@qq.com"    #用户名

    mail_pass="************"   #口令,QQ邮箱是输入授权码,在qq邮箱设置 里用验证过的手机发送短信获得,不含空格

    sender = '********@qq.com'

 

    #设置邮件中的测试结果

    resultstr = '失败' if test_result else '通过'

 

    main_msg = email.MIMEMultipart.MIMEMultipart()

    message = MIMEText('''附件是本次自动化构建的报告,请注意查收 \n\n''', 'plain', 'utf-8')

    main_msg.attach(message)

    result = MIMEText('测试结果: '+resultstr, 'plain', 'utf-8')

    main_msg.attach(result)

    ## 读入文件内容并格式化

    for file_name in file_names:

        data = open(file_name, 'rb')

        ctype,encoding = mimetypes.guess_type(file_name)

        if ctype is None or encoding is not None:

            ctype = 'application/octet-stream'

        maintype,subtype = ctype.split('/',1)

        file_msg = email.MIMEBase.MIMEBase(maintype, subtype)

        file_msg.set_payload(data.read())

        data.close()

        email.Encoders.encode_base64(file_msg)#把附件编码

 

        basename = os.path.basename(file_name)

        file_msg.add_header('Content-Disposition','attachment', filename = basename)#修改邮件头

        main_msg.attach(file_msg)

 

    main_msg['From'] = Header("robot自动发送", 'utf-8')

    reciverstr = ';'.join(receivers)

    main_msg['To'] = Header(reciverstr, 'utf-8')

 

    subject = 'robotframework测试结果'

    main_msg['Subject'] = Header(subject, 'utf-8')

 

 

    try:

        smtpObj = smtplib.SMTP_SSL()

        smtpObj.connect(mail_host, 465)

        smtpObj.login(mail_user,mail_pass)

        smtpObj.sendmail(sender, receivers, main_msg.as_string())

        print "邮件发送成功。"

    except smtplib.SMTPException, e:

        print "Error: 无法发送邮件。错误原因:", e

 

if __name__ == '__main__':

    send_email(file_names=['E:\\pics\\1.png','E:\\pics\\2.jpg'])

2 /Lib/site-packages/robot/run.py修改两个地方
2.1 修改run.py处理send_email动作:
        件模

from robot.sendEmail import send_email

再找到RobotFrameworkmain函数,增加*号中的如下行:

def main(self, datasources, **options):
        settings = RobotSettings(options)
        LOGGER.register_console_logger(**settings.console_output_config)
        LOGGER.info('Settings:\n%s' % unic(settings))
        suite = TestSuiteBuilder(settings['SuiteNames'],
                                 settings['WarnOnSkipped'],
                                 settings['Extension']).build(*datasources)
        suite.configure(**settings.suite_config)
        if settings.pre_run_modifiers:
            suite.visit(ModelModifier(settings.pre_run_modifiers,
                                      settings.run_empty_suite, LOGGER))
        with pyloggingconf.robot_handler_enabled(settings.log_level):
            result = suite.run(settings)
            LOGGER.info("Tests execution ended. Statistics:\n%s"
                        % result.suite.stat_message)
            if settings.log or settings.report or settings.xunit:
                writer = ResultWriter(settings.output if settings.log
                                      else result)
                writer.write_results(settings.get_rebot_settings())
        #*********************************#
        if len(settings['Email']):
            send_email(settings['Email'],[settings.report,settings.log],
                        test_result=result.return_code)
        #*********************************#
        return result.return_code

2.2 还是run.py,修改USAGES里的options
        增加如下行:

Options
=======
 -m --email email *       set email options #增加这行,不要把注释复制
 -F --extension value     Parse only files with this extension when executing

主要是pybot命令行使用
 
3  
找到robot/conf/settings.py文件修改settings
        找到 _cli_opts 设置,增加一条

'Email'            : ('email', []),

修改robotide,找到$pythonpath/Lib/site-packages/robotide/contrib/testrunner/usages.py

Options
=======
 -m --email email *       set email options#增加这行,不要把注释复制进去
 -N --name name           Set the name of the top level test suite. Underscores

主要ride设置时使用。
 
运行及
    1  pybot运行方式,如需多个人,需后面再跟--email XXXX
        pybot --email [email protected] --email [email protected] "C:\\Users\\Administrator\\Desktop\\test.robot"
    2  ride 运行方式

 

 

扩展发送结果邮件的功能参考链接:http://www.robotframework.net/?/article/118

RobotFramework测试报告结果在jenkins中展示

  1. 在jenkins中添加jenkins插件。

在系统管理->管理插件->可选插件->Robot Framework Plugin,然后安装。

  1. 在构建时,这里我用的是我自己的本地RF工程,所以直接将该工程复制到了本地的jenkins工作的该job工程的目录下。

然后在jenkins的Execute shell中增加构建步骤:

/usr/local/bin/pybot –email [email protected] ${WORKSPACE}/Test/test.txt  ##这里的test.txt表示的是需要运行的RF脚本。

因为已经安装了Robot Framework Plugin,所以在构建后操作中选择Publish Robot Framework test results该选项,在Directory of Robot output添加:${WORKSPACE}。

每次在构建完成之后,就可以在该job工程中看到Robot Results结果,它实际上就是你${WORKSPACE}下的该RF结果报告。

 

 

猜你喜欢

转载自blog.csdn.net/Jack_Chen3/article/details/83151624