fix(接口测试): 修复提取变量相关问题

This commit is contained in:
fit2-zhao 2020-09-27 19:42:13 +08:00
parent a934214100
commit 5f8c7ef45d
4 changed files with 32 additions and 4 deletions

View File

@ -42,7 +42,6 @@ public class JMeterService {
Object scriptWrapper = SaveService.loadElement(is);
HashTree testPlan = getHashTree(scriptWrapper);
addBackendListener(testId, debugReportId, testPlan);
LocalRunner runner = new LocalRunner(testPlan);
runner.run();
} catch (Exception e) {

View File

@ -2,14 +2,24 @@ package io.metersphere.api.jmeter;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.threads.JMeterVariables;
import org.springframework.util.StringUtils;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class JMeterVars {
public static Map<String, JMeterVariables> variables = new HashMap<>();
public static void addVars(String testId, JMeterVariables vars) {
variables.put(testId, vars);
public static void addVars(String 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 -> {
vs.put(item, vars.get(item));
});
}
variables.put(testId, vs);
}
}

@ -1 +1 @@
Subproject commit cf6b06526324326a563d933e07118fac014a63b4
Subproject commit c2dacf960cdb1ed35664bdd3432120b1203b73d8

View File

@ -1208,6 +1208,25 @@ 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(org.apache.jmeter.threads.JMeterContextService.getContext().getThread().getThreadName(),vars," + "\"" + vars.join(";") + "\"" + ");"
sampler.put(new JSR223PostProcessor(name, request.jsr223PostProcessor));
}
}