jmeter—如何使用java代码存储来自第一个请求的数据并将其传递给第二个请求

jmeter—如何使用java代码存储来自第一个请求的数据并将其传递给第二个请求,java,jmeter,Java,Jmeter,我在GUI模式下创建了一个简单的测试计划,它包含3个HTTP请求和“创建购物车”响应包含“id”,我需要将其作为路径参数传递给“更新购物车”的请求 我使用JSON提取器来提取和存储这个变量,在GUI模式下,一切正常。我通过${token}访问变量 我的问题是——我不知道如何从java代码中提取、存储和访问存储的变量。我使用了JsonPostProcessor,但似乎使用不当。请查看下面的代码: ... HashTree testPlanContainer = new Has

我在GUI模式下创建了一个简单的测试计划,它包含3个HTTP请求和“创建购物车”响应包含“id”,我需要将其作为路径参数传递给“更新购物车”的请求
我使用JSON提取器来提取和存储这个变量,在GUI模式下,一切正常。我通过${token}访问变量


我的问题是——我不知道如何从java代码中提取、存储和访问存储的变量。我使用了JsonPostProcessor,但似乎使用不当。
请查看下面的代码:

    ...

    HashTree testPlanContainer = new HashTree();

    HeaderManager headerManager = new HeaderManager();
    headerManager.add(new Header("Authorization", "Bearer " + token));
    headerManager.setProperty(TestElement.TEST_CLASS, HeaderManager.class.getName());
    headerManager.setProperty(TestElement.GUI_CLASS, HeaderPanel.class.getName());

    HTTPSampler createCustomer = new HTTPSampler();
    ...set domain, method, body, etc

    HTTPSampler createCart = new HTTPSampler();
    ...set domain, method, body, etc

    JSONPostProcessor jsonPostProcessor = new JSONPostProcessor();
    jsonPostProcessor.setRefNames("cart-id");
    jsonPostProcessor.setJsonPathExpressions("$.id");
    jsonPostProcessor.process();

    HashTree composeCreateCartWithJsonExtractor = new HashTree();
    composeCreateCartWithJsonExtractor.add(createCart, jsonPostProcessor);

    HTTPSampler updateCart = new HTTPSampler();
    updateCart.setPath("path"  + ${cart-id}); //how can I access this variable from java code?
    ... set domain, method, body, etc

    LoopController loopController = new LoopController();
    ... set details

    SetupThreadGroup threadGroup = new SetupThreadGroup();
    ... set details

    TestPlan testPlan = new TestPlan("My test plan");
    ...set details 

    testPlanContainer.add(testPlan);

    HashTree composer = testPlanContainer.add(testPlan, threadGroup);
    composer.add(headerManager);
    composer.add(createCustomer);
    composer.add(composeCreateCartWithJsonExtractor);
    composer.add(updateCart);

    SaveService.saveTree(testPlanContainer, new FileOutputStream("src/main/resources/jmxFile.jmx"));

    Summariser summariser = new Summariser("summaryOfResults");
    ResultCollector resultCollector = new ResultCollector(summariser);
    ...
    testPlanContainer.add(testPlanContainer.getArray()[0], resultCollector);
    ...
我相信我的错误在JsonPostProcessor的某个地方,也许我应该使用另一个类或另一种方法来提取、存储和调用从一个请求到另一个请求的数据。
如果没有看到
设置详细信息
实现,就不可能帮助您,为了作为采样器的子级添加,您需要将两者都添加到,然后将此哈希树添加到的哈希树,将线程组的哈希树添加到的哈希树,等等

示例最小工作代码如下所示:

// JMeter Test Plan, basically JOrphan HashTree
HashTree testPlanTree = new HashTree();

// First HTTP Sampler - open jsonplaceholder.typicode.com and extract id
HTTPSamplerProxy jsonplaceholderSampler = new HTTPSamplerProxy();
jsonplaceholderSampler.setDomain("jsonplaceholder.typicode.com");
jsonplaceholderSampler.setPort(80);
jsonplaceholderSampler.setPath("/todos/1");
jsonplaceholderSampler.setMethod("GET");
jsonplaceholderSampler.setName("HTTP Request - get id");
jsonplaceholderSampler.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
jsonplaceholderSampler.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());

JSONPostProcessor jsonPostProcessor = new JSONPostProcessor();
jsonPostProcessor.setName("JSON Extractor");
jsonPostProcessor.setProperty("JSONPostProcessor.referenceNames", "cart-id");
jsonPostProcessor.setProperty("JSONPostProcessor.jsonPathExprs", "$.id");
jsonPostProcessor.setProperty(TestElement.TEST_CLASS, JSONPostProcessor.class.getName());
jsonPostProcessor.setProperty(TestElement.GUI_CLASS, JSONPostProcessorGui.class.getName());


// Second HTTP Sampler - open example com
HTTPSamplerProxy exampleComSampler = new HTTPSamplerProxy();
exampleComSampler.setDomain("example.com");
exampleComSampler.setPort(80);
exampleComSampler.setPath("/${cart-id}");
exampleComSampler.setMethod("GET");
exampleComSampler.setName("HTTP Request - send id");
exampleComSampler.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
exampleComSampler.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());


// Loop Controller
LoopController loopController = new LoopController();
loopController.setLoops(1);
loopController.setFirst(true);
loopController.setProperty(TestElement.TEST_CLASS, LoopController.class.getName());
loopController.setProperty(TestElement.GUI_CLASS, LoopControlPanel.class.getName());
loopController.initialize();

// Thread Group
ThreadGroup threadGroup = new ThreadGroup();
threadGroup.setName("Example Thread Group");
threadGroup.setNumThreads(1);
threadGroup.setRampUp(1);
threadGroup.setSamplerController(loopController);
threadGroup.setProperty(TestElement.TEST_CLASS, ThreadGroup.class.getName());
threadGroup.setProperty(TestElement.GUI_CLASS, ThreadGroupGui.class.getName());

// Test Plan
TestPlan testPlan = new TestPlan("Create JMeter Script From Java Code");
testPlan.setProperty(TestElement.TEST_CLASS, TestPlan.class.getName());
testPlan.setProperty(TestElement.GUI_CLASS, TestPlanGui.class.getName());
testPlan.setUserDefinedVariables((Arguments) new ArgumentsPanel().createTestElement());

// Construct Test Plan from previously initialized elements
testPlanTree.add(testPlan);
HashTree threadGroupHashTree = testPlanTree.add(testPlan, threadGroup);
HashTree firstSamplerHashTree = new HashTree();
firstSamplerHashTree.add(exampleComSampler);
firstSamplerHashTree.add(jsonplaceholderSampler, jsonPostProcessor);
threadGroupHashTree.add(firstSamplerHashTree);
有关一般信息,您还可以在评论部分找到一些提示和代码片段


如果你不太习惯用java编写代码,那么使用工具可能会更容易。p> 如果不查看您的

设置详细信息
实现,就无法帮助您,为了作为采样器的子级添加,您需要将两者都添加到,然后将此HashTree添加到的HashTree,将线程组的HashTree添加到的HashTree,等等

示例最小工作代码如下所示:

// JMeter Test Plan, basically JOrphan HashTree
HashTree testPlanTree = new HashTree();

// First HTTP Sampler - open jsonplaceholder.typicode.com and extract id
HTTPSamplerProxy jsonplaceholderSampler = new HTTPSamplerProxy();
jsonplaceholderSampler.setDomain("jsonplaceholder.typicode.com");
jsonplaceholderSampler.setPort(80);
jsonplaceholderSampler.setPath("/todos/1");
jsonplaceholderSampler.setMethod("GET");
jsonplaceholderSampler.setName("HTTP Request - get id");
jsonplaceholderSampler.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
jsonplaceholderSampler.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());

JSONPostProcessor jsonPostProcessor = new JSONPostProcessor();
jsonPostProcessor.setName("JSON Extractor");
jsonPostProcessor.setProperty("JSONPostProcessor.referenceNames", "cart-id");
jsonPostProcessor.setProperty("JSONPostProcessor.jsonPathExprs", "$.id");
jsonPostProcessor.setProperty(TestElement.TEST_CLASS, JSONPostProcessor.class.getName());
jsonPostProcessor.setProperty(TestElement.GUI_CLASS, JSONPostProcessorGui.class.getName());


// Second HTTP Sampler - open example com
HTTPSamplerProxy exampleComSampler = new HTTPSamplerProxy();
exampleComSampler.setDomain("example.com");
exampleComSampler.setPort(80);
exampleComSampler.setPath("/${cart-id}");
exampleComSampler.setMethod("GET");
exampleComSampler.setName("HTTP Request - send id");
exampleComSampler.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
exampleComSampler.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());


// Loop Controller
LoopController loopController = new LoopController();
loopController.setLoops(1);
loopController.setFirst(true);
loopController.setProperty(TestElement.TEST_CLASS, LoopController.class.getName());
loopController.setProperty(TestElement.GUI_CLASS, LoopControlPanel.class.getName());
loopController.initialize();

// Thread Group
ThreadGroup threadGroup = new ThreadGroup();
threadGroup.setName("Example Thread Group");
threadGroup.setNumThreads(1);
threadGroup.setRampUp(1);
threadGroup.setSamplerController(loopController);
threadGroup.setProperty(TestElement.TEST_CLASS, ThreadGroup.class.getName());
threadGroup.setProperty(TestElement.GUI_CLASS, ThreadGroupGui.class.getName());

// Test Plan
TestPlan testPlan = new TestPlan("Create JMeter Script From Java Code");
testPlan.setProperty(TestElement.TEST_CLASS, TestPlan.class.getName());
testPlan.setProperty(TestElement.GUI_CLASS, TestPlanGui.class.getName());
testPlan.setUserDefinedVariables((Arguments) new ArgumentsPanel().createTestElement());

// Construct Test Plan from previously initialized elements
testPlanTree.add(testPlan);
HashTree threadGroupHashTree = testPlanTree.add(testPlan, threadGroup);
HashTree firstSamplerHashTree = new HashTree();
firstSamplerHashTree.add(exampleComSampler);
firstSamplerHashTree.add(jsonplaceholderSampler, jsonPostProcessor);
threadGroupHashTree.add(firstSamplerHashTree);
有关一般信息,您还可以在评论部分找到一些提示和代码片段


如果你不太习惯用java编写代码,那么使用工具可能会更容易。p> 为什么需要HTTPSampler来执行HTTP请求?为什么不使用HttpClient发送请求和接收响应?为什么需要HTTPSampler来执行HTTP请求?为什么不使用HttpClient发送请求和接收响应呢?亲爱的Dmitri,老实说,我希望你能帮我找到答案,我在这里阅读了大量你的答案,发现它们非常有用。这一个也不例外,它有帮助)再次感谢你分享你的专家Dear Dmitri,老实说,我希望你能帮助我回答这个问题,我在这里读了很多你的答案,发现它们非常有用。这一次也不例外,它有帮助)再次感谢您分享您的专业知识