用例提取为全局变量可引用

This commit is contained in:
liangdaliang
2025-03-20 18:13:18 +08:00
parent b07edf584a
commit 2addbd2179
7 changed files with 79 additions and 7 deletions

View File

@@ -6,6 +6,8 @@ import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import java.util.Map;
/**
* 用例对象 test_case
*
@@ -47,4 +49,7 @@ public class TestCase extends BaseEntity
/** 用例会话id */
private String caseSid;
/** 存储用例上下文提取结果 */
private Map<String, String> contextResultMap;
}

View File

@@ -141,6 +141,8 @@ public class TestCaseServiceImpl implements ITestCaseService
log.error("用例计划已被删除,不能执行!");
return null;
}
Map<String, String> contextResultMap = new HashMap<>();
testCase.setContextResultMap(contextResultMap);
TestCaseLog testCaseLog = new TestCaseLog();
testCaseLog.setCaseId(id);
testCaseLog.setCreateTime(DateUtils.getNowDate());
@@ -219,10 +221,12 @@ public class TestCaseServiceImpl implements ITestCaseService
testCaseResult.setResponseBody(resultMap.get("responseBody"));
// 处理参数提取
Map<String, String> assignmentResultMap = new HashMap<>();
assignmentResultMap.putAll(testCase.getContextResultMap());
String assignmentResult = this.dealTestCaseStepAssignment(resultMap, assignmentResultMap, assignment);
testCaseResult.setAssignment(assignmentResult);
// 根据提取结果处理校验规则
String assertionResult = this.dealTestCaseStepAssertion(assignmentResultMap, assertion);
testCase.getContextResultMap().putAll(assignmentResultMap);
testCaseResult.setAssertion(assertionResult);
Long useTime = null;
if (resultMap.get("costMiliseconds") != null) {
@@ -295,10 +299,12 @@ public class TestCaseServiceImpl implements ITestCaseService
testCaseResult.setSqlResult(gson.toJson(sqlResult));
// 处理参数提取
Map<String, String> assignmentResultMap = new HashMap<>();
assignmentResultMap.putAll(testCase.getContextResultMap());
String assignmentResult = this.dealDataSourceTestCaseStepAssignment(resultMapList, assignmentResultMap, sqlSpecialResultMap, assignment);
testCaseResult.setAssignment(assignmentResult);
// 根据提取结果处理校验规则
String assertionResult = this.dealTestCaseStepAssertion(assignmentResultMap, assertion);
testCase.getContextResultMap().putAll(assignmentResultMap);
testCaseResult.setAssertion(assertionResult);
if ("fail".equals(assignmentResultMap.get("assertionResult"))) {
testCaseResult.setStatus("失败");
@@ -510,6 +516,8 @@ public class TestCaseServiceImpl implements ITestCaseService
ruleResult.setValue(value);
} else if ("${RESPONSE_CODE}".equals(content)) {
ruleResult.setValue(responseCode);
} else if (content.startsWith("${")) {
ruleResult.setValue(assignmentResultMap.get(StringUtils.substringBetween(content, "${", "}")));
}
assignmentResultMap.put(rule.getName(), ruleResult.getValue());
ruleResultList.add(ruleResult);
@@ -564,6 +572,8 @@ public class TestCaseServiceImpl implements ITestCaseService
} else {
ruleResult.setValue(affectRows);
}
} else if (content.startsWith("${")) {
ruleResult.setValue(assignmentResultMap.get(StringUtils.substringBetween(content, "${", "}")));
}
assignmentResultMap.put(rule.getName(), ruleResult.getValue());
ruleResultList.add(ruleResult);

View File

@@ -213,12 +213,19 @@ export default {
},
hosts: {
type: Array
},
quoteList: {
type: Array
}
},
created() {
// 在这里进行数据预处理
this.preProcessFormData();
},
mounted() {
// 在组件挂载后调用方法
this.appendNamesToExtractObject();
},
data() {
return {
activeName: "Headers",
@@ -260,6 +267,24 @@ export default {
}
},
methods: {
appendNamesToExtractObject() {
if (this.quoteList) {
this.quoteList.forEach(item => {
item.assignment.forEach(item2 => {
if (item2.name && !this.isDuplicate(item2.name)) {
this.extract_object.push({
value: '${' + item2.name + '}',
label: '${' + item2.name + '}'
});
}
});
});
}
},
isDuplicate(name) {
let compareName = '${' + name + '}';
return this.extract_object.some(obj => obj.value === compareName);
},
handleLogChange(val) {
const selectedItem = this.form.testCaseResultList.find(item => item.title === val);
if (selectedItem) {

View File

@@ -111,8 +111,15 @@ export default {
},
datasourceList: {
type: Array
},
quoteList: {
type: Array
}
},
mounted() {
// 在组件挂载后调用方法
this.appendNamesToExtractObject();
},
data() {
return {
activeName: "sql",
@@ -153,6 +160,24 @@ export default {
}
},
methods: {
appendNamesToExtractObject() {
if (this.quoteList) {
this.quoteList.forEach(item => {
item.assignment.forEach(item2 => {
if (item2.name && !this.isDuplicate(item2.name)) {
this.extract_object.push({
value: '${' + item2.name + '}',
label: '${' + item2.name + '}'
});
}
});
});
}
},
isDuplicate(name) {
let compareName = '${' + name + '}';
return this.extract_object.some(obj => obj.value === compareName);
},
handleLogChange(val) {
const selectedItem = this.form.testCaseResultList.find(item => item.title === val);
if (selectedItem) {

View File

@@ -28,8 +28,8 @@
<el-tag v-if="item.requestUrl" effect="plain" style="text-align: center;margin-right: 12px;" type="success">{{ item.requestUrl }}</el-tag>
</template>
<el-button size="mini" type="text" @click="handleDel(index)" style="float: right">删除</el-button>
<page1 v-if="item.type == 1" :form="item" :hosts="hosts"/>
<page2 v-if="item.type == 2" :form="item" :datasourceList="datasourceList"/>
<page1 v-if="item.type == 1" :form="item" :hosts="hosts" :quoteList="quoteList" />
<page2 v-if="item.type == 2" :form="item" :datasourceList="datasourceList" :quoteList="quoteList" />
</el-collapse-item>
</el-collapse>
</div>
@@ -39,10 +39,11 @@
<script>
import page1 from "@/views/test/case/detail/page1.vue";
import page2 from "@/views/test/case/detail/page2.vue";
import page4 from "./page4.vue";
export default {
dicts: ['step_type'],
components: {page1, page2},
components: {page4, page1, page2},
props: {
form: {
type: Object
@@ -56,6 +57,9 @@ export default {
list: {
type: Array
},
quoteList: {
type: Array
}
},
data() {
return {

View File

@@ -59,6 +59,9 @@ export default {
list: {
type: Array
},
quoteList: {
type: Array
}
},
data() {
return {

View File

@@ -27,10 +27,10 @@
<el-tag effect="plain" style="text-align: center;margin-right: 12px;" class="drag-handle">{{ index + 1 }}</el-tag>
</template>
<el-button size="mini" type="text" @click="handleDel(index)" style="float: right">删除</el-button>
<page1 v-if="item.type == 1" :form="item" :hosts="hosts"/>
<page2 v-if="item.type == 2" :form="item" :datasourceList="datasourceList"/>
<page3 v-if="item.type == 3" :form="item" :list="item.childrenList" :hosts="hosts" :datasourceList="datasourceList"/>
<page4 v-if="item.type == 4" :form="item" :list="item.childrenList" :hosts="hosts" :datasourceList="datasourceList"/>
<page1 v-if="item.type == 1" :form="item" :hosts="hosts" :quoteList="list" />
<page2 v-if="item.type == 2" :form="item" :datasourceList="datasourceList" :quoteList="list" />
<page3 v-if="item.type == 3" :form="item" :list="item.childrenList" :hosts="hosts" :datasourceList="datasourceList" :quoteList="list" />
<page4 v-if="item.type == 4" :form="item" :list="item.childrenList" :hosts="hosts" :datasourceList="datasourceList" :quoteList="list" />
</el-collapse-item>
</draggable>
</el-collapse>