【RFS】【robotframework_3】robotframework集成Jenkins執行並郵件發送執行結果

要求:

1.robotframework結合jenkins,實現集成構建

2.構建後能解析執行結果,並郵件發送給特定人的郵箱

3.郵件內容可以自定義,做到美觀(自帶的郵件內容不美觀),下圖是自定義模版

 

我的前置條件:

1.Jenkins部署在Windows環境下(如果Jenkins部署在Linux環境下,robotframework最好也搭建在Linux下)

2.robotframework也在Windows環境下,腳本沒放到svn上,本次內容不涉及svn(jenkins結合本地的腳本跑的)

3.已經安裝了Jenkins(下載地址https://jenkins.io/download/

 

步驟:

一。登錄Jenkins,系統設置

1.下載插件

系統管理-插件管理,選擇“可選插件”tab頁,在過濾框中依次搜索並下載:Robot Framework pluginEmail Extension Plugin,Zentimestamp plugin

2.系統設置郵件配置:系統管理-系統設置(只改圖中的地方,其他不用管)

 二。新建job

1.新建任務,選擇“構建一個自由風格的軟件項目”,點擊確定

2.1因爲我腳本沒放在svn上,所以這邊選擇無

2.2構建選擇執行Windows批處理命令,其中的路徑是你腳本的存放路徑,其他不變

 

3.3構建後操作,選擇publish robot framework test results,路徑是執行腳本後存放log地址,例如log.html,report.html等文件的目錄,下面數字不用管了

 

3.4 構建後操作,再選擇個editable email notification。配置信息如下

下圖中照選。如果沒有此頁面點擊advaned。其中reciplent list就是系統設置中郵箱收件人 

 

三。自定義郵件內容

找到Jenkins按照目錄,我的在d:/jenkins

目錄下有個文件夾叫email-templates,沒有自己新建個

在該文件夾下新建個文檔,命名爲:robot_results.groovy。裏面內容:

 

<%  
 import java.text.DateFormat  
 import java.text.SimpleDateFormat  
 %>  
    <!-- Robot Framework Results -->  
<%  
 def robotResults = false  
 def actions = build.actions // List<hudson.model.Action>  
 actions.each() { action ->  
  if( action.class.simpleName.equals("RobotBuildAction") ) { // hudson.plugins.robot.RobotBuildAction  
   robotResults = true %>
                   <div style="width:100%;float:left"> 
                    <table cellspacing="0" cellpadding="4" border="1" align="left">  
                        <thead>
                            <tr bgcolor="#F3F3F3">
                                <td style="text-align:center" colspan="4"><b>自動化測試彙總報告</b></td>    
                            </tr>
                            <tr>
                                <td bgcolor="#F3F3F3" style="width:80px"><b>詳細報告:</b></td>
                                <td colspan="4"><a href="${rooturl}${build.url}robot/report/report.html">點擊查看報告詳情</a></td>
                            </tr>                            
                            <tr bgcolor="#F3F3F3">
                                <td><b>用例總數</b></td>
                                <td><b>通過</b></td>
                                <td style="width:60px"><b>不通過</b></td>
                                <td style="width:100px"><b>通過率</b></td>
                            </tr>
                            <tr>
                                <td><%= action.result.overallTotal %></td>
                                <td><b><span style="color:#66CC00"><%= action.result.overallPassed %></span></b></td>
                                <td><b><span style="color:#FF3333"><%= action.result.overallFailed %></span></b></td>
                                <td><%= action.overallPassPercentage %>%</td>
                            </tr>
                            <tr bgcolor="#F3F3F3">  
                                <td colspan="2"><b>Test Name</b></td>  
                                <td><b>Status</b></td>  
                                <td><b>Elapsed Time</b></td>  
                            </tr>  
                        </thead>  
                        <tbody>  

<% def suites = action.result.allSuites  
   suites.each() { suite ->   
    def currSuite = suite  
    def suiteName = currSuite.displayName  
    // ignore top 2 elements in the structure as they are placeholders  
    while (currSuite.parent != null && currSuite.parent.parent != null) {  
     currSuite = currSuite.parent  
     suiteName = currSuite.displayName + "." + suiteName  
    } %>  

                            <tr>
                                <td colspan="4"><b><%= suiteName %></b></td>
                            </tr>  

<%  DateFormat format = new SimpleDateFormat("yyyyMMdd HH:mm:ss")
    def execDateTcPairs = []
    suite.caseResults.each() { tc ->  
      Date execDate = format.parse(tc.starttime)
      execDateTcPairs << [execDate, tc]
    }
    // primary sort execDate, secondary displayName
    execDateTcPairs = execDateTcPairs.sort{ a,b -> a[1].displayName <=> b[1].displayName }
    execDateTcPairs = execDateTcPairs.sort{ a,b -> a[0] <=> b[0] }
    execDateTcPairs.each() {
      def execDate = it[0]
      def tc = it[1]  %>

                            <tr>  
                                <td colspan="2"><%= tc.displayName %></td>  
                                <td><b><span style="color:<%= tc.isPassed() ? "#66CC00" : "#FF3333" %>"><%= tc.isPassed() ? "PASS" : "FAIL" %></span></b></td>  
                                <td><%= tc.getDuration().intdiv(60000)+"分"+(tc.getDuration()-tc.getDuration().intdiv(60000)*60000).intdiv(1000)+"秒" %></td>  
                            </tr>  
                            <% if(tc.errorMsg != null) {%>
                            <tr>
                                <td ><b><span style="font-size:10px;color:#FF3333">錯誤描述:</span></b></td>
                                <td colspan="3"><span style="font-size:10px"><%= tc.errorMsg%></span></td>

                            </tr>
                            <%
                             }%>
<%  } // tests  
   } // suites %>  
                        </tbody>
                    </table>

                     <p style="color:#AE0000;clear:both">*這個是通過Jenkins自動構建得出的報告,僅供參考。</p>
                    </div>
                    <%  
  } // robot results  
 }  
 if (!robotResults) { %>  
        <p>No Robot Framework test results found.</p>  
<%  
 } %>  

大功告成,去Jenkins裏構建下,就能收到郵件了。

參考博客: https://blog.csdn.net/ojiawang/article/details/51872481

https://blog.csdn.net/chushujin/article/details/81279127

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