[英]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.