0
votes

I am using Java code to compose a .jmx file for Jmeter GUI. I want to extract the GET response from HTTP proxy sampler that I use for "login", the response is used as a session id to recognize the user. Moreover, I need the next sampler to use the variable that is being extracted. In addition, I also need a cookie manager for each thread group.

Which API should I called for adding a extractor to a http proxy sampler and add add a cookie manager to a thread group?

I have already created http sampler,set the path and set the arguments. All I need now is a extractor to help me get the variable and a cookie manager

Below is my code in creating the thread group and adding http sampler to the thread group

HashTree testPlanTree = new ListedHashTree();
        TestPlan testPlan = new TestPlan("Create JMeter Script From Java Code");
        testPlanTree.add(testPlan);
        HTTPSamplerProxy newSampler = null;
        LoopController loopController;
        ThreadGroup threadGroup = null;
        HashTree threadGroupHashTree = null;
        int i = 1; // exclude the first empty call
        for (ArrayList<String> elementList : calls) {
            if (elementList.size() == 3) {
                System.out.println(elementList);
                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 = new ThreadGroup();
                threadGroup.setName(elementList.get(1) + "_" + elementList.get(2));
                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.setProperty(TestElement.TEST_CLASS, TestPlan.class.getName());
                testPlan.setProperty(TestElement.GUI_CLASS, TestPlanGui.class.getName());
                testPlan.setUserDefinedVariables((Arguments) new ArgumentsPanel().createTestElement());

                threadGroupHashTree = testPlanTree.add(testPlan, threadGroup);
            }
            else {
                newSampler = new HTTPSamplerProxy();
                LinkedHashMap<String, ArrayList<String>> arguments = httpSamplerList.get(i);
                if (elementList.get(0).equals("Login")) {
                    newSampler.setMethod(arguments.get("HTTPSampler.method").get(0));
                    newSampler.add
                }

                else if  (elementList.get(0).equals("Logout"))
                    newSampler.setMethod(arguments.get("HTTPSampler.method").get(0));
                else
                    newSampler.setMethod(elementList.get(0));

                System.out.println(httpSamplerList.get(i));

                newSampler.setPort(8080);
                newSampler.setPath(arguments.get("HTTPSampler.path").get(0));
                newSampler.setName(elementList.get(0)); // currently use a integer number naming as test
                newSampler.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
                newSampler.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());

                // Set up arguments
                if (arguments.get("arguments").size() > 0) {
                    if (arguments.get("HTTPSampler.method").get(0).equals("GET")) {
                        for (int j = 0; j < arguments.get("arguments").size(); j=j+2) {
                            newSampler.addArgument(arguments.get("arguments").get(j), arguments.get("arguments").get(j+1));
                        }
                    }
                    else if (arguments.get("HTTPSampler.method").get(0).equals("POST")||arguments.get("HTTPSampler.method").get(0).equals("PUT")) {
                        newSampler.addNonEncodedArgument("",arguments.get("arguments").get(0) , "");
                        newSampler.setPostBodyRaw(true);
                    }
                }
                threadGroupHashTree.add(newSampler);
                i++;
            }
        }
1

1 Answers

0
votes
  1. HTTP Cookie Manager:

    CookieManager cookieManager = new CookieManager();
    cookieManager.setName("HTTP Cookie Manager");
    cookieManager.setProperty(TestElement.TEST_CLASS, CookieManager.class.getName());
    cookieManager.setProperty(TestElement.GUI_CLASS, CookiePanel.class.getName());
    
  2. Regular Expression Extractor

    RegexExtractor regexExtractor = new RegexExtractor();
    regexExtractor.setName("Regular Expression Extractor");
    regexExtractor.setProperty("RegexExtractor.useHeaders", "false");
    regexExtractor.setProperty("RegexExtractor.refname", "yourVariable");
    regexExtractor.setProperty("RegexExtractor.regex", "yourRegex");
    regexExtractor.setProperty("RegexExtractor.template", "yourTemplate");
    regexExtractor.setProperty("RegexExtractor.match_number", "yourMatchNumber");
    regexExtractor.setProperty("RegexExtractor.default", "yourDefaultValue");
    regexExtractor.setProperty(TestElement.TEST_CLASS, RegexExtractor.class.getName());
    regexExtractor.setProperty(TestElement.GUI_CLASS, RegexExtractorGui.class.getName());
    

Check out Five Ways To Launch a JMeter Test without Using the JMeter GUI to learn more about different options of running a JMeter test including creating test plan programmatically.