0
votes

I am creating a JMeter jmx file dynamically by using JMeter APIs. I am able to add a ThreadGroup within a TestPlan and a JavaSampler within the ThreadGroup. But when I add a CSVDataSet element within the Java Sampler, it does not get saved properly.

The following code is used to create a new CSVDataSet element

    CSVDataSet csvDataSet = new CSVDataSet();
    csvDataSet.setName("CSV Data Set");
    csvDataSet.setComment("Sample CSV Data Set");
    csvDataSet.setDelimiter(",");
    csvDataSet.setFileEncoding("");
    csvDataSet.setFilename("d:\\jmeter\\data.csv"); // variable
    csvDataSet.setQuotedData(true);
    csvDataSet.setRecycle(true);
    csvDataSet.setShareMode(shareMode.all);
    csvDataSet.setStopThread(false);
    csvDataSet.setVariableNames("firstname, lastname, email"); // variable
    csvDataSet.setEnabled(true);

When this is saved using SaveService.saveTree, the final jmx does not contain all the values which were set.

    <org.apache.jorphan.collections.HashTree>
      <CSVDataSet testname="CSV Data Set Config" enabled="true">
        <stringProp name="TestPlan.comments">Sample CSV Data Set Config</stringProp>
      </CSVDataSet>
    <org.apache.jorphan.collections.HashTree/>

As seen above, only the test name, enabled, and comments are added. The rest of the variables are completely ignored.

Is there something that needs to be set in order to get all the values as expected? or is this a bug in JMeter? I am using version 2.11

The basic code is as per section 4.3 from following link http://blazemeter.com/blog/5-ways-launch-jmeter-test-without-using-jmeter-gui

To that I add the code shown above. The way it is added is,

    testPlanTree.add("testPlan", testPlan);
    testPlanTree.add("loopController", loopController);
    testPlanTree.add("threadGroup", threadGroup);
    testPlanTree.add("httpSampler", httpSampler);
    testPlanTree.add("csvDataSet", csvDataSet);

    SaveService
            .saveTree(testPlanTree, new FileOutputStream("d:\\test.jmx"));

output of CSVDataSet block is as shown above.

2

2 Answers

2
votes

After looking into the JMeter source code, it seems all the properties are set using the setProperty function rather than the individual setter functions. So putting the following code does the job of creating the CSVDataSet element properly.

    csvDataSet.setProperty("delimiter", ",");
    csvDataSet.setProperty("fileEncoding", "");
    csvDataSet.setProperty("filename", "d:\\data.csv");
    csvDataSet.setProperty("quotedData", true);
    csvDataSet.setProperty("recycle", true);
    csvDataSet.setProperty("shareMode", "shareMode.all");
    csvDataSet.setProperty("stopThread", false);
    csvDataSet.setProperty("variableNames", "var1, var2, var3");

Not sure why setters are not used in the code, but this seems to be the way to go for now

0
votes

It is clearly not a bug in JMeter otherwise CSV Data Set could not be saved.

It is probably an issue in the way you build the HashTree, but unless you show the full code, you cannot get help.

By the way, as I said in a previous answer, what you are trying to do to build different tests based on input parameter is not good idea IMHO, the approach will be very fragile towards upcoming versions of JMeter.

JMeter provides ways to do it that you should follow.