Swati C
Swati C

Reputation: 13

How to set BeanShellPostProcessor script from java code instead from Jmeter GUI?

I have requirement, where I need to add BeanShellPostProcessor programmatically preferably in java.

Below is the sample code to add BeanShellPostProcessor to JMX and it is working.But I don't want to hard code the script (see the below code), script will change for each API test. How to write the beanshell script programmatically

String script = "if (prev.getResponseCode().equals(401) == true) { prev.setResponseOK();}";

        BeanShellPostProcessor bspp = new BeanShellPostProcessor();
        bspp.setProperty(TestElement.TEST_CLASS, BeanShellPostProcessor.class.getName());
        bspp.setProperty(TestElement.GUI_CLASS, TestBeanGUI.class.getName());
        bspp.setName("BeanShell PostProcessor");
        bspp.setProperty("resetInterpreter", false);
        bspp.setProperty("enabled", true);
        bspp.setProperty("script", script );

        HTTPSampleProxyHashTree.add(bspp);

Upvotes: 1

Views: 576

Answers (1)

Dmitri T
Dmitri T

Reputation: 168072

  1. Since JMeter 3.1 you should be using JSR223 Test Elements and Groovy language for scripting so consider migrating to JSR223 PostProcessor instead.
  2. You can make your script dynamic by loading it from i.e. JMeter Properties. The relevant code would be something like:

    String script = JMeterUtils.getPropDefault("my.script",
            "if (prev.getResponseCode().equals(401) == true) { prev.setResponseOK();}");
    JSR223PostProcessor jsr223PostProcessor = new JSR223PostProcessor();
    jsr223PostProcessor.setName("JSR223 PostProcessor");
    jsr223PostProcessor.setProperty("cacheKey", "true");
    jsr223PostProcessor.setProperty("script", script);
    jsr223PostProcessor.setProperty("scriptLanguage", "groovy");
    jsr223PostProcessor.setProperty(TestElement.TEST_CLASS, JSR223PostProcessor.class.getName());
    jsr223PostProcessor.setProperty(TestElement.GUI_CLASS, TestBeanGUI.class.getName());
    

    if my.script property is set - JMeter will use its value as the Groovy script, if not - it will proceed with the default hard-coded value

  3. You don't even need scripting at all, it would be enough to add a Response Assertion with Ignore Status box ticked.

  4. Full code of the class which adds a JSR223 PostProcessor as a child of the HTTP Request sampler:

    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.engine.StandardJMeterEngine;
    import org.apache.jmeter.extractor.JSR223PostProcessor;
    import org.apache.jmeter.protocol.http.control.gui.HttpTestSampleGui;
    import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy;
    import org.apache.jmeter.reporters.ResultCollector;
    import org.apache.jmeter.reporters.Summariser;
    import org.apache.jmeter.save.SaveService;
    import org.apache.jmeter.testbeans.gui.TestBeanGUI;
    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.FileOutputStream;
    
    public class JMeterFromScratch {
    
        public static void main(String[] args) throws Exception {
    
            String jmeterHome = "/path/to/your/jmeter/installation";
            StandardJMeterEngine jmeter = new StandardJMeterEngine();
    
            //JMeter initialization (properties, log levels, locale, etc)
            JMeterUtils.setJMeterHome(jmeterHome);
            JMeterUtils.loadJMeterProperties(jmeterHome + "/bin/jmeter.properties");
            JMeterUtils.initLocale();
    
            // JMeter Test Plan, basically JOrphan HashTree
            HashTree testPlanTree = new HashTree();
    
            // JMeter Test Plan, basically JOrphan HashTree
    
            // Create JSR223 PostProcessor
            String script = JMeterUtils.getPropDefault("my.script",
                    "if (prev.getResponseCode().equals(401) == true) { prev.setResponseOK();}");
            JSR223PostProcessor jsr223PostProcessor = new JSR223PostProcessor();
            jsr223PostProcessor.setName("JSR223 PostProcessor");
            jsr223PostProcessor.setProperty("cacheKey", "true");
            jsr223PostProcessor.setProperty("script", script);
            jsr223PostProcessor.setProperty("scriptLanguage", "groovy");
            jsr223PostProcessor.setProperty(TestElement.TEST_CLASS, JSR223PostProcessor.class.getName());
            jsr223PostProcessor.setProperty(TestElement.GUI_CLASS, TestBeanGUI.class.getName());
    
            // HTTP Sampler - open example.com
            HTTPSamplerProxy examplecomSampler = new HTTPSamplerProxy();
            examplecomSampler.setDomain("example.com.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());
    
    
            // 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());
    
            // HTTP Request Sampler and JSR223 PostProcessor
            HashTree blazeMeterSamplerTree = new HashTree();
            blazeMeterSamplerTree.add(examplecomSampler, jsr223PostProcessor);
    
            // Construct Test Plan from previously initialized elements
            testPlanTree.add(testPlan);
            HashTree threadGroupHashTree = testPlanTree.add(testPlan, threadGroup);
            threadGroupHashTree.add(blazeMeterSamplerTree);
    
            SaveService.saveTree(testPlanTree, new FileOutputStream(jmeterHome + "/bin/test.jmx"));
    
            //Add Summarizer output
            Summariser summer = null;
            String summariserName = JMeterUtils.getPropDefault("summariser.name", "summary");
            if (summariserName.length() > 0) {
                summer = new Summariser(summariserName);
            }
    
            // Store Execution Results into a .csv file
            String csvFile = jmeterHome + "/bin/result.csv";
            ResultCollector logger = new ResultCollector(summer);
            logger.setFilename(csvFile);
            testPlanTree.add(testPlanTree.getArray()[0], logger);
    
            //Run Test Plan
            jmeter.configure(testPlanTree);
            jmeter.run();
    
            System.exit(0);
    
        }
    }
    
  5. More information: Five Ways To Launch a JMeter Test without Using the JMeter GUI

Upvotes: 1

Related Questions