gpt4 book ai didi

java - 使用 Java 从 Jmeter 添加断言模块到 HashTree

转载 作者:太空宇宙 更新时间:2023-11-04 09:19:34 25 4
gpt4 key购买 nike

编辑

我的测试脚本的雇佣关系如下:

- test plan
- thread group 1
- http sampler 1-1

- thread group 2
- http sampler 2-1
- thread group 1
- http sampler 1-1

- thread group 3
- http sampler 3-1

我可以遍历所有线程组。但是,我无法使用类似的模式来遍历 http 采样器。

这是我的代码:

public static void main(String[] args) throws IOException {
// LOAD EXITISTING JMETER XML
JmxReader jmxReader = new JmxReader(".\\resources\\manually-configure.jmx");
HashTree testPlanTree = jmxReader.getTree();

SearchByClass testPlanSearcher = new SearchByClass(TestPlan.class);
SearchByClass threadGroupSearcher = new SearchByClass(ThreadGroup.class);
SearchByClass httpSamplerSearcher = new SearchByClass(HTTPSampler.class);
testPlanTree.traverse(testPlanSearcher);
Iterator testPlanIter = testPlanSearcher.getSearchResults().iterator();

while (testPlanIter.hasNext()) { // This loop will only execute once, due to we only have one test plan
TestPlan testPlan = (TestPlan) testPlanIter.next();
HashTree subTreeOfTestPlan = testPlanSearcher.getSubTree(testPlan);
subTreeOfTestPlan.traverse(threadGroupSearcher);
Iterator threadGroupIter = threadGroupSearcher.getSearchResults().iterator();
while(threadGroupIter.hasNext()) {
ThreadGroup threadGroup = (ThreadGroup) threadGroupIter.next();
HashTree subTreeOfThreadGroup = threadGroupSearcher.getSubTree(threadGroup);
subTreeOfThreadGroup.traverse(httpSamplerSearcher);
Iterator httpSamplerIter = httpSamplerSearcher.getSearchResults().iterator();

while (httpSamplerIter.hasNext()) {
httpSamplerIter.next();
System.out.println("I found a http sampler"); \\ ??? Nothing got printed
}
}
}
}

遍历 http smapler 的第三个内部循环不执行。但是,每个线程组中至少有一个采样器。

最佳答案

你基本上需要实例化 ResponseAssertion并将其添加到您需要的任何位置,例如添加到测试计划的根目录中:

testPlanTree = SaveService.loadTree(in);

ResponseAssertion responseAssertion = new ResponseAssertion();
responseAssertion.setName("Response Assertion");
//configure the assertion according to your requirements
responseAssertion.setProperty(TestElement.TEST_CLASS, ResponseAssertion.class.getName());
responseAssertion.setProperty(TestElement.GUI_CLASS, AssertionGui.class.getName());

testPlanTree.add(responseAssertion);

SaveService.saveTree(testPlanTree, new FileOutputStream("/path/to/script/with/assertion.jmx"));

更多信息:Five Ways To Launch a JMeter Test without Using the JMeter GUI

关于java - 使用 Java 从 Jmeter 添加断言模块到 HashTree,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58504877/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com