From 5f8c7ef45d9e73ef4c4af79dea1d7d03c333d414 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Sun, 27 Sep 2020 19:42:13 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=8F=90=E5=8F=96=E5=8F=98=E9=87=8F=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../metersphere/api/jmeter/JMeterService.java | 1 - .../io/metersphere/api/jmeter/JMeterVars.java | 14 ++++++++++++-- backend/src/main/java/io/metersphere/xpack | 2 +- .../api/test/model/ScenarioModel.js | 19 +++++++++++++++++++ 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/jmeter/JMeterService.java b/backend/src/main/java/io/metersphere/api/jmeter/JMeterService.java index 45e177624d..91178520bb 100644 --- a/backend/src/main/java/io/metersphere/api/jmeter/JMeterService.java +++ b/backend/src/main/java/io/metersphere/api/jmeter/JMeterService.java @@ -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) { diff --git a/backend/src/main/java/io/metersphere/api/jmeter/JMeterVars.java b/backend/src/main/java/io/metersphere/api/jmeter/JMeterVars.java index 380fd0078f..8839acf52d 100644 --- a/backend/src/main/java/io/metersphere/api/jmeter/JMeterVars.java +++ b/backend/src/main/java/io/metersphere/api/jmeter/JMeterVars.java @@ -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 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 extracts = Arrays.asList(extract.split(";")); + extracts.forEach(item -> { + vs.put(item, vars.get(item)); + }); + } + variables.put(testId, vs); } } diff --git a/backend/src/main/java/io/metersphere/xpack b/backend/src/main/java/io/metersphere/xpack index cf6b065263..c2dacf960c 160000 --- a/backend/src/main/java/io/metersphere/xpack +++ b/backend/src/main/java/io/metersphere/xpack @@ -1 +1 @@ -Subproject commit cf6b06526324326a563d933e07118fac014a63b4 +Subproject commit c2dacf960cdb1ed35664bdd3432120b1203b73d8 diff --git a/frontend/src/business/components/api/test/model/ScenarioModel.js b/frontend/src/business/components/api/test/model/ScenarioModel.js index 4c247d0874..23f25856e9 100644 --- a/frontend/src/business/components/api/test/model/ScenarioModel.js +++ b/frontend/src/business/components/api/test/model/ScenarioModel.js @@ -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)); } }