UI自动化框架Jenkins+Robot Framework+git

目标:

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


前提

1、 配置好Robot Framework的环境,脚本可以正常运行

2、 部署好Jenkins的环境,Jenkins的安装不是本文的重点

3、 在Jenkins里安装好以下插件:Email Extension Plugin、Zentimestamp plugin、Robot Framework plugin

配置

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

》设置${BUILD_TIMESTAMP}格式


配置 Extended E-mail Notification默认设置


2、创建一个任务


3、

在源码管理处添加git源码地址,jenkins将从该位置获取项目代码:

URL获取位置:

Credentials点击“Add”添加:

Branch Specifier添加分之名,例如:AntTest,建议添全(refs/heads/AntTest,有疑问的地方可以点击旁边的小问号查看信息)

 构建触发器常用的有两种下面一一列举

a.当提交代码时触发 选中"Build when a change is pushed to Gitlab......"高级:

“Filter branches by name”中“include”添加触发该jenkins项目分支(防止上传文件到其他分支也触发这个项目),点击“Secret token”中“Generate”生成Token码,并复制。打开gitlab项目页,在侧边栏Setting中选中integrations,在URL栏中填写jenkins项目地址,Secret token中填写在jenkins中复制的Token码,点掉“Enable SSL verification”,点击“Add webhook”:

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

日程表中从左到右依次是:

分钟     小时     天       月      星期

0~59  0~23  1~31  1~12   0~7

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


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


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

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

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


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

4、自定义RobotFramework结构汇总的邮件模板格式,效果如,(我使用的模板 只显示失败的用例,具体全部的看附件)

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

在$Jenkins_Home/email-templates目录(如果没有email-templates请自行创建)下创建一个robot_results.groovy文件,内容如下:

<%
import java.text.DateFormat
import java.text.SimpleDateFormat
%>



<%
def robotResults = false
def actions = build.actions // List
actions.each() { action ->
  if( action.class.simpleName.equals("RobotBuildAction") ) { // hudson.plugins.robot.RobotBuildAction
    robotResults = true %>

Robot Framework Results



        
          
            
            
            
            
            
          
        


        


          


            
            
            
            
          


          


            
            
            
            
          


        


      
TypeTotalPassedFailedPass %
All Tests<%= action.result.overallTotal %><%= action.result.overallPassed %><%= action.result.overallFailed %><%= action.overallPassPercentage %>
Critical Tests<%= action.result.criticalTotal %><%= action.result.criticalPassed %><%= action.result.criticalFailed %><%= action.criticalPassPercentage %>
















  
  
  



<%  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
      } %>

<%    DateFormat format = new SimpleDateFormat("yyyyMMdd HH:mm:ss.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]  
        def tcresult = tc.isPassed() %>
<% if (!tcresult) { %>

  
  
  

<% } %>
<%    } // tests
    } // suites %>
Test NameStatusExecution Datetime
<%= suiteName %>
<%= tc.displayName%>"><%= tc.isPassed() ? "PASS" : "FAIL" %><%= execDate %>
<%
  } // robot results
}
if (!robotResults) { %>

No Robot Framework test results found.


<%
} %>

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


--------------------- 
作者:pangnuonuo 
来源:CSDN 
原文:https://blog.csdn.net/pangnuonuo/article/details/80431908 
版权声明:本文为博主原创文章,转载请附上博文链接!

你可能感兴趣的:(Jenkins,Testing)