【问题标题】:Adding Assertion module to HashTree from Jmeter using Java使用 Java 将断言模块从 Jmeter 添加到 HashTree
【发布时间】:2019-10-22 12:58:10
【问题描述】:

编辑

我的测试脚本的层次如下:

- 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 smaplers 的第三个内部循环不执行。但是,每个线程组中肯定至少有一个采样器。

【问题讨论】:

    标签: java jmeter qa


    【解决方案1】:

    您基本上需要实例化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

    【讨论】:

    • 感谢您的帮助。但是,需要将断言添加为 http 采样器的子项。如何从 testPlan 树中获取特定元素(例如 http 采样器)?如果我有多个 http 采样器,我能否以与 GUI 中相同的顺序获取它们?
    • 使用 SearchByClass 定位所有 HTTP 请求采样器,然后过滤它们以匹配您需要的采样器,然后添加断言
    • @Dmitri_T 我更新了我的问题,我尝试了你的建议,它部分有效。但是,当我尝试遍历第三个层次结构时,我遇到了一些新问题。查看我编辑的帖子了解更多详情。
    猜你喜欢
    • 1970-01-01
    • 2018-09-21
    • 2013-04-17
    • 2011-04-15
    • 1970-01-01
    • 2021-07-13
    • 2021-05-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多