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

目标:

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

前提

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

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

3、 在Jenkins里安装好以下插件:Email Extension PluginZentimestamp pluginRobot 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
%>
<STYLE>
BODY, TABLE, TD, TH, P {
  font-family:Verdana,Helvetica,sans serif;
  font-size:13px;
  color:black;
}
h1 { color:black; }
h2 { color:black; }
h3 { color:black; }
TD.bg1 { color:white; background-color:#0000C0; font-size:120% }
TD.bg2 { color:white; background-color:#4040FF; font-size:110% }
TD.bg3 { color:white; background-color:#8080FF; }
TD.test_passed { color:blue; }
TD.test_failed { color:red; }
TD.console { font-family:Courier New; }
</STYLE>
<BODY>
<!-- 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 %>
<p><h4>Robot Framework Results</h4></p>
<table cellspacing="0" cellpadding="4" border="1" align="left" width="80%">
        <thead>
          <tr bgcolor="#F3F3F3">
            <td><b>Type</b></td>
            <td><b>Total</b></td>
            <td><b>Passed</b></td>
            <td><b>Failed</b></td>
            <td><b>Pass %</b></td>
          </tr>
        </thead>


        <tbody>


          <tr><td><b>All Tests</b></td>
            <td><%= action.result.overallTotal %></td>
            <td><%= action.result.overallPassed %></td>
            <td><%= action.result.overallFailed %></td>
            <td><%= action.overallPassPercentage %></td>
          </tr>


          <tr><td><b>Critical Tests</b></td>
            <td><%= action.result.criticalTotal %></td>
            <td><%= action.result.criticalPassed %></td>
            <td><%= action.result.criticalFailed %></td>
            <td><%= action.criticalPassPercentage %></td>
          </tr>


        </tbody>
      </table>
<br />
<br />
<br />
<br />
<br />
<br />
<table cellspacing="0" cellpadding="4" border="1" align="left"  width="90%">
<thead>
<tr bgcolor="#F3F3F3">
  <td><b>Test Name</b></td>
  <td><b>Status</b></td>
  <td><b>Execution Datetime</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="3"><b><%= suiteName %></b></td></tr>
<%    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) { %>
<tr>
  <td><%= tc.displayName%></td>
  <td style="color: <%= tc.isPassed() ? "#66CC00" : "#FF3333" %>"><%= tc.isPassed() ? "PASS" : "FAIL" %></td>
  <td><%= execDate %></td>
</tr>
<% } %>
<%    } // tests
    } // suites %>
</tbody></table><%
  } // robot results
}
if (!robotResults) { %>
<p>No Robot Framework test results found.</p>
<%
} %>
</BODY>

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


猜你喜欢

转载自blog.csdn.net/pangnuonuo/article/details/80431908