fix(接口测试): 修复循环控制器下的脚本执行取值问题

--bug=1022319 --user=赵勇 【接口测试】github#21478,次数循环控制器,python脚本不生效 https://www.tapd.cn/55049933/s/1332321
This commit is contained in:
fit2-zhao 2023-02-07 13:37:29 +08:00
parent bc608e60b7
commit da92c1416e
4 changed files with 24 additions and 52 deletions

View File

@ -52,6 +52,7 @@ import java.io.File;
import java.net.URL;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class ElementUtil {
private static final String PRE = "PRE";
@ -453,61 +454,31 @@ public class ElementUtil {
public static void mergeHashTree(MsTestElement element, LinkedList<MsTestElement> targetHashTree) {
try {
LinkedList<MsTestElement> sourceHashTree = element.getHashTree();
if (CollectionUtils.isNotEmpty(sourceHashTree) && CollectionUtils.isNotEmpty(targetHashTree) && sourceHashTree.size() < targetHashTree.size()) {
if (CollectionUtils.isNotEmpty(element.getHashTree())
&& CollectionUtils.isNotEmpty(targetHashTree)
&& element.getHashTree().size() == targetHashTree.size()) {
element.setHashTree(targetHashTree);
return;
}
List<String> sourceIds = new ArrayList<>();
List<String> delIds = new ArrayList<>();
Map<String, MsTestElement> updateMap = new HashMap<>();
if (CollectionUtils.isEmpty(sourceHashTree)) {
if (CollectionUtils.isNotEmpty(targetHashTree)) {
element.setHashTree(targetHashTree);
}
return;
}
if (CollectionUtils.isNotEmpty(targetHashTree)) {
for (MsTestElement item : targetHashTree) {
if (StringUtils.isNotEmpty(item.getId())) {
updateMap.put(item.getId(), item);
}
}
}
// 找出待更新内容和源已经被删除的内容
if (CollectionUtils.isNotEmpty(sourceHashTree)) {
for (int i = 0; i < sourceHashTree.size(); i++) {
MsTestElement source = sourceHashTree.get(i);
if (source != null) {
sourceIds.add(source.getId());
if (!StringUtils.equals(source.getLabel(), "SCENARIO-REF-STEP") && StringUtils.isNotEmpty(source.getId())) {
if (updateMap.containsKey(source.getId())) {
sourceHashTree.set(i, updateMap.get(source.getId()));
} else {
delIds.add(source.getId());
}
}
// 历史数据兼容
if (StringUtils.isEmpty(source.getId()) && !StringUtils.equals(source.getLabel(), "SCENARIO-REF-STEP") && i < targetHashTree.size()) {
sourceHashTree.set(i, targetHashTree.get(i));
}
}
}
}
// 合并步骤
List<MsTestElement> sourceList = Stream.of(element.getHashTree(), targetHashTree)
.flatMap(Collection::stream)
.distinct()
.collect(Collectors.toList());
// 删除多余的步骤
sourceHashTree.removeIf(item -> item != null && delIds.contains(item.getId()));
// 补充新增的源引用步骤
if (CollectionUtils.isNotEmpty(targetHashTree)) {
for (MsTestElement item : targetHashTree) {
if (!sourceIds.contains(item.getId())) {
sourceHashTree.add(item);
}
// 历史数据补充id
sourceList.forEach(item -> {
if (StringUtils.isBlank(item.getId())) {
item.setId(UUID.randomUUID().toString());
}
}
if (CollectionUtils.isNotEmpty(sourceHashTree)) {
element.setHashTree(sourceHashTree);
}
});
sourceList = sourceList.stream().collect(Collectors
.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(MsTestElement::getId))),
ArrayList::new));
element.setHashTree((LinkedList<MsTestElement>) sourceList);
} catch (Exception e) {
element.setHashTree(targetHashTree);
}

View File

@ -80,6 +80,7 @@ public class MsJSR223Processor extends MsTestElement {
ElementUtil.setBaseParams(processor, this.getParent(), config, resourceId, this.getIndex());
processor.setProperty(TestElement.TEST_CLASS, JSR223Sampler.class.getName());
processor.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("TestBeanGUI"));
processor.setProperty("cacheKey", false);
processor.setProperty("scriptLanguage", this.getScriptLanguage());
if (StringUtils.isNotEmpty(this.getScriptLanguage()) && this.getScriptLanguage().equals("nashornScript")) {
processor.setProperty("scriptLanguage", "nashorn");

View File

@ -75,7 +75,7 @@ public class MsJSR223PostProcessor extends MsTestElement {
}
processor.setProperty(TestElement.TEST_CLASS, JSR223PostProcessor.class.getName());
processor.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("TestBeanGUI"));
/*processor.setProperty("cacheKey", "true");*/
processor.setProperty("cacheKey", false);
processor.setProperty("scriptLanguage", this.getScriptLanguage());
if (StringUtils.isNotEmpty(this.getScriptLanguage()) && this.getScriptLanguage().equals("nashornScript")) {
processor.setProperty("scriptLanguage", "nashorn");

View File

@ -88,7 +88,7 @@ public class MsJSR223PreProcessor extends MsTestElement {
}
processor.setProperty(TestElement.TEST_CLASS, JSR223PreProcessor.class.getName());
processor.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("TestBeanGUI"));
/*processor.setProperty("cacheKey", "true");*/
processor.setProperty("cacheKey", false);
processor.setProperty("scriptLanguage", this.getScriptLanguage());
if (StringUtils.isNotEmpty(this.getScriptLanguage()) && this.getScriptLanguage().equals("nashornScript")) {
processor.setProperty("scriptLanguage", "nashorn");