簡體   English   中英

使用 Java 將斷言模塊從 Jmeter 添加到 HashTree

[英]Adding Assertion module to HashTree from Jmeter using Java

編輯

我的測試腳本的雇用如下:

- 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 的第三個內部循環不執行。 但是,每個線程組中肯定至少有一個采樣器。

您基本上需要實例化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"));

更多信息: 在不使用 JMeter GUI 的情況下啟動 JMeter 測試的五種方法

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM