yapi-plugin-pl-auto-test
Version:
YAPI自动化测试插件,支持在YAPI设置测试计划,历史测试结果存入ES,界面显示测试结果,自定义通知。
130 lines (117 loc) • 4.36 kB
JavaScript
const schedule = require('node-schedule');
const planModel = require('./models/plan');
const projectModel = require('../../server/models/project');
const groupModel = require('../../server/models/group');
const resultModel = require('./models/result');
const resultController = require('./controllers/result');
const yapi = require('yapi.js');
const axios = require('axios');
const jobMap = new Map();
const logType = "pl-auto-test";
/**
* 定时执行测试计划
*/
class testSchedule {
constructor(ctx) {
this.ctx = ctx;
this.planModel = yapi.getInst(planModel);
// this.scheduleCached = yapi.getInst(scheduleCached);
this.resultModel = yapi.getInst(resultModel);
this.projectModel = yapi.getInst(projectModel);
this.groupModel = yapi.getInst(groupModel);
this.resultController = yapi.getInst(resultController);
this.init();
}
// 初始化定时任务
async init () {
const allPlan = await this.planModel.listAll();
for (let i = 0, len = allPlan.length; i < len; i++) {
const plan = allPlan[i];
if (plan.is_plan_open) {
this.addTestJob(plan._id, plan);
}
}
}
/**
* 添加一个测试计划
* @param {测试计划id} planId
* @param {测试计划} plan
*/
async addTestJob (planId, plan) {
let url = plan.plan_url;
// 自动测试不支持下载
url = url.replace("download=true", "download=false");
url = url.replace("mode=html", "mode=json");
// 清空url中带的plan_id
url = url.replace(/&plan_id=\d+&/, "&")
// 替换API
url = url.replace("/api/open/run_auto_test", "/api/open/plugin/test/run");
url += `&plan_id=${planId}`;
const handlerPlan = async (planId, plan) => {
let projectInfo = {}
let groupInfo = {}
try {
projectInfo = await this.projectModel.get(plan.project_id);
groupInfo = await this.groupModel.get(projectInfo.group_id);
const urlEncode = encodeURI(url);
const result = await axios.get(urlEncode);
if (result.data.hasOwnProperty("errmsg")) {
this.saveTestLog(plan.plan_name, result.data.errmsg, plan.uid, plan.project_id);
yapi.commons.log(`group:${groupInfo.group_name},project:${projectInfo.name},planId:${planId},plan:${plan.plan_name},detail:${JSON.stringify(result.data)}`, logType);
if (result.data.errmsg == "collection id值不存在"){
yapi.commons.log(`collection id值不存在,即将删除测试计划,group:${groupInfo.group_name},project:${projectInfo.name},planId:${planId},plan:${plan.plan_name}`, logType);
// 内存删除任务
await this.deleteTestJob(id);
// db关闭计划
await this.planModel.del(planId)
}
} else {
if (plan.plan_result_size >= 0) {
const results = await this.resultModel.findByPlan(planId);
const ids = results.map((val) => val._id).slice(plan.plan_result_size);
await this.resultModel.deleteByIds(ids);
}
}
} catch (error) { // 增加project信息
yapi.commons.log(`planId:${planId},groupId:${projectInfo.group_id},${projectInfo.name},catch error:${error},plan:${JSON.stringify(plan)}`, logType);
}
};
const scheduleItem = schedule.scheduleJob(plan.plan_cron, async () => {
handlerPlan(planId, plan, jobItem);
});
// 判断是否已经存在这个任务
const jobItem = jobMap.get(planId);
if (jobItem) {
jobItem.cancel();
}
jobMap.set(planId, scheduleItem);
}
/**
* 获取测试计划
* @param {测试计划id} planId
*/
getTestJob (planId) {
return jobMap.get(planId);
}
/**
* 删除测试计划
* @param {测试计划id} planId
*/
deleteTestJob (planId) {
let jobItem = jobMap.get(planId);
if (jobItem) {
jobItem.cancel();
}
}
// 动态中添加测试结果
saveTestLog (plan, msg, uid, projectId) {
yapi.commons.saveLog({
content: `成功执行计划名为"${plan}"的自动化测试,${msg}。`,
type: 'project',
uid: uid,
username: "自动化测试",
typeid: projectId
});
}
}
module.exports = testSchedule;