Jmeter非GUI:未设置RandomVariableConfig值

时间:2018-01-16 04:38:30

标签: jmeter jmeter-3.2

我正在尝试在我的代码中使用RandomVariableConfig()来生成测试计划。我在下面粘贴我的代码。执行代码后,将创建一个名为Test3.jmx的文件。当我通读它时,我看到以下内容:

<RandomVariableConfig guiclass="TestBeanGUI" 
testclass="RandomVariableConfig" testname="RandomVariable"/>

由于某种原因,我的minimumValue,maximumValue,variableName未被设置。我在jmx文件中没有看到这些值。

请您指导一下为什么会这样以及如何解决这个问题?谢谢!

这是我的代码:

public class Test3 {

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

        StandardJMeterEngine jmeterEngine = new StandardJMeterEngine();
        JMeterUtils.setJMeterHome("/Users/chandrat/Documents/JMeter/apache-jmeter-3.3");
        JMeterUtils.loadJMeterProperties("src/main/resources/jmeter.properties");
        JMeterUtils.initLocale();

        //create test plan
        TestPlan testPlan = new TestPlan();
        testPlan.setEnabled(true);
        testPlan.setName("Test Plan");
        testPlan.setProperty(TestElement.TEST_CLASS, TestPlan.class.getName());
        testPlan.setProperty(TestElement.GUI_CLASS, TestPlanGui.class.getName());
        testPlan.setUserDefinedVariables((Arguments) new ArgumentsPanel().createTestElement());

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

        //create thread group
        ThreadGroup threadGroup = new ThreadGroup();
        threadGroup.setName("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());

        // create http sampler for Renga request
        HTTPSampler rengaSampler = new HTTPSampler();
        rengaSampler.setName("Renga Create User Request");
        rengaSampler.setPostBodyRaw(true);
        rengaSampler.addArgument("", "<?xml version=\"1.0\" encoding=\"UTF-8\"?> PLACEHOLDER REQUEST BODY");

        rengaSampler.setProtocol("https");
        rengaSampler.setDomain("sample.domain");
        rengaSampler.setPath("sample/path);
        rengaSampler.setMethod("POST");
        rengaSampler.setFollowRedirects(true);
        rengaSampler.setAutoRedirects(false);
        rengaSampler.setUseKeepAlive(true);

        rengaSampler.setProperty(TestElement.TEST_CLASS, HTTPSampler.class.getName());
        rengaSampler.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());

        HeaderManager rengaHeaders = new HeaderManager();
        rengaHeaders.setProperty(TestElement.TEST_CLASS, HeaderManager.class.getName());
        rengaHeaders.setProperty(TestElement.GUI_CLASS, HeaderPanel.class.getName());
        rengaHeaders.setName("Renga Headers");
        rengaHeaders.setEnabled(true);

        Header h1 = new Header();
        h1.setName("Content-type");
        h1.setValue("text/XML");
        Header h2 = new Header();
        h2.setName("char-set");
        h2.setValue("UTF-8");
        rengaHeaders.add(h1);
        rengaHeaders.add(h2);

        //generate random variable
        RandomVariableConfig randomVariable = new RandomVariableConfig();
        randomVariable.setName("RandomVariable");
        randomVariable.setVariableName("rand");
        randomVariable.setMinimumValue("12345678");
        randomVariable.setMaximumValue("88888888888888");
        randomVariable.setPerThread(false);
        randomVariable.setProperty(TestElement.TEST_CLASS, RandomVariableConfig.class.getName());
        randomVariable.setProperty(TestElement.GUI_CLASS, TestBeanGUI.class.getName());

        //Add sampler to the thread group

        HashTree testPlanHashTree = new HashTree();
        HashTree threadGroupHashTree = testPlanHashTree.add(testPlan, threadGroup);

        HashTree rengaSamplerHashTree = new HashTree();
        rengaSamplerHashTree.add(rengaSampler, rengaHeaders);
        rengaSamplerHashTree.add(rengaSampler, randomVariable);
        threadGroupHashTree.add(rengaSamplerHashTree);


        // Generating the JMX file
        SaveService.saveTree(testPlanHashTree, new FileOutputStream(JMeterUtils.getJMeterHome() + "/bin/Test3.jmx"));
    }
}

1 个答案:

答案 0 :(得分:0)

您创建Random Variable配置元素的方法不正确,您需要修改相关代码,如下所示:

//generate random variable
RandomVariableConfig randomVariable = new RandomVariableConfig();
randomVariable.setName("RandomVariable");
randomVariable.setProperty("variableName","rand");
randomVariable.setProperty("minimumValue","12345678");
randomVariable.setProperty("maximumValue","88888888888888");
randomVariable.setProperty("perThread",false);
randomVariable.setProperty(TestElement.TEST_CLASS, RandomVariableConfig.class.getName());
randomVariable.setProperty(TestElement.GUI_CLASS, TestBeanGUI.class.getName());

有关运行和创建JMeter测试(包括程序化)的不同选项的更多信息,请参阅Five Ways To Launch a JMeter Test without Using the JMeter GUI文章。

但请注意,在绝大多数情况下,使用JMeter GUI更容易进行测试创建,因为其他选项不能正式支持,并且获得JMeter开发人员支持的可能性很小。