refactor(接口测试): 重写提取方法
This commit is contained in:
parent
34a0f005f3
commit
abcd4b299b
|
@ -39,6 +39,7 @@ public class APIBackendListenerClient extends AbstractBackendListenerClient impl
|
||||||
|
|
||||||
public String runMode = ApiRunMode.RUN.name();
|
public String runMode = ApiRunMode.RUN.name();
|
||||||
|
|
||||||
|
private JMeterVars variables;
|
||||||
// 测试ID
|
// 测试ID
|
||||||
private String testId;
|
private String testId;
|
||||||
|
|
||||||
|
@ -154,10 +155,9 @@ public class APIBackendListenerClient extends AbstractBackendListenerClient impl
|
||||||
responseResult.setResponseTime(result.getTime());
|
responseResult.setResponseTime(result.getTime());
|
||||||
responseResult.setResponseMessage(result.getResponseMessage());
|
responseResult.setResponseMessage(result.getResponseMessage());
|
||||||
|
|
||||||
if (JMeterVars.variables != null && JMeterVars.variables.get(result.hashCode()) != null) {
|
if (variables.get(result.hashCode()) != null) {
|
||||||
JMeterVars.variables.get(result.hashCode()).remove("TESTSTART.MS"); //去除系统变量
|
|
||||||
List<String> vars = new LinkedList<>();
|
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());
|
first.add(second.getKey() + ":" + second.getValue());
|
||||||
return first;
|
return first;
|
||||||
}, (first, second) -> {
|
}, (first, second) -> {
|
||||||
|
@ -199,6 +199,7 @@ public class APIBackendListenerClient extends AbstractBackendListenerClient impl
|
||||||
if (StringUtils.isBlank(this.runMode)) {
|
if (StringUtils.isBlank(this.runMode)) {
|
||||||
this.runMode = ApiRunMode.RUN.name();
|
this.runMode = ApiRunMode.RUN.name();
|
||||||
}
|
}
|
||||||
|
variables = new JMeterVars();
|
||||||
}
|
}
|
||||||
|
|
||||||
private ResponseAssertionResult getResponseAssertionResult(AssertionResult assertionResult) {
|
private ResponseAssertionResult getResponseAssertionResult(AssertionResult assertionResult) {
|
||||||
|
|
|
@ -41,6 +41,7 @@ public class JMeterService {
|
||||||
try {
|
try {
|
||||||
Object scriptWrapper = SaveService.loadElement(is);
|
Object scriptWrapper = SaveService.loadElement(is);
|
||||||
HashTree testPlan = getHashTree(scriptWrapper);
|
HashTree testPlan = getHashTree(scriptWrapper);
|
||||||
|
JMeterVars.addJSR223PostProcessor(testPlan);
|
||||||
addBackendListener(testId, debugReportId, testPlan);
|
addBackendListener(testId, debugReportId, testPlan);
|
||||||
LocalRunner runner = new LocalRunner(testPlan);
|
LocalRunner runner = new LocalRunner(testPlan);
|
||||||
runner.run();
|
runner.run();
|
||||||
|
|
|
@ -1,25 +1,65 @@
|
||||||
package io.metersphere.api.jmeter;
|
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.jmeter.threads.JMeterVariables;
|
||||||
|
import org.apache.jorphan.collections.HashTree;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.*;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public class JMeterVars {
|
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) {
|
public static void addVars(Integer testId, JMeterVariables vars, String extract) {
|
||||||
JMeterVariables vs = new JMeterVariables();
|
JMeterVariables vs = new JMeterVariables();
|
||||||
if (!StringUtils.isEmpty(extract) && vars != null) {
|
if (!StringUtils.isEmpty(extract) && vars != null) {
|
||||||
List<String> extracts = Arrays.asList(extract.split(";"));
|
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.put(item, vars.get(item));
|
||||||
});
|
});
|
||||||
|
vs.remove("TESTSTART.MS"); // 标示变量移除
|
||||||
}
|
}
|
||||||
variables.put(testId, vs);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1208,25 +1208,6 @@ class JMXGenerator {
|
||||||
sampler.put(new JSR223PreProcessor(name, request.jsr223PreProcessor));
|
sampler.put(new JSR223PreProcessor(name, request.jsr223PreProcessor));
|
||||||
}
|
}
|
||||||
if (request.jsr223PostProcessor && request.jsr223PostProcessor.script) {
|
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));
|
sampler.put(new JSR223PostProcessor(name, request.jsr223PostProcessor));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue