fix(接口测试): 修复提取变量相关问题
This commit is contained in:
parent
a934214100
commit
5f8c7ef45d
|
@ -42,7 +42,6 @@ public class JMeterService {
|
||||||
Object scriptWrapper = SaveService.loadElement(is);
|
Object scriptWrapper = SaveService.loadElement(is);
|
||||||
HashTree testPlan = getHashTree(scriptWrapper);
|
HashTree testPlan = getHashTree(scriptWrapper);
|
||||||
addBackendListener(testId, debugReportId, testPlan);
|
addBackendListener(testId, debugReportId, testPlan);
|
||||||
|
|
||||||
LocalRunner runner = new LocalRunner(testPlan);
|
LocalRunner runner = new LocalRunner(testPlan);
|
||||||
runner.run();
|
runner.run();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|
|
@ -2,14 +2,24 @@ package io.metersphere.api.jmeter;
|
||||||
|
|
||||||
import org.apache.jmeter.samplers.SampleResult;
|
import org.apache.jmeter.samplers.SampleResult;
|
||||||
import org.apache.jmeter.threads.JMeterVariables;
|
import org.apache.jmeter.threads.JMeterVariables;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public class JMeterVars {
|
public class JMeterVars {
|
||||||
public static Map<String, JMeterVariables> variables = new HashMap<>();
|
public static Map<String, JMeterVariables> variables = new HashMap<>();
|
||||||
|
|
||||||
public static void addVars(String testId, JMeterVariables vars) {
|
public static void addVars(String testId, JMeterVariables vars, String extract) {
|
||||||
variables.put(testId, vars);
|
JMeterVariables vs = new JMeterVariables();
|
||||||
|
if (!StringUtils.isEmpty(extract) && vars != null) {
|
||||||
|
List<String> extracts = Arrays.asList(extract.split(";"));
|
||||||
|
extracts.forEach(item -> {
|
||||||
|
vs.put(item, vars.get(item));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
variables.put(testId, vs);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
Subproject commit cf6b06526324326a563d933e07118fac014a63b4
|
Subproject commit c2dacf960cdb1ed35664bdd3432120b1203b73d8
|
|
@ -1208,6 +1208,25 @@ 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(org.apache.jmeter.threads.JMeterContextService.getContext().getThread().getThreadName(),vars," + "\"" + vars.join(";") + "\"" + ");"
|
||||||
|
|
||||||
sampler.put(new JSR223PostProcessor(name, request.jsr223PostProcessor));
|
sampler.put(new JSR223PostProcessor(name, request.jsr223PostProcessor));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue