Jenkins(二)之自定义Robot Framework结果报告

目标:

实现RobotFramework的脚本定时自动执行,执行完后自动将结果发送到指定邮箱

前提

1、 配置好Robot Framework的环境,脚本可以正常运行,如果不会请看我之前写的博客Robot Framework 环境搭建

2、 部署好Jenkins的环境,Jenkins的安装不是本文的重点,不懂的请问度娘(其实很简单,装Tomcat,把Jenkins.war包扔到Tomcat的webapp目录里)

3、 在Jenkins里安装好以下插件:Email Extension PluginZentimestamp pluginRobot Framework plugin

配置

1、进入【系统管理】-【系统设置】进行如下配置:

》设置${BUILD_TIMESTAMP}格式

BUILD_TIMESTAMP格式化设置


》配置 Extended E-mail Notification默认设置

Extended E-mail Notification默认设置


2、创建一个任务

这里写图片描述


3、任务的配置

》设置保留的构建的数量

这里写图片描述


》设置每天凌晨1点的时候自动执行

这里写图片描述


》增加构建步骤,类型为:Execute shell

这里写图片描述


》设置运行RobotFramework脚本的命令,这里用-d 来定义RobotFramework的结果输出目录,格式如:

robot -d /结果输出/${BUILD_TIMESTAMP} /脚本目录/
    
    
  • 1

这里用${BUILD_TIMESTAMP}环境变量是让每次构建的结构都放在以日期格式命名的文件夹里

这里写图片描述


》增加构建后操作步骤Publish Robot Framework test results,配置如下:

这里写图片描述


》再增加一个构建后操作步骤Editable Email Notification,配置可以默认,也可以根据你的需要来配置,以下是我的配置:

这里写图片描述


4、自定义RobotFramework结构汇总的邮件模板格式,效果如:

这里写图片描述


大家是否记得前面 Extended E-mail Notification默认设置里Default Content的值是填写 ${SCRIPT, template=”robot_results.groovy”}

这里写图片描述

这里就告诉大家怎么去设置这个模板

在$Jenkins_Home/email-templates目录(如果没有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>  
<%  
 } %>  
    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95

其中Jenkins_Home的路径不知道在哪里的话,你可以去看一下系统设置页面,上面有写有:

这里写图片描述

完!

        <link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/markdown_views-ea0013b516.css">
            </div>

目标:

实现RobotFramework的脚本定时自动执行,执行完后自动将结果发送到指定邮箱

前提

1、 配置好Robot Framework的环境,脚本可以正常运行,如果不会请看我之前写的博客Robot Framework 环境搭建

2、 部署好Jenkins的环境,Jenkins的安装不是本文的重点,不懂的请问度娘(其实很简单,装Tomcat,把Jenkins.war包扔到Tomcat的webapp目录里)

3、 在Jenkins里安装好以下插件:Email Extension PluginZentimestamp pluginRobot Framework plugin

配置

1、进入【系统管理】-【系统设置】进行如下配置:

》设置${BUILD_TIMESTAMP}格式

BUILD_TIMESTAMP格式化设置


》配置 Extended E-mail Notification默认设置

Extended E-mail Notification默认设置


2、创建一个任务

这里写图片描述


3、任务的配置

》设置保留的构建的数量

这里写图片描述


》设置每天凌晨1点的时候自动执行

这里写图片描述


》增加构建步骤,类型为:Execute shell

这里写图片描述


》设置运行RobotFramework脚本的命令,这里用-d 来定义RobotFramework的结果输出目录,格式如:

robot -d /结果输出/${BUILD_TIMESTAMP} /脚本目录/
  
  
  • 1

这里用${BUILD_TIMESTAMP}环境变量是让每次构建的结构都放在以日期格式命名的文件夹里

这里写图片描述


》增加构建后操作步骤Publish Robot Framework test results,配置如下:

这里写图片描述


》再增加一个构建后操作步骤Editable Email Notification,配置可以默认,也可以根据你的需要来配置,以下是我的配置:

这里写图片描述


4、自定义RobotFramework结构汇总的邮件模板格式,效果如:

这里写图片描述


大家是否记得前面 Extended E-mail Notification默认设置里Default Content的值是填写 ${SCRIPT, template=”robot_results.groovy”}

这里写图片描述

这里就告诉大家怎么去设置这个模板

在$Jenkins_Home/email-templates目录(如果没有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>  
<%  
 } %>  
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95

其中Jenkins_Home的路径不知道在哪里的话,你可以去看一下系统设置页面,上面有写有:

这里写图片描述

完!

        <link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/markdown_views-ea0013b516.css">
            </div>

猜你喜欢

转载自blog.csdn.net/qq_38317509/article/details/81316940