refactor(接口测试): 重写提取方法

This commit is contained in:
fit2-zhao 2020-09-28 15:02:12 +08:00
parent 34a0f005f3
commit abcd4b299b
4 changed files with 52 additions and 29 deletions

View File

@ -39,6 +39,7 @@ public class APIBackendListenerClient extends AbstractBackendListenerClient impl
public String runMode = ApiRunMode.RUN.name();
private JMeterVars variables;
// 测试ID
private String testId;
@ -154,10 +155,9 @@ public class APIBackendListenerClient extends AbstractBackendListenerClient impl
responseResult.setResponseTime(result.getTime());
responseResult.setResponseMessage(result.getResponseMessage());
if (JMeterVars.variables != null && JMeterVars.variables.get(result.hashCode()) != null) {
JMeterVars.variables.get(result.hashCode()).remove("TESTSTART.MS"); //去除系统变量
if (variables.get(result.hashCode()) != null) {
List<String> vars = new LinkedList<>();
JMeterVars.variables.get(result.hashCode()).entrySet().parallelStream().reduce(vars, (first, second) -> {
variables.get(result.hashCode()).entrySet().parallelStream().reduce(vars, (first, second) -> {
first.add(second.getKey() + "" + second.getValue());
return first;
}, (first, second) -> {
@ -199,6 +199,7 @@ public class APIBackendListenerClient extends AbstractBackendListenerClient impl
if (StringUtils.isBlank(this.runMode)) {
this.runMode = ApiRunMode.RUN.name();
}
variables = new JMeterVars();
}
private ResponseAssertionResult getResponseAssertionResult(AssertionResult assertionResult) {

View File

@ -41,6 +41,7 @@ public class JMeterService {
try {
Object scriptWrapper = SaveService.loadElement(is);
HashTree testPlan = getHashTree(scriptWrapper);
JMeterVars.addJSR223PostProcessor(testPlan);
addBackendListener(testId, debugReportId, testPlan);
LocalRunner runner = new LocalRunner(testPlan);
runner.run();

View File

@ -1,25 +1,65 @@
package io.metersphere.api.jmeter;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.extractor.JSR223PostProcessor;
import org.apache.jmeter.extractor.RegexExtractor;
import org.apache.jmeter.extractor.XPath2Extractor;
import org.apache.jmeter.extractor.json.jsonpath.JSONPostProcessor;
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy;
import org.apache.jmeter.threads.JMeterVariables;
import org.apache.jorphan.collections.HashTree;
import org.springframework.util.StringUtils;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
public class JMeterVars {
public static Map<Integer, JMeterVariables> variables = new HashMap<>();
private static Map<Integer, JMeterVariables> variables = new HashMap<>();
// 线程执行过程调用提取变量值
public static void addVars(Integer testId, JMeterVariables vars, String extract) {
JMeterVariables vs = new JMeterVariables();
if (!StringUtils.isEmpty(extract) && vars != null) {
List<String> extracts = Arrays.asList(extract.split(";"));
extracts.forEach(item -> {
Optional.ofNullable(extracts).orElse(new ArrayList<>()).forEach(item -> {
vs.put(item, vars.get(item));
});
vs.remove("TESTSTART.MS"); // 标示变量移除
}
variables.put(testId, vs);
}
// 递归处理所有请求对有提取变量的增加后置脚本
public static void addJSR223PostProcessor(HashTree tree) {
for (Object key : tree.keySet()) {
HashTree node = tree.get(key);
if (key instanceof HTTPSamplerProxy) {
StringJoiner extract = new StringJoiner(";");
for (Object child : node.keySet()) {
if (child instanceof RegexExtractor) {
RegexExtractor regexExtractor = (RegexExtractor) child;
extract.add(regexExtractor.getRefName());
} else if (child instanceof XPath2Extractor) {
XPath2Extractor regexExtractor = (XPath2Extractor) child;
extract.add(regexExtractor.getRefName());
} else if (child instanceof JSONPostProcessor) {
JSONPostProcessor regexExtractor = (JSONPostProcessor) child;
extract.add(regexExtractor.getRefNames());
}
}
if (Optional.ofNullable(extract).orElse(extract).length() > 0) {
JSR223PostProcessor shell = new JSR223PostProcessor();
shell.setEnabled(true);
shell.setProperty("script", "io.metersphere.api.jmeter.JMeterVars.addVars(prev.hashCode(),vars," + "\"" + extract.toString() + "\"" + ");");
node.add(shell);
}
}
if (node != null) {
addJSR223PostProcessor(node);
}
}
}
public JMeterVariables get(Integer key) {
return variables.get(key);
}
}

View File

@ -1208,25 +1208,6 @@ class JMXGenerator {
sampler.put(new JSR223PreProcessor(name, request.jsr223PreProcessor));
}
if (request.jsr223PostProcessor && request.jsr223PostProcessor.script) {
// 增加一段后置脚步,获取 提取变量的内容
let vars = [];
if (request.extract.regex) {
for (let i = 0; i < request.extract.regex.length; i++) {
vars.push(request.extract.regex[i].variable);
}
}
if (request.extract.json) {
for (let i = 0; i < request.extract.json.length; i++) {
vars.push(request.extract.json[i].variable);
}
}
if (request.extract.xpath) {
for (let i = 0; i < request.extract.xpath.length; i++) {
vars.push(request.extract.xpath[i].variable);
}
}
request.jsr223PostProcessor.script += "\n" + "io.metersphere.api.jmeter.JMeterVars.addVars(prev.hashCode(),vars," + "\"" + vars.join(";") + "\"" + ");"
sampler.put(new JSR223PostProcessor(name, request.jsr223PostProcessor));
}
}