【问题标题】:How to add Basic Authentication while creating Jmeter Script from Jmeter API using java?如何在使用 java 从 Jmeter API 创建 Jmeter 脚本时添加基本身份验证?
【发布时间】:2018-03-16 11:49:52
【问题描述】:

我浏览了一些脚本,我们可以使用 apache jmeter api 从头开始​​创建 Jmeter 脚本。

但是,无法为我的 api 测试脚本添加授权。

我知道“AuthManager”类可用于它,但是,我无法正确使用它,我遇到未经授权的错误,即使我提供了正确的凭据。

请帮帮我!

【问题讨论】:

    标签: java jmeter jmeter-3.2 jmeter-maven-plugin jmeter-4.0


    【解决方案1】:

    您需要将AuthManager 的实例添加到您的Test Plan 相关代码将类似于:

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

    完整的测试计划以防万一:

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

    更多信息:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多