refactor(接口测试): 优化场景可以启用禁用自身变量

--bug=1010558 --user=赵勇 【接口测试】github#10702,存在多个自定义请求与场景组合的接口自动化场景,自定义请求的提参在下个自定义请求中获取的不是最新的提参值,请定位并修改 https://www.tapd.cn/55049933/s/1108642
This commit is contained in:
fit2-zhao 2022-02-23 14:13:23 +08:00 committed by fit2-zhao
parent 0a4bab8a7f
commit 7a71cf1477
9 changed files with 94 additions and 84 deletions

View File

@ -145,7 +145,9 @@ public class MsScenario extends MsTestElement {
Arguments valueSupposeMock = ParameterConfig.valueSupposeMock(arguments);
// 这里加入自定义变量解决ForEach循环控制器取值问题循环控制器无法从vars中取值
scenarioTree.add(valueSupposeMock);
scenarioTree.add(ElementUtil.argumentsToProcessor(valueSupposeMock));
if (this.variableEnable != null && this.variableEnable) {
scenarioTree.add(ElementUtil.argumentsToProcessor(valueSupposeMock));
}
}
if (this.variableEnable == null || this.variableEnable) {
ElementUtil.addCsvDataSet(scenarioTree, variables, this.isEnvironmentEnable() ? newConfig : config, "shareMode.group");
@ -245,13 +247,17 @@ public class MsScenario extends MsTestElement {
ApiScenarioMapper apiScenarioMapper = CommonBeanFactory.getBean(ApiScenarioMapper.class);
EnvironmentGroupProjectService environmentGroupProjectService = CommonBeanFactory.getBean(EnvironmentGroupProjectService.class);
ApiScenarioWithBLOBs scenario = apiScenarioMapper.selectByPrimaryKey(this.getId());
String environmentType = scenario.getEnvironmentType();
String environmentJson = scenario.getEnvironmentJson();
String environmentGroupId = scenario.getEnvironmentGroupId();
if (StringUtils.equals(environmentType, EnvironmentType.GROUP.name())) {
this.environmentMap = environmentGroupProjectService.getEnvMap(environmentGroupId);
} else if (StringUtils.equals(environmentType, EnvironmentType.JSON.name())) {
this.environmentMap = JSON.parseObject(environmentJson, Map.class);
if (scenario != null) {
String environmentType = scenario.getEnvironmentType();
String environmentJson = scenario.getEnvironmentJson();
String environmentGroupId = scenario.getEnvironmentGroupId();
if (StringUtils.equals(environmentType, EnvironmentType.GROUP.name())) {
this.environmentMap = environmentGroupProjectService.getEnvMap(environmentGroupId);
} else if (StringUtils.equals(environmentType, EnvironmentType.JSON.name())) {
this.environmentMap = JSON.parseObject(environmentJson, Map.class);
}
} else {
this.setEnvironmentEnable(false);
}
if (this.environmentMap != null && !this.environmentMap.isEmpty()) {
@ -301,6 +307,15 @@ public class MsScenario extends MsTestElement {
arguments.setName(StringUtils.isNotEmpty(this.getName()) ? this.getName() : "Arguments");
arguments.setProperty(TestElement.TEST_CLASS, Arguments.class.getName());
arguments.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("ArgumentsPanel"));
// 环境通用变量
if (config.isEffective(this.getProjectId()) && config.getConfig().get(this.getProjectId()).getCommonConfig() != null
&& CollectionUtils.isNotEmpty(config.getConfig().get(this.getProjectId()).getCommonConfig().getVariables())) {
config.getConfig().get(this.getProjectId()).getCommonConfig().getVariables().stream().filter(KeyValue::isValid).filter(KeyValue::isEnable).forEach(keyValue ->
arguments.addArgument(keyValue.getName(), keyValue.getValue(), "=")
);
// 清空变量防止重复添加
config.getConfig().get(this.getProjectId()).getCommonConfig().getVariables().clear();
}
if (CollectionUtils.isNotEmpty(this.getVariables())) {
this.getVariables().stream().filter(ScenarioVariable::isConstantValid).forEach(keyValue ->
arguments.addArgument(keyValue.getName(), keyValue.getValue(), "=")
@ -314,15 +329,6 @@ public class MsScenario extends MsTestElement {
}
});
}
// 环境通用变量
if (config.isEffective(this.getProjectId()) && config.getConfig().get(this.getProjectId()).getCommonConfig() != null
&& CollectionUtils.isNotEmpty(config.getConfig().get(this.getProjectId()).getCommonConfig().getVariables())) {
config.getConfig().get(this.getProjectId()).getCommonConfig().getVariables().stream().filter(KeyValue::isValid).filter(KeyValue::isEnable).forEach(keyValue ->
arguments.addArgument(keyValue.getName(), keyValue.getValue(), "=")
);
// 清空变量防止重复添加
config.getConfig().get(this.getProjectId()).getCommonConfig().getVariables().clear();
}
if (arguments.getArguments() != null && arguments.getArguments().size() > 0) {
return arguments;
}

View File

@ -5,7 +5,7 @@ import lombok.Data;
@Data
public class CountController {
private int loops;
private int interval;
private long interval;
private boolean proceed;
private Object requestResult;

View File

@ -475,7 +475,7 @@ public class MsHTTPSamplerProxy extends MsTestElement {
if (StringUtils.isNotEmpty(this.getPort()) && this.getPort().startsWith("${")) {
url = url.replace(this.getPort(), "10990");
}
if (url == null) {
if (StringUtils.isEmpty(url)) {
MSException.throwException("请重新选择环境");
}
URL urlObject = new URL(url);

View File

@ -322,6 +322,7 @@ public class ApiScenarioExecuteService {
hashTree = request.getTestElement().generateHashTree(config);
LogUtil.info(request.getTestElement().getJmx(hashTree));
} catch (Exception e) {
LoggerUtil.error(e);
MSException.throwException(e.getMessage());
}
if (request.isSaved()) {

View File

@ -462,22 +462,24 @@ export default {
},
handleExport() {
if (this.report.reportVersion && this.report.reportVersion > 1) {
if(this.report.reportType === 'API_INTEGRATED'){
if (this.report.reportType === 'API_INTEGRATED') {
let scenario = {name: "", requestResults: []};
this.content.scenarios = [scenario];
this.formatExportApi(this.fullTreeNodes, scenario);
}else {
this.fullTreeNodes.forEach(item => {
if (item.type === "scenario") {
let scenario = {name: item.label, requestResults: []};
if (this.content.scenarios && this.content.scenarios.length > 0) {
this.content.scenarios.push(scenario);
} else {
this.content.scenarios = [scenario];
} else {
if (this.fullTreeNodes) {
this.fullTreeNodes.forEach(item => {
if (item.type === "scenario") {
let scenario = {name: item.label, requestResults: []};
if (this.content.scenarios && this.content.scenarios.length > 0) {
this.content.scenarios.push(scenario);
} else {
this.content.scenarios = [scenario];
}
this.formatExportApi(item.children, scenario);
}
this.formatExportApi(item.children, scenario);
}
})
})
}
}
}
this.reportExportVisible = true;

View File

@ -362,7 +362,7 @@
:old-on-sample-error="onSampleError"
:new-on-sample-error="newOnSampleError"
:project-list="projectList"
:type ="type"
:type="type"
></scenario-diff>
</el-dialog>
@ -522,10 +522,10 @@ export default {
executeType: "",
versionData: [],
newData: [],
oldData:[],
oldData: [],
dialogVisible: false,
newScenarioDefinition: [],
oldScenarioDefinition:[],
oldScenarioDefinition: [],
currentItem: {},
pluginDelStep: false
}
@ -1109,6 +1109,7 @@ export default {
}
this.resetResourceId(item.hashTree);
item.enable === undefined ? item.enable = true : item.enable;
item.variableEnable = item.variableEnable === undefined ? true : item.variableEnable;
if (this.selectedTreeNode !== undefined) {
this.selectedTreeNode.hashTree.push(item);
} else {
@ -1420,12 +1421,12 @@ export default {
getApiScenario() {
this.loading = true;
this.stepEnable = true;
if (this.currentScenario.tags !== undefined && this.currentScenario.tags ) {
if(!(this.currentScenario.tags instanceof Array)){
if (this.currentScenario.tags !== undefined && this.currentScenario.tags) {
if (!(this.currentScenario.tags instanceof Array)) {
this.currentScenario.tags = JSON.parse(this.currentScenario.tags);
}
}else{
this.$set(this.currentScenario,'tags',[])
} else {
this.$set(this.currentScenario, 'tags', [])
}
if (!this.currentScenario.variables) {
this.currentScenario.variables = [];
@ -1766,64 +1767,64 @@ export default {
});
},
compare(row) {
this.$get('/api/automation/get/' + row.id+"/"+this.currentScenario.refId, response => {
this.$get("/api/automation/getApiScenario/" + response.data.id, res => {
if (res.data) {
if(res.data.scenarioDefinition != null){
let obj = JSON.parse(res.data.scenarioDefinition);
if(obj){
if(obj.hashTree){
for (let i = 0; i < obj.hashTree.length; i++) {
if(!obj.hashTree[i].index){
obj.hashTree[i].index = i+1;
}
obj.hashTree[i].disabled = true;
if (!obj.hashTree[i].requestResult) {
obj.hashTree[i].requestResult = [{responseResult: {}}];
}
this.$get('/api/automation/get/' + row.id + "/" + this.currentScenario.refId, response => {
this.$get("/api/automation/getApiScenario/" + response.data.id, res => {
if (res.data) {
if (res.data.scenarioDefinition != null) {
let obj = JSON.parse(res.data.scenarioDefinition);
if (obj) {
if (obj.hashTree) {
for (let i = 0; i < obj.hashTree.length; i++) {
if (!obj.hashTree[i].index) {
obj.hashTree[i].index = i + 1;
}
this.newEnableCookieShare = obj.enableCookieShare;
if (obj.onSampleError === undefined) {
this.newOnSampleError = true;
} else {
this.newOnSampleError = obj.onSampleError;
obj.hashTree[i].disabled = true;
if (!obj.hashTree[i].requestResult) {
obj.hashTree[i].requestResult = [{responseResult: {}}];
}
}
this.dataProcessing(obj.hashTree);
this.newScenarioDefinition = obj.hashTree;
for (let i = 0; i < this.oldScenarioDefinition.length; i++) {
this.oldScenarioDefinition[i].disabled = true;
}
if (response.data.environmentJson) {
this.newProjectEnvMap = objToStrMap(JSON.parse(response.data.environmentJson));
this.newEnableCookieShare = obj.enableCookieShare;
if (obj.onSampleError === undefined) {
this.newOnSampleError = true;
} else {
//
this.newProjectEnvMap.set(this.projectId, obj.environmentId);
this.newOnSampleError = obj.onSampleError;
}
}
this.dataProcessing(obj.hashTree);
this.newScenarioDefinition = obj.hashTree;
for (let i = 0; i < this.oldScenarioDefinition.length; i++) {
this.oldScenarioDefinition[i].disabled = true;
}
if (response.data.environmentJson) {
this.newProjectEnvMap = objToStrMap(JSON.parse(response.data.environmentJson));
} else {
//
this.newProjectEnvMap.set(this.projectId, obj.environmentId);
}
}
res.data.userName = response.data.userName
this.dealWithTag(res.data);
this.oldData = this.currentScenario;
this.newData = res.data;
this.closeExpansion()
}
this.sort();
this.dialogVisible = true;
});
res.data.userName = response.data.userName
this.dealWithTag(res.data);
this.oldData = this.currentScenario;
this.newData = res.data;
this.closeExpansion()
}
this.sort();
this.dialogVisible = true;
});
})
},
closeDiff(){
closeDiff() {
this.oldScenarioDefinition = []
},
dealWithTag(newScenario){
if(newScenario.tags){
if(Object.prototype.toString.call(newScenario.tags)==="[object String]"){
dealWithTag(newScenario) {
if (newScenario.tags) {
if (Object.prototype.toString.call(newScenario.tags) === "[object String]") {
newScenario.tags = JSON.parse(newScenario.tags);
}
}
if(this.currentScenario.tags){
if(Object.prototype.toString.call(this.currentScenario.tags)==="[object String]"){
if (this.currentScenario.tags) {
if (Object.prototype.toString.call(this.currentScenario.tags) === "[object String]") {
this.currentScenario.tags = JSON.parse(this.currentScenario.tags);
}
}

View File

@ -22,7 +22,7 @@
<el-row>
<el-col :span="8">
<span class="ms-span ms-radio">{{ $t('loop.loops') }}</span>
<el-input-number size="small" v-model="controller.countController.loops" :placeholder="$t('commons.millisecond')" :max="1000*10000000" :min="0"/>
<el-input-number size="small" v-model="controller.countController.loops" :placeholder="$t('commons.millisecond')" :max="100000000" :min="0"/>
<span class="ms-span ms-radio"></span>
</el-col>
<el-col :span="8">

View File

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

View File

@ -229,7 +229,7 @@ export default {
edit_info: "編輯詳情",
enable_scene: "原場景環境",
variable_scene: "原場景变量",
reference_settings: "引用设置",
reference_settings: "场景设置",
enable_scene_info: "啟用場景環境:當前步驟使用場景原始環境配置運行",
environment: "運行環境",
run_success: "執行成功",