增加把测试结果推送到企业微信以及阿里钉钉的功能

This commit is contained in:
seagull 2020-09-17 20:13:34 +08:00
parent e00e4a813d
commit 5721cc4da7
11 changed files with 462 additions and 415 deletions

View File

@ -46,7 +46,7 @@
<p><b>自动构建状态: </b> 【<font color='#5CACEE'>${buildstatus}</font>】</p>
<p><b>自动重启TOMCAT状态 </b> 【<font color='#5CACEE'>${restartstatus}</font>】</p>
<br>
<p><b>本次任务预期执行用例共【<font color='#2828FF'>${casecount}</font>】条,耗時【${time}】</b></p>
<p><b>本次任务预期执行用例共【<font color='#2828FF'>${casecount}</font>】条,耗時【<font color='#2828FF'>${time}</font>】</b></p>
<p><b>用例执行成功: </b> 【<font color='#28FF28'>${casesuc}</font>】</p>
<p><b>用例执行失败: </b> 【<font color='#FF0000'>${casefail}</font>】</p>
<p><b>用例有可能由于脚本原因未成功解析被锁定:</b> 【<font color='#AE57A4'>${caselock}</font>】</p>

View File

@ -21,11 +21,6 @@ mail.smtp.username=xxxxx@xx.com
mail.smtp.password=xxxxx
#邮件模板路径
mail.freemarker.template=/email_template/
#==================向第三方应用推送任务执行结果(JSON)========================
#第三方平台推送开关 true打开 false关闭
task.push.switch=false
#第三方平台推送URL
task.push.url=localhost
#==================Jenkins API配置========================
#jenkins访问地址示例 http://192.168.100.100:8080/jenkins/
jenkins.url=http://localhost

View File

@ -168,7 +168,7 @@ public class AppTestControl {
LogUtil.APP.info("当前项目【{}】测试计划中的用例已经全部执行完成...",projectname);
MailSendInitialization.sendMailInitialization(HtmlMail.htmlSubjectFormat(jobname),
HtmlMail.htmlContentFormat(tastcount, taskId, buildResult.toString(), restartstatus, testtime, jobname),
taskId, taskScheduling, tastcount);
taskId, taskScheduling, tastcount,testtime,buildResult.toString(),restartstatus);
// 关闭APP以及appium会话
if ("Android".equals(properties.getProperty("platformName"))) {
assert androiddriver != null;
@ -179,11 +179,11 @@ public class AppTestControl {
}
} else {
LogUtil.APP.warn("项目构建失败自动化测试自动退出请前往JENKINS中检查项目构建情况。");
MailSendInitialization.sendMailInitialization(jobname, "构建项目过程中失败自动化测试自动退出请前去JENKINS查看构建情况", taskId, taskScheduling, null);
MailSendInitialization.sendMailInitialization(jobname, "构建项目过程中失败自动化测试自动退出请前去JENKINS查看构建情况", taskId, taskScheduling, null,"0小时0分0秒",buildResult.toString(),restartstatus);
}
} else {
LogUtil.APP.warn("项目TOMCAT重启失败自动化测试自动退出请检查项目TOMCAT运行情况。");
MailSendInitialization.sendMailInitialization(jobname, "项目TOMCAT重启失败自动化测试自动退出请检查项目TOMCAT运行情况", taskId, taskScheduling, null);
MailSendInitialization.sendMailInitialization(jobname, "项目TOMCAT重启失败自动化测试自动退出请检查项目TOMCAT运行情况", taskId, taskScheduling, null,"0小时0分0秒",buildResult.toString(),restartstatus);
}
//关闭Appium服务的线程
if(as!=null){

View File

@ -153,18 +153,18 @@ public class TestControl {
String testtime = serverOperation.getTestTime(taskid);
MailSendInitialization.sendMailInitialization(HtmlMail.htmlSubjectFormat(jobname),
HtmlMail.htmlContentFormat(tastcount, taskid, buildResult.toString(), restartstatus, testtime, jobname),
taskid, taskScheduling, tastcount);
taskid, taskScheduling, tastcount,testtime,buildResult.toString(),restartstatus);
threadExecute.shutdown();
LogUtil.APP.info("亲,没有下一条啦!我发现你的用例已经全部执行完毕,快去看看有没有失败的用例吧!");
} else {
LogUtil.APP.warn("项目构建失败,自动化测试自动退出!请查看构建日志检查项目构建情况...");
MailSendInitialization.sendMailInitialization(jobname, "构建项目过程中失败,自动化测试自动退出!请查看构建日志检查项目构建情况...", taskid,
taskScheduling, null);
taskScheduling, null,"0小时0分0秒",buildResult.toString(),restartstatus);
}
} else {
LogUtil.APP.warn("项目TOMCAT重启失败自动化测试自动退出请检查项目TOMCAT运行情况。");
MailSendInitialization.sendMailInitialization(jobname, "项目TOMCAT重启失败自动化测试自动退出请检查项目TOMCAT运行情况", taskid,
taskScheduling, null);
taskScheduling, null,"0小时0分0秒",buildResult.toString(),restartstatus);
}
}

View File

@ -135,19 +135,19 @@ public class WebTestControl {
LogUtil.APP.info("当前项目【{}】测试计划中的用例已经全部执行完成...",projectname);
MailSendInitialization.sendMailInitialization(HtmlMail.htmlSubjectFormat(jobname),
HtmlMail.htmlContentFormat(tastcount, taskid, buildResult.toString(), restartstatus, testtime, jobname),
taskid, taskScheduling, tastcount);
taskid, taskScheduling, tastcount,testtime,buildResult.toString(),restartstatus);
// 关闭浏览器
assert wd != null;
wd.quit();
} else {
LogUtil.APP.warn("项目构建失败自动化测试自动退出请前往JENKINS中检查项目构建情况。");
MailSendInitialization.sendMailInitialization(jobname, "构建项目过程中失败自动化测试自动退出请前去JENKINS查看构建情况", taskid,
taskScheduling, null);
taskScheduling, null,"0小时0分0秒",buildResult.toString(),restartstatus);
}
} else {
LogUtil.APP.warn("项目TOMCAT重启失败自动化测试自动退出请检查项目TOMCAT运行情况。");
MailSendInitialization.sendMailInitialization(jobname, "项目TOMCAT重启失败自动化测试自动退出请检查项目TOMCAT运行情况", taskid,
taskScheduling, null);
taskScheduling, null,"0小时0分0秒",buildResult.toString(),restartstatus);
}
}

View File

@ -116,13 +116,12 @@ public class serverOperation {
* 发送 eMailer varchar(100) ; --收件人
*/
public static String[] getEmailAddress(String taskIdStr) {
int taskId = Integer.parseInt(taskIdStr);
public static String[] getEmailAddress(TaskScheduling taskScheduling,String taskIdStr) {
String[] address = null;
try {
TaskScheduling taskScheduling = GetServerApi.cGetTaskSchedulingByTaskId(taskId);
if (!taskScheduling.getEmailSendCondition().equals(-1)) {
String temp = taskScheduling.getEmailAddress();
if(StrUtil.isNotBlank(temp)){
// 清除最后一个;
if (temp.contains(";") && temp.substring(temp.length() - 1).contains(";")) {
temp = temp.substring(0, temp.length() - 1);
@ -135,6 +134,9 @@ public class serverOperation {
address = new String[1];
address[0] = temp;
}
}else{
LogUtil.APP.warn("邮件地址配置为空,取消邮件发送...");
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
@ -231,8 +233,7 @@ public class serverOperation {
long hour = (l / (60 * 60 * 1000) - day * 24);
long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
desTime = "<font color='#2828FF'>" + hour + "</font>Сʱ<font color='#2828FF'>" + min
+ "</font>·Ö<font color='#2828FF'>" + s + "</font>Ãë";
desTime = hour + "小时" + min + "" + s + "";
}
} catch (Exception e) {
// TODO Auto-generated catch block

View File

@ -30,6 +30,8 @@ public class TaskScheduling extends BaseEntity
private Integer clientId;
/** 邮件通知地址 */
private String emailAddress;
/** 第三方推送地址 */
private String pushUrl;
/** 发送邮件通知时的具体逻辑, 0-全部1-成功,-1-失败 */
private Integer emailSendCondition;
/** jenkins构建链接 */
@ -159,6 +161,16 @@ public class TaskScheduling extends BaseEntity
{
return emailAddress;
}
public void setPushUrl(String pushUrl)
{
this.pushUrl = pushUrl;
}
public String getPushUrl()
{
return pushUrl;
}
public void setEmailSendCondition(Integer emailSendCondition)
{
this.emailSendCondition = emailSendCondition;
@ -241,6 +253,7 @@ public class TaskScheduling extends BaseEntity
.append("planId", getPlanId())
.append("clientId", getClientId())
.append("emailAddress", getEmailAddress())
.append("pushUrl", getPushUrl())
.append("emailSendCondition", getEmailSendCondition())
.append("buildingLink", getBuildingLink())
.append("remoteShell", getRemoteShell())

View File

@ -39,7 +39,7 @@ public class FreemarkerEmailTemplate {
/**
* Ä£°åÒýÇæÅäÖÃ
*/
public String getText(String templateId, Map<Object, Object> parameters) {
public String getText(String templateId, Map<String, Object> parameters) {
@SuppressWarnings("deprecation")
Configuration configuration = new Configuration();
configuration.setTemplateLoader(new ClassTemplateLoader(FreemarkerEmailTemplate.class, TEMPLATE_PATH));

View File

@ -1,9 +1,13 @@
package luckyclient.tool.mail;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import luckyclient.remote.entity.ProjectProtocolTemplate;
import luckyclient.utils.LogUtil;
import luckyclient.utils.config.SysConfig;
@ -22,53 +26,25 @@ import luckyclient.utils.httputils.HttpClientTools;
public class HtmlMail {
static FreemarkerEmailTemplate fet = new FreemarkerEmailTemplate();
public static String htmlContentFormat(int[] taskcount, String taskid, String buildstatus, String restartstatus, String time, String jobname) {
Map<Object, Object> parameters = new HashMap<>(0);
parameters.put("buildstatus", buildstatus);
parameters.put("restartstatus", restartstatus);
parameters.put("casecount", taskcount[0]);
parameters.put("casesuc", taskcount[1]);
parameters.put("casefail", taskcount[2]);
parameters.put("caselock", taskcount[3]);
parameters.put("caseunex", taskcount[4]);
public static String htmlContentFormat(int[] taskCount, String taskId, String buildStatus, String restartStatus, String time, String jobName) {
Map<String, Object> parameters = new HashMap<>(0);
parameters.put("buildstatus", buildStatus);
parameters.put("restartstatus", restartStatus);
parameters.put("casecount", taskCount[0]);
parameters.put("casesuc", taskCount[1]);
parameters.put("casefail", taskCount[2]);
parameters.put("caselock", taskCount[3]);
parameters.put("caseunex", taskCount[4]);
parameters.put("time", time);
parameters.put("taskid", taskid);
parameters.put("jobname", jobname);
try {
Map<String, String> headmsg = new HashMap<>(0);
Properties properties = SysConfig.getConfiguration();
if ("true".equals(properties.getProperty("task.push.switch").toLowerCase())) {
LogUtil.APP.info("开始向第三方平台推送任务执行情况...");
Map<String, Object> pushparameters = new HashMap<>(0);
pushparameters.put("buildstatus", buildstatus);
pushparameters.put("restartstatus", restartstatus);
pushparameters.put("casecount", taskcount[0]);
pushparameters.put("casesuc", taskcount[1]);
pushparameters.put("casefail", taskcount[2]);
pushparameters.put("caselock", taskcount[3]);
pushparameters.put("caseunex", taskcount[4]);
pushparameters.put("time", time);
pushparameters.put("taskid", taskid);
pushparameters.put("jobname", jobname);
parameters.put("taskid", taskId);
parameters.put("jobname", jobName);
String pushurl = properties.getProperty("task.push.url");
ProjectProtocolTemplate ppt=new ProjectProtocolTemplate();
ppt.setEncoding("utf-8");
ppt.setTimeout(60);
HttpClientTools hct = new HttpClientTools();
hct.httpClientPostJson(pushurl, pushparameters, headmsg,ppt);
}
} catch (Exception e) {
LogUtil.APP.error("向第三方平台推送任务执行情况出现异常,请检查!", e);
return fet.getText("task-body", parameters);
}
return fet.getText("task-body", parameters);
}
public static String htmlSubjectFormat(String jobname) {
Map<Object, Object> parameters = new HashMap<>(0);
Map<String, Object> parameters = new HashMap<>(0);
parameters.put("jobname", jobname);
return fet.getText("task-title", parameters);
}
}

View File

@ -1,11 +1,18 @@
package luckyclient.tool.mail;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import luckyclient.remote.api.serverOperation;
import luckyclient.remote.entity.ProjectProtocolTemplate;
import luckyclient.remote.entity.TaskScheduling;
import luckyclient.utils.LogUtil;
import luckyclient.utils.config.SysConfig;
import luckyclient.utils.httputils.HttpClientTools;
/**
* =================================================================
@ -19,7 +26,7 @@ import luckyclient.utils.config.SysConfig;
*/
public class MailSendInitialization {
public static void sendMailInitialization(String subject, String content, String taskid, TaskScheduling taskScheduling, int[] taskCount) {
public static void sendMailInitialization(String subject, String content, String taskid, TaskScheduling taskScheduling, int[] taskCount, String time, String buildStatus, String restartStatus) {
boolean isSend = false;
if (null == taskCount) {
isSend = true;
@ -27,28 +34,32 @@ public class MailSendInitialization {
if (taskCount.length == 5 && null != taskScheduling) {
Integer sendCondition = taskScheduling.getEmailSendCondition();
// 用例全部成功了发送, casecount != casesuc
if (null!=sendCondition&&1 == sendCondition) {
if (null != sendCondition && 1 == sendCondition) {
if (taskCount[0] == taskCount[1]) {
isSend = true;
}
}
// 用例部分失败了发送
if (null!=sendCondition&&2 == sendCondition) {
if (null != sendCondition && 2 == sendCondition) {
if (taskCount[2] > 0) {
isSend = true;
}
}
// 全发
if (null!=sendCondition&&0 == sendCondition) {
if (null != sendCondition && 0 == sendCondition) {
isSend = true;
}
}
}
if (!isSend) {
LogUtil.APP.info("当前任务不需要发送邮件通知!");
LogUtil.APP.info("当前任务不需要发送邮件或是推送通知!");
return;
}
String[] addresses = serverOperation.getEmailAddress(taskid);
//向第三方推送消息
MailSendInitialization msi= new MailSendInitialization();
msi.pushMessage(taskScheduling, content, taskCount, time, buildStatus, restartStatus);
String[] addresses = serverOperation.getEmailAddress(taskScheduling,taskid);
Properties properties = SysConfig.getConfiguration();
if (addresses != null) {
LogUtil.APP.info("准备将测试结果发送邮件通知!请稍等...");
@ -77,13 +88,63 @@ public class MailSendInitialization {
}
String addressesmail = stringBuilder.toString();
if (sms.sendHtmlMail(mailInfo)) {
LogUtil.APP.info("给{}的测试结果通知邮件发送完成!",addressesmail);
LogUtil.APP.info("给{}的测试结果通知邮件发送完成!", addressesmail);
} else {
LogUtil.APP.warn("给{}的测试结果通知邮件发送失败!",addressesmail);
LogUtil.APP.warn("给{}的测试结果通知邮件发送失败!", addressesmail);
}
} else {
LogUtil.APP.info("当前任务不需要发送邮件通知!");
}
}
private void pushMessage(TaskScheduling taskScheduling, String content, int[] taskCount, String time, String buildStatus, String restartStatus) {
try {
Map<String, String> headmsg = new HashMap<>(0);
Properties properties = SysConfig.getConfiguration();
LogUtil.APP.info("准备初始化第三方消息推送的数据...");
String pushUrl = taskScheduling.getPushUrl();
if(StrUtil.isNotBlank(pushUrl)){
String ip = properties.getProperty("server.web.ip");
String port = properties.getProperty("server.web.port");
ProjectProtocolTemplate ppt = new ProjectProtocolTemplate();
ppt.setEncoding("utf-8");
ppt.setTimeout(60);
ppt.setIsResponseHead(1);
ppt.setIsResponseCode(1);
HttpClientTools hct = new HttpClientTools();
Map<String, Object> parameters = new HashMap<>(0);
if(null != taskCount){
content = "LuckyFrame自动化测试任务【" + taskScheduling.getSchedulingName() + "】执行结果\n" +
"自动构建状态:【" + buildStatus + "\n" +
"自动重启TOMCAT状态" + restartStatus + "\n" +
"本次任务预期执行用例共【" + taskCount[0] + "】条,耗時【" + time + "\n" +
"用例执行成功:【" + taskCount[1] + "\n" +
"用例执行失败:【" + taskCount[2] + "\n" +
"用例有可能由于脚本原因未成功解析被锁定:【" + taskCount[3] + "\n" +
"用例由于长时间未收到接口Response未执行完成" + taskCount[4] + "\n" +
"详情请前往自动化测试平台查看http://" + ip + ":" + port;
}
JSONObject contentJson = JSON.parseObject("{\"content\": \"" + content + "\"}");
JSONObject atJson = JSON.parseObject("{\"atMobiles\": [],\"isAtAll\":true}");
parameters.put("msgtype", "text");
parameters.put("text", contentJson);
parameters.put("at", atJson);
LogUtil.APP.info("开始向第三方平台推送任务执行情况...");
hct.httpClientPostJson(pushUrl, parameters, headmsg, ppt);
LogUtil.APP.info("向第三方平台推送任务执行数据成功...");
}else{
LogUtil.APP.warn("推送地址配置为空,取消第三方推送...");
}
} catch (Exception e) {
LogUtil.APP.error("向第三方平台推送任务执行情况出现异常,请检查!", e);
}
}
}

View File

@ -1,5 +1,6 @@
package luckyclient.utils.httputils;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import luckyclient.remote.entity.ProjectProtocolTemplate;
@ -1745,7 +1746,7 @@ public class HttpClientTools {
} else if (urlParam.startsWith("https://")) {
//采用绕过验证的方式处理https请求
SSLContext sslContext;
if (null == cerpath || "".equals(cerpath.trim())) {
if (StrUtil.isBlank(cerpath)) {
LogUtil.APP.info("开始构建HTTPS单向认证请求...");
TrustManager[] trustManagers = {new MyX509TrustManager()};
sslContext = SSLContext.getInstance("TLS");