0

I have gone through some of the scripts, where we can create Jmeter scripts from scratch using apache jmeter api.

But, not able to add authorization to my api testing scripts.

I know 'AuthManager' class is available for it, But, I am not able to use it correctly, I am geeting unauthorised error, even if i give correct credentials.

Please help me out!

Deepak S
  • 9
  • 6

1 Answers1

0

You need to add an instance of AuthManager to your Test Plan The relevant code would be something like:

AuthManager manager = new AuthManager();
Authorization authorization = new Authorization();
authorization.setURL("http://example.com");
authorization.setUser("foo");
authorization.setPass("bar");
manager.addAuth(authorization);
manager.setName(JMeterUtils.getResString("auth_manager_title")); // $NON-NLS-1$
manager.setProperty(TestElement.TEST_CLASS, AuthManager.class.getName());
manager.setProperty(TestElement.GUI_CLASS, AuthPanel.class.getName());

Full test plan just in case:

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.protocol.http.control.AuthManager;
import org.apache.jmeter.protocol.http.control.Authorization;
import org.apache.jmeter.protocol.http.control.gui.HttpTestSampleGui;
import org.apache.jmeter.protocol.http.gui.AuthPanel;
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.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();

        // Create HTTP Authorization Manager
        AuthManager manager = new AuthManager();
        Authorization authorization = new Authorization();
        authorization.setURL("http://example.com");
        authorization.setUser("foo");
        authorization.setPass("bar");
        manager.addAuth(authorization);
        manager.setName(JMeterUtils.getResString("auth_manager_title")); // $NON-NLS-1$
        manager.setProperty(TestElement.TEST_CLASS, AuthManager.class.getName());
        manager.setProperty(TestElement.GUI_CLASS, AuthPanel.class.getName());

        // HTTP Sampler - open example.com
        HTTPSamplerProxy httpRequest = new HTTPSamplerProxy();
        httpRequest.setDomain("example.com");
        httpRequest.setPort(80);
        httpRequest.setPath("/");
        httpRequest.setMethod("GET");
        httpRequest.setName("Open example.com");
        httpRequest.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
        httpRequest.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 Header Manager
        HashTree httpRequestTree = new HashTree();
        httpRequestTree.add(httpRequest, manager);

        // Construct Test Plan from previously initialized elements
        testPlanTree.add(testPlan);
        HashTree threadGroupHashTree = testPlanTree.add(testPlan, threadGroup);
        threadGroupHashTree.add(httpRequestTree);

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

        //add Summarizer output to get test progress in stdout like:
        // summary =      2 in   1.3s =    1.5/s Avg:   631 Min:   290 Max:   973 Err:     0 (0.00%)
        Summariser summer = null;
        String summariserName = JMeterUtils.getPropDefault("summariser.name", "summary");
        if (summariserName.length() > 0) {
            summer = new Summariser(summariserName);
        }


        // Store execution results into a .jtl file
        String logFile = jmeterHome + "/bin/result.jtl";
        ResultCollector logger = new ResultCollector(summer);
        logger.setFilename(logFile);
        testPlanTree.add(testPlanTree.getArray()[0], logger);

        // Run Test Plan
        jmeter.configure(testPlanTree);
        jmeter.run();

        System.exit(0);
    }
}

More information:

Dmitri T
  • 159,985
  • 5
  • 83
  • 133