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

如果你的路徑是/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結果報告。

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章