feat(接口测试): 增加场景步骤引用设置功能允许开启禁用原场景变量

This commit is contained in:
fit2-zhao 2022-02-16 18:37:23 +08:00 committed by fit2-zhao
parent 7af7a7580b
commit 3d9d9dffe8
8 changed files with 113 additions and 74 deletions

View File

@ -74,6 +74,9 @@ public class MsScenario extends MsTestElement {
@JSONField(ordinal = 29)
private boolean environmentEnable;
@JSONField(ordinal = 30)
private Boolean variableEnable;
private static final String BODY_FILE_DIR = FileUtils.BODY_FILE_DIR;
public MsScenario() {
@ -136,22 +139,24 @@ public class MsScenario extends MsTestElement {
if (config != null && !config.getExcludeScenarioIds().contains(this.getId())) {
scenarioTree = MsCriticalSectionController.createHashTree(tree, this.getName(), this.isEnable());
}
// 场景变量和环境变量
// 环境变量
Arguments arguments = arguments(this.isEnvironmentEnable() ? newConfig : config);
if (arguments != null) {
if (arguments != null && (this.variableEnable == null || this.variableEnable)) {
Arguments valueSupposeMock = ParameterConfig.valueSupposeMock(arguments);
// 这里加入自定义变量解决ForEach循环控制器取值问题循环控制器无法从vars中取值
scenarioTree.add(valueSupposeMock);
scenarioTree.add(ElementUtil.argumentsToProcessor(valueSupposeMock));
}
ElementUtil.addCsvDataSet(scenarioTree, variables, this.isEnvironmentEnable() ? newConfig : config, "shareMode.group");
ElementUtil.addCounter(scenarioTree, variables, false);
ElementUtil.addRandom(scenarioTree, variables);
if (CollectionUtils.isNotEmpty(this.headers)) {
if (this.isEnvironmentEnable()) {
newConfig.setHeaders(this.headers);
} else {
config.setHeaders(this.headers);
if (this.variableEnable == null || this.variableEnable) {
ElementUtil.addCsvDataSet(scenarioTree, variables, this.isEnvironmentEnable() ? newConfig : config, "shareMode.group");
ElementUtil.addCounter(scenarioTree, variables, false);
ElementUtil.addRandom(scenarioTree, variables);
if (CollectionUtils.isNotEmpty(this.headers)) {
if (this.isEnvironmentEnable()) {
newConfig.setHeaders(this.headers);
} else {
config.setHeaders(this.headers);
}
}
}
// 添加全局前置
@ -175,7 +180,7 @@ public class MsScenario extends MsTestElement {
}
private void setGlobProcessor(ParameterConfig config, HashTree scenarioTree, boolean isPre) {
if (config.getConfig() != null) {
if (config.getConfig() != null && (this.variableEnable == null || this.variableEnable)) {
config.getConfig().forEach((k, environmentConfig) -> {
if (environmentConfig != null) {
MsJSR223Processor processor = isPre ? environmentConfig.getPreStepProcessor() : environmentConfig.getPostStepProcessor();
@ -211,14 +216,14 @@ public class MsScenario extends MsTestElement {
LinkedList<MsTestElement> sourceHashTree = mapper.readValue(element.getString("hashTree"), new TypeReference<LinkedList<MsTestElement>>() {
});
// 场景变量
if (StringUtils.isNotEmpty(element.getString("variables"))) {
if (StringUtils.isNotEmpty(element.getString("variables")) && (this.variableEnable == null || this.variableEnable)) {
LinkedList<ScenarioVariable> variables = mapper.readValue(element.getString("variables"),
new TypeReference<LinkedList<ScenarioVariable>>() {
});
this.setVariables(variables);
}
// 场景请求头
if (StringUtils.isNotEmpty(element.getString("headers"))) {
if (StringUtils.isNotEmpty(element.getString("headers")) && (this.variableEnable == null || this.variableEnable)) {
LinkedList<KeyValue> headers = mapper.readValue(element.getString("headers"),
new TypeReference<LinkedList<KeyValue>>() {
});

View File

@ -7,6 +7,6 @@ public class MsWhileController {
private String variable;
private String operator;
private String value;
private int timeout;
private long timeout;
private Object requestResult;
}

View File

@ -223,6 +223,9 @@ public class ApiScenarioEnvService {
* @param apiScenarioWithBLOBs
*/
public void setScenarioEnv(ApiScenarioWithBLOBs apiScenarioWithBLOBs, RunScenarioRequest request) {
if (apiScenarioWithBLOBs == null) {
return;
}
String environmentType = apiScenarioWithBLOBs.getEnvironmentType();
String environmentJson = apiScenarioWithBLOBs.getEnvironmentJson();
String environmentGroupId = apiScenarioWithBLOBs.getEnvironmentGroupId();
@ -245,68 +248,70 @@ public class ApiScenarioEnvService {
}
public boolean checkScenarioEnv(ApiScenarioWithBLOBs apiScenarioWithBLOBs, TestPlanApiScenario testPlanApiScenarios) {
String definition = apiScenarioWithBLOBs.getScenarioDefinition();
MsScenario scenario = JSONObject.parseObject(definition, MsScenario.class);
boolean isEnv = true;
Map<String, String> envMap = scenario.getEnvironmentMap();
if (testPlanApiScenarios != null) {
String envType = testPlanApiScenarios.getEnvironmentType();
String envJson = testPlanApiScenarios.getEnvironment();
String envGroupId = testPlanApiScenarios.getEnvironmentGroupId();
if (StringUtils.equals(envType, EnvironmentType.JSON.toString())
&& StringUtils.isNotBlank(envJson)) {
envMap = JSON.parseObject(testPlanApiScenarios.getEnvironment(), Map.class);
} else if (StringUtils.equals(envType, EnvironmentType.GROUP.name())
&& StringUtils.isNotBlank(envGroupId)) {
envMap = environmentGroupProjectService.getEnvMap(envGroupId);
} else {
envMap = new HashMap<>();
}
}
ScenarioEnv apiScenarioEnv = getApiScenarioEnv(definition);
// 所有请求非全路径检查环境
if (!apiScenarioEnv.getFullUrl()) {
try {
if (envMap == null || envMap.isEmpty()) {
isEnv = false;
if (apiScenarioWithBLOBs != null) {
String definition = apiScenarioWithBLOBs.getScenarioDefinition();
MsScenario scenario = JSONObject.parseObject(definition, MsScenario.class);
Map<String, String> envMap = scenario.getEnvironmentMap();
if (testPlanApiScenarios != null) {
String envType = testPlanApiScenarios.getEnvironmentType();
String envJson = testPlanApiScenarios.getEnvironment();
String envGroupId = testPlanApiScenarios.getEnvironmentGroupId();
if (StringUtils.equals(envType, EnvironmentType.JSON.toString())
&& StringUtils.isNotBlank(envJson)) {
envMap = JSON.parseObject(testPlanApiScenarios.getEnvironment(), Map.class);
} else if (StringUtils.equals(envType, EnvironmentType.GROUP.name())
&& StringUtils.isNotBlank(envGroupId)) {
envMap = environmentGroupProjectService.getEnvMap(envGroupId);
} else {
Set<String> projectIds = apiScenarioEnv.getProjectIds();
projectIds.remove(null);
if (CollectionUtils.isNotEmpty(envMap.keySet())) {
for (String id : projectIds) {
Project project = projectMapper.selectByPrimaryKey(id);
if (project == null) {
id = apiScenarioWithBLOBs.getProjectId();
}
String s = envMap.get(id);
if (StringUtils.isBlank(s)) {
isEnv = false;
break;
} else {
ApiTestEnvironmentWithBLOBs env = apiTestEnvironmentMapper.selectByPrimaryKey(s);
if (env == null) {
envMap = new HashMap<>();
}
}
ScenarioEnv apiScenarioEnv = getApiScenarioEnv(definition);
// 所有请求非全路径检查环境
if (!apiScenarioEnv.getFullUrl()) {
try {
if (envMap == null || envMap.isEmpty()) {
isEnv = false;
} else {
Set<String> projectIds = apiScenarioEnv.getProjectIds();
projectIds.remove(null);
if (CollectionUtils.isNotEmpty(envMap.keySet())) {
for (String id : projectIds) {
Project project = projectMapper.selectByPrimaryKey(id);
if (project == null) {
id = apiScenarioWithBLOBs.getProjectId();
}
String s = envMap.get(id);
if (StringUtils.isBlank(s)) {
isEnv = false;
break;
} else {
ApiTestEnvironmentWithBLOBs env = apiTestEnvironmentMapper.selectByPrimaryKey(s);
if (env == null) {
isEnv = false;
break;
}
}
}
} else {
isEnv = false;
}
} else {
isEnv = false;
}
} catch (Exception e) {
isEnv = false;
LogUtil.error(e.getMessage(), e);
}
} catch (Exception e) {
isEnv = false;
LogUtil.error(e.getMessage(), e);
}
}
// 1.8 之前环境是 environmentId
if (!isEnv) {
String envId = scenario.getEnvironmentId();
if (StringUtils.isNotBlank(envId)) {
ApiTestEnvironmentWithBLOBs env = apiTestEnvironmentMapper.selectByPrimaryKey(envId);
if (env != null) {
isEnv = true;
// 1.8 之前环境是 environmentId
if (!isEnv) {
String envId = scenario.getEnvironmentId();
if (StringUtils.isNotBlank(envId)) {
ApiTestEnvironmentWithBLOBs env = apiTestEnvironmentMapper.selectByPrimaryKey(envId);
if (env != null) {
isEnv = true;
}
}
}
}

View File

@ -48,6 +48,7 @@ public class MsHashTreeService {
private static final String ENABLE = "enable";
private static final String NUM = "num";
private static final String ENV_ENABLE = "environmentEnable";
private static final String VARIABLE_ENABLE = "variableEnable";
private static final String DISABLED = "disabled";
private static final String VERSION_NAME = "versionName";
private static final String VERSION_ENABLE = "versionEnable";
@ -217,10 +218,16 @@ public class MsHashTreeService {
private JSONObject setRefScenario(JSONObject element) {
boolean enable = element.containsKey(ENABLE) ? element.getBoolean(ENABLE) : true;
if (!element.containsKey(VARIABLE_ENABLE)) {
element.put(VARIABLE_ENABLE, true);
}
ApiScenarioDTO scenarioWithBLOBs = extApiScenarioMapper.selectById(element.getString(ID));
if (scenarioWithBLOBs != null && StringUtils.isNotEmpty(scenarioWithBLOBs.getScenarioDefinition())) {
boolean environmentEnable = element.containsKey(ENV_ENABLE)
? element.getBoolean(ENV_ENABLE) : false;
boolean variableEnable = element.containsKey(VARIABLE_ENABLE)
? element.getBoolean(VARIABLE_ENABLE) : true;
if (StringUtils.equalsIgnoreCase(element.getString(REFERENCED), REF)) {
element = JSON.parseObject(scenarioWithBLOBs.getScenarioDefinition());
@ -229,6 +236,9 @@ public class MsHashTreeService {
}
element.put(ID, scenarioWithBLOBs.getId());
element.put(ENV_ENABLE, environmentEnable);
if (!element.containsKey(VARIABLE_ENABLE)) {
element.put(VARIABLE_ENABLE, variableEnable);
}
this.setElement(element, scenarioWithBLOBs.getNum(), enable, scenarioWithBLOBs.getVersionName(), scenarioWithBLOBs.getVersionEnable());
} else {
if (StringUtils.equalsIgnoreCase(element.getString(REFERENCED), REF)) {

View File

@ -43,11 +43,24 @@
</span>
</template>
<template v-slot:scenarioEnable v-if="!ifFromVariableAdvance">
<el-tooltip :content="$t('commons.enable_scene_info')" placement="top">
<el-checkbox v-model="scenario.environmentEnable" @change="checkEnv" :disabled="scenario.disabled">
{{ $t('commons.enable_scene') }}
</el-checkbox>
</el-tooltip>
<el-popover
placement="bottom"
width="200"
trigger="click">
<ul>
<el-tooltip :content="$t('commons.enable_scene_info')" placement="top">-->
<el-checkbox v-model="scenario.environmentEnable" @change="checkEnv" :disabled="scenario.disabled">
{{ $t('commons.enable_scene') }}
</el-checkbox>
</el-tooltip>
<el-checkbox v-model="scenario.variableEnable" :disabled="scenario.disabled">
{{ $t('commons.variable_scene') }}
</el-checkbox>
</ul>
<span class="el-dropdown-link ms-test-running" slot="reference" @click.stop>
{{ $t('commons.reference_settings') }}
</span>
</el-popover>
</template>
<template v-slot:button v-if="!ifFromVariableAdvance">
<el-tooltip :content="$t('api_test.run')" placement="top" v-if="!scenario.run">
@ -90,7 +103,7 @@ export default {
type: Boolean,
default: true,
},
showVersion:{
showVersion: {
type: Boolean,
default: true,
},

View File

@ -224,7 +224,9 @@ export default {
execute_history: "Execute history",
debug_history: "Debug history",
testing: "Testing",
enable_scene: "Enable scene environment",
enable_scene: "Original scene environment",
variable_scene: "Original scene variable",
reference_settings: "Reference settings",
enable_scene_info: "Enable scene environment: the current step uses the original environment configuration of the scene to run",
edit_info: "Edit details",
environment: "Environment",

View File

@ -226,7 +226,9 @@ export default {
debug_history: "调试历史",
testing: "测试中",
edit_info: "编辑详情",
enable_scene: "启用场景环境",
enable_scene: "原场景环境",
variable_scene: "原场景变量",
reference_settings: "引用设置",
enable_scene_info: "启用场景环境:当前步骤使用场景原始环境配置运行",
environment: "运行环境",
run_success: "执行成功",

View File

@ -226,7 +226,9 @@ export default {
debug_history: "調試歷史",
testing: "測試中",
edit_info: "編輯詳情",
enable_scene: "啟用場景環境",
enable_scene: "原場景環境",
variable_scene: "原場景变量",
reference_settings: "引用设置",
enable_scene_info: "啟用場景環境:當前步驟使用場景原始環境配置運行",
environment: "運行環境",
run_success: "執行成功",