简体   繁体   English

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

[英]jmeter - How can I store data from first request and pass it to second request using java code

I've created a simple test plan in GUI mode, it contains 3 HTTP requests and "Create cart" response contains "id" that I need to pass as a path param to the request of "Update cart" .我在 GUI 模式下创建了一个简单的测试计划,它包含 3 个 HTTP 请求, “创建购物车”响应包含“id” ,我需要将其作为路径参数传递给“更新购物车”的请求。
I've used the JSON extractor to extract and store this variable and in GUI mode everything works just fine.我使用 JSON 提取器来提取和存储这个变量,在 GUI 模式下一切正常。 I access the variable via ${token}我通过 ${token} 访问变量

测试计划结构
The issue I have - I don't know how to extract, store and access the stored variable from the java code.我遇到的问题 - 我不知道如何从 Java 代码中提取、存储和访问存储的变量。 I played around with JsonPostProcessor but seems like I use it incorrectly.我玩过 JsonPostProcessor,但似乎我使用不正确。
Kindly see the code below:请看下面的代码:

    ...

    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);
    ...

I believe my mistake is somewhere around the JsonPostProcessor, maybe I should use another class or another way how to extract, store and invoke data from one request to another.我相信我的错误出在 JsonPostProcessor 附近,也许我应该使用另一个类或另一种方法来提取、存储和调用从一个请求到另一个请求的数据。 Will appreciate any advice将不胜感激任何建议

It is impossible to help you without seeing your set details implementation, in order to add JSON Extractor as a child of a Sampler you need to add both to a HashTree , then add this HashTree to the Thread Group 's HashTree, add Thread Group's HashTree to Test Plan 's HashTree, etc.没有看到您的set details实现就不可能帮助您,为了将JSON Extractor添加为 Sampler 的子项,您需要将两者都添加到HashTree ,然后将此 HashTree 添加到Thread Group的 HashTree,添加 Thread Group 的 HashTree到Test Plan的 HashTree 等。

Example minimal working code would be something like:示例最小工作代码将类似于:

// 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);

Check out Five Ways To Launch a JMeter Test without Using the JMeter GUI article for general information, you can also find some hints and code snippets in the comments section.查看在不使用 JMeter GUI 的情况下启动 JMeter 测试的五种方法文章了解一般信息,您还可以在评论部分找到一些提示和代码片段。

If you're not too comfortable with writing Java code using JMeter API it might be easier to consider using Taurus tool which provides possibility to build JMeter test plans programmatically using simple YAML syntax .如果您不太习惯使用JMeter API编写 Java 代码,那么考虑使用Taurus工具可能会更容易,该工具提供了使用简单的 YAML 语法以编程方式构建 JMeter 测试计划的可能性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在非GUI模式下使用Java代码进行JMeter测试的post请求中添加请求体数据? - How to add the request body data in the post request for JMeter testing using Java code in the non-GUI mode? 使用RestTemplate,如何首先将请求发送到代理,以便我可以将我的junits与JMeter一起使用? - Using RestTemplate, how to send the request to a proxy first so I can use my junits with JMeter? 如何使用Java在jmeter中获取身体数据请求 - How to get body data request in jmeter using java 在 Jmeter 中的每个 http 请求之后,我如何将 JsonExtractor 返回存储在数组中? - how can i store the JsonExtractor return in an array after each http request in Jmeter? JMeter - 如何将多行响应数据传递给 ForEach Controller 请求 - JMeter - How to pass Multiline response data to the ForEach Controller request 如何在Java代码中创建jdbc请求的Jmeter测试 - How to create a Jmeter test of jdbc request in Java code 可以存储来自JMeter请求的结果并使用它吗? - Possible to store result from JMeter request and use it? JMeter-将JDBC响应代码传递给Java请求? - JMeter - passing jdbc response code to a java request? 如何在JMeter中访问Java请求的返回值? - How do I access the returned value of a Java Request in JMeter? 我如何将请求对象从一个类传递到另一个类 - how can i pass request object from one class to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM