Authored by zhengyouwei

项目发布增加自动测试

package com.ui.model.req;
import lombok.Data;
/**
* Created by zhengyouwei on 2016/11/28.
*/
@Data
public class AutoTestReq {
private String taskname;
private String env;
private String models;
private String branch;
private int proId;
private int versionId;
private String mailto;
}
... ...
... ... @@ -8,6 +8,7 @@ import com.ui.model.domain.BuildMessage;
import com.ui.model.req.*;
import com.ui.project.ProjectEnvironment;
import com.ui.project.ProjectOnline;
import org.apache.commons.lang.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
... ... @@ -17,6 +18,7 @@ import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;
/**
... ... @@ -142,12 +144,12 @@ public class ProjectBuildCtrl {
if ("Deploy".equals(operate_name)) {
String[] array = project_name.split(",");
if (array.length > 4) {
String[] newArray = new String[]{"","","",""};
String[] newArray = new String[]{"", "", "", ""};
for (int i = 0; i < array.length; i++) {
newArray[i%4] = newArray[i%4] + array[i] + ",";
newArray[i % 4] = newArray[i % 4] + array[i] + ",";
}
for (String project : newArray) {
buildRequest.setProject(project.substring(0,project.length() - 1));
buildRequest.setProject(project.substring(0, project.length() - 1));
BuildMessage buildMessage = httpRestClient.post(ProjectEnvironment.getUrl(environment_name) + "build", buildRequest, BuildMessage.class);
if (buildMessage != null) {
list.add(buildMessage);
... ... @@ -275,4 +277,37 @@ public class ProjectBuildCtrl {
return response;
}
@RequestMapping("/aototest/autoTestService")
@ResponseBody
public BaseResponse autoTestService(String models, String branch, HttpSession session) {
User user = (User) session.getAttribute("user");
AutoTestReq autoTestReq = new AutoTestReq();
String id = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + RandomStringUtils.randomNumeric(6);
autoTestReq.setTaskname(id);
autoTestReq.setEnv("5");
autoTestReq.setModels(models.substring(0, models.length() - 1));
autoTestReq.setBranch(branch);
autoTestReq.setProId(0);
autoTestReq.setVersionId(0);
autoTestReq.setMailto(user.getEmail());
BaseResponse baseResponse = httpRestClient.post("http://qmc.yohops.com:9999/autotest/autoTestService", autoTestReq, BaseResponse.class);
System.out.println(id);
return baseResponse;
}
@RequestMapping("/aototest/getAutoTestResult")
@ResponseBody
public BaseResponse getAutoTestResult(String id) {
AutoTestReq autoTestReq = new AutoTestReq();
autoTestReq.setTaskname(id);
BaseResponse baseResponse = httpRestClient.post("http://qmc.yohops.com:9999/autotest/getAutoTestResult", autoTestReq, BaseResponse.class);
return baseResponse;
}
@RequestMapping("/aototest/toAutoTestResult")
public ModelAndView toAutoTestResult(String id,Model model) {
model.addAttribute("id",id);
return new ModelAndView("project/autotestresult");
}
}
... ...
<%@page language="java" contentType="text/html;charset=utf-8" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="<%=basePath %>css/bootstrap.min.css"/>
<link rel="stylesheet" href="<%=basePath %>css/bootstrap-responsive.min.css"/>
<link rel="stylesheet" href="<%=basePath %>css/jquery.gritter.css"/>
<link rel="stylesheet" href="<%=basePath %>css/jquery-ui.css"/>
<script src="<%=basePath %>js/excanvas.min.js" charset="UTF-8" type="text/javascript"></script>
<script src="<%=basePath %>js/jquery-1.12.0.min.js" charset="UTF-8" type="text/javascript"></script>
<script src="<%=basePath %>js/jquery-ui.custom.js" charset="UTF-8" type="text/javascript"></script>
<script src="<%=basePath %>/js/bootstrap.min.js"></script>
<script src="<%=basePath %>/js/unicorn.js"></script>
<script src="<%=basePath %>js/bootstrap-plugin/datetimepicker/moment-with-locales.js" charset="UTF-8"
type="text/javascript"></script>
<script src="<%=basePath %>js/bootstrap-plugin/datetimepicker/bootstrap-datetimepicker.js" charset="UTF-8"
type="text/javascript"></script>
<script src="<%=basePath %>js/global.js" charset="UTF-8" type="text/javascript"></script>
<script src="<%=basePath %>js/bootstrap-plugin/bootstrap.pagination.js" charset="UTF-8"
type="text/javascript"></script>
<script src="<%=basePath %>js/bootstrap-plugin/bootstrap.table.js" charset="UTF-8" type="text/javascript"></script>
<script src="<%=basePath %>js/bootstrap-plugin/bootstrap.dialog.js" charset="UTF-8" type="text/javascript"></script>
<script src="<%=basePath %>js/bootstrap-plugin/bootstrap.form.js" charset="UTF-8" type="text/javascript"></script>
<script src="<%=basePath %>js/bootstrap-plugin/bootstrap.panel.js" charset="UTF-8" type="text/javascript"></script>
<script src="<%=basePath %>js/bootstrap-plugin/bootstrap.alerts.js" charset="UTF-8" type="text/javascript"></script>
<script src="<%=basePath %>js/bootstrap-plugin/bootstrap.accordion.js" charset="UTF-8"
type="text/javascript"></script>
<script src="<%=basePath %>js/bootstrap-plugin/bootstrap.breadcrumb.js" charset="UTF-8"
type="text/javascript"></script>
<script src="<%=basePath %>js/bootstrap-plugin/bootstrap.validate.js" charset="UTF-8"
type="text/javascript"></script>
<script src="<%=basePath %>js/bootstrap-plugin/bootstrap.form.js" charset="UTF-8" type="text/javascript"></script>
<script src="<%=basePath %>js/layer/layer.js" charset="UTF-8" type="text/javascript"></script>
<script src="<%=basePath %>js/bootstrap-plugin/bootstrap.select.js" charset="UTF-8" type="text/javascript"></script>
<script src="<%=basePath %>js/jstree/jstree.min.js"></script>
<script src="<%=basePath %>js/jquery.toaster.js"></script>
<script src="<%=basePath %>js/jquery.gritter.min.js"></script>
<script>
var contextPath = '<%=basePath %>';
</script>
<title>YOHO!运维</title>
</head>
<body >
<!-- 右侧具体内容 -->
<input type="hidden" id="hidden_id" value="${id}">
<div>
<br/>
<br/>
<br/>
<br/>
<span>
tips:测试结果正在获取中,请不要关闭页面。或者您也可以等待邮件,邮件稍后会发送到您的邮箱!
</span>
</div>
<div id="infoTable">
</div>
</body>
<script>
$(function () {
var jq = $("#infoTable");
var id = $("#hidden_id").val();
var tableParam = {
columnAutoWidth: false,
url: contextPath + "project/aototest/getAutoTestResult?id=" + id,
striped: true,
title: "自动测试结果",
dataType: "json",
beforeSend:function(){
var div = $("<div>").attr("tableSelector", jq.selector).addClass("modal-backdrop fade in").appendTo($("body")).hide();
var tableOffset = jq.offset();
var tableWidth = jq.outerWidth(true), tableHeight = jq.outerHeight(true);
div.append($("<div>").addClass("table-loading").css({
left : (tableWidth - 124) / 2,
top : (tableHeight - 124) / 2
}));
div.css({
width : tableWidth,
height : tableHeight,
left : tableOffset.left,
top : tableOffset.top
});
div.show();
},
loadFilter: function (data) {
$("body").find("div[tableSelector='"+ jq.selector +"']").remove();
if (!data || data.code != 200) {
return [];
}else{
clearInterval(interval);
}
return data.data.autotestRunCollections;
},
onLoadSuccess: function (data) {
},
columns: [{
title: "版本号",
field: "versionId",
width: "10%"
}, {
title: "执行集名称",
field: "runCollectionName",
width: "10%"
}, {
title: "总数",
field: "testcaseTotal",
width: "10%"
},{
title: "运行时间",
field: "totalTime",
width: "10%"
},{
title: "成功",
field: "testcaseSuccess",
width: "10%"
},{
title: "失败",
field: "testcaseFailures",
width: "10%"
},{
title: "错误",
field: "testcaseErrors",
width: "10%"
},{
title: "跳过 ",
field: "testcaseSkips",
width: "10%"
}
]
};
$("#infoTable").table(tableParam);
var interval = setInterval(function () {//查后台,展示数据
$("#infoTable").table(tableParam);
}, 5000);
});
</script>
<script>
document.onkeydown = function()
{
if(event.keyCode==116 || event.keyCode==8 || (event.ctrlKey && event.keyCode==82) || event.keyCode == 13) {
event.keyCode=0;
event.returnValue = false;
}
}
document.oncontextmenu = function() {event.returnValue = false;}
</script>
\ No newline at end of file
... ...
... ... @@ -39,7 +39,7 @@
<div class="form-group">
<label class="col-sm-1 control-label">操作选择</label>
<div class="col-sm-8">
<div class="col-sm-8" style="display: inline">
<div class="rdio rdio-default">
<input type="radio" name="operate" id="operatedeploy" value="Deploy" checked="checked"
onclick="operateChange()"/>
... ... @@ -51,6 +51,9 @@
<label for="operateback">回滚</label>
</div>
</div>
<div id="autotest-div">
<input type="button" class="btn btn-primary" id="autotest-button" onclick="autotest()" value="执行自动测试用例" </input>
</div>
</div>
<div class="form-group">
... ...
... ... @@ -72,10 +72,8 @@
var removeArray = new Array();
$(function () {
var interval;
var ip = $("#hidden_ip").val();
var cloud = $("#hidden_cloud").val();
var exe = $("#hidden_exe").val();
var project = $("#hidden_project").val();
var tableParam = {
columnAutoWidth: false,
url: contextPath + "/javaRestart/stopOrRestart",
... ... @@ -86,10 +84,7 @@
return defaultLoadFilter(data);
},
onLoadSuccess: function (data) {
$.each(data, function (idx, val) {
myArray[idx] = val.id;
});
setStatus();
clearInterval(interval);
},
columns: [{
title: "项目名称",
... ... @@ -132,15 +127,9 @@
//加载表格
$("#infoTable").table(tableParam);
$("#infoTable").table("load", {
'ip': ip,
'cloud': cloud,
'exe': exe,
'project': project
});
var interval = setInterval(function () {//查后台,展示数据
setStatus();
interval = setInterval(function () {//查后台,展示数据
$("#infoTable").table("load");
}, 3000);
});
... ...
... ... @@ -13,12 +13,16 @@ function operateChange() {
document.getElementById("branch-div").style.display = "none";
//展示 回滚选择框
document.getElementById("rollback-div").style.display = "";
//隐藏自动执行测试用例
document.getElementById("autotest-div").style.display = "none";
} else {
//展示Branch输入框
document.getElementById("branch-div").style.display = "";
//隐藏 回滚选择框
document.getElementById("rollback-div").style.display = "none";
//展示自动执行测试用例
document.getElementById("autotest-div").style.display = "";
}
}
... ... @@ -278,7 +282,54 @@ function branchdefault() {
}
}
function autotest(){
var environment = $("input[name='environments']:checked").val();
if("qcloud_gray" == environment){
alert("灰度环境不支持测试用例执行");
return;
}
var projects = new Array();
$('input[name="project1"]:checked').each(function () {
projects.push($(this).val());//向数组中添加元素
});
$('input[name="project2"]:checked').each(function () {
projects.push($(this).val());//向数组中添加元素
});
$('input[name="project3"]:checked').each(function () {
projects.push($(this).val());//向数组中添加元素
});
if (projects.length == 0) {
alert("请选择项目");
return;
}
var branch = $("input[name='branch']").val();
if (branch == "") {
alert("请填写分支名称");
return;
}
$.ajax({
url: 'aototest/autoTestService?models=' + projects + "&branch=" + branch,
type: 'POST',
dataType: 'json',
success: function (data) {
console.log(data);
if(data.code != 200){
alert(data.message);
}else{
var iWidth = 900;
var iHeight = 600;
var iTop = (window.screen.availHeight - 30 - iHeight) / 2; //获得窗口的垂直位置;
var iLeft = (window.screen.availWidth - 10 - iWidth) / 2; //获得窗口的水平位置;
window.open("aototest/toAutoTestResult?id=" + data.data.taskname, "_blank", "height=" + iHeight + ", width=" + iWidth + ", top=" + iTop + ", left=" + iLeft);
}
},
error: function (e) {
alert("系统错误");
}
});
}
... ...