Creating a New JMeter Test Purely in Java ans save as valid *.jmx

مصطفی Source

I follow methods introduced in the this thread and then save the hashTree as follow:

    FileOutputStream out = new FileOutputStream("test.jmx");
    SaveService.saveTree(hashTree, out);

But, JMeter fails to open the file: enter image description here

Is it possible to save and restore created test?


In the other hand, there is a project on GitHub to create and save test. How ever the result is not valid JMX file.


I compare created file with a simple JMeter valid test script. These attributes are not added:

guiclass="TestPlanGui" testclass="TestPlan" 

I add them manually and all error fixed?!


Test and GUI classes must set as properties in a test element. Here is part of AbstractJMeterGuiComponent :

    mc.setProperty(new StringProperty(TestElement.GUI_CLASS, this.getClass().getName()));
    mc.setProperty(new StringProperty(TestElement.TEST_CLASS, mc.getClass().getName()));

So, GUI and the core is tied to each other and there are some main configurations in GUI.

How to build a valid JMX whiteout know about GUI?

javajmeter

Answers

answered 6 months ago Dmitri T #1

Most likely you forgot to set the following properties:

If you omit them you will still be able to run the test using JMeterEngine, however attempt to open the generated script in GUI will fail.

Here is an example Test Plan which contains a Thread Group and a HTTP Request sampler, you can use it as a reference.

import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.config.gui.ArgumentsPanel;
import org.apache.jmeter.control.LoopController;
import org.apache.jmeter.control.gui.LoopControlPanel;
import org.apache.jmeter.control.gui.TestPlanGui;
import org.apache.jmeter.protocol.http.control.gui.HttpTestSampleGui;
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.testelement.TestPlan;
import org.apache.jmeter.threads.ThreadGroup;
import org.apache.jmeter.threads.gui.ThreadGroupGui;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.collections.HashTree;

import java.io.File;
import java.io.FileOutputStream;

public class JMeterFromJava {

    public static void main(String[] args) throws Exception {

        String jmeterHome = "/path/to/your/jmeter/installation";


        JMeterUtils.setJMeterHome(jmeterHome);
        JMeterUtils.loadJMeterProperties(jmeterHome + "/bin/jmeter.properties");
        JMeterUtils.initLocale();


        HashTree testPlanTree = new HashTree();

        HTTPSamplerProxy examplecomSampler = new HTTPSamplerProxy();
        examplecomSampler.setDomain("example.com");
        examplecomSampler.setPort(80);
        examplecomSampler.setPath("/");
        examplecomSampler.setMethod("GET");
        examplecomSampler.setName("Open example.com");
        examplecomSampler.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
        examplecomSampler.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());


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

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

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

        testPlanTree.add(testPlan);
        HashTree threadGroupHashTree = testPlanTree.add(testPlan, threadGroup);
        threadGroupHashTree.add(examplecomSampler);

        SaveService.saveTree(testPlanTree, new FileOutputStream(jmeterHome + "/bin/test.jmx"));


    }
}

References:

comments powered by Disqus