0
votes

We have JMeter script having many test elements like test fragments, include controllers , beanshell samplers, ssh samplers, JDBC etc. When we tried running JMX script using Java code( below) include controller part of the script are getting skipped. We are running Test Fragments using include controllers by mentioning absolute as well as relative path. Please suggest us how to make test fragments inside JMX file using below Java code.

We have one JMeter script say xyz.jmx which is called from ABC.jmx jemeter script using include controller.

public class Test_INV_TI_001_XML extends BaseClass {
StandardJMeterEngine jmeter = new StandardJMeterEngine();
Summariser summer = null;
JMeterResultCollector results;

@Test()
public void INV_TI_001_XML() throws Exception {
    File JmxFile1 = new File("/Path/To/JMX/File/ABC.jmx");
    HashTree testPlanTree = SaveService.loadTree(JmxFile1);

    JMeterTreeModel treeModel = new JMeterTreeModel();
    JMeterTreeNode root = (JMeterTreeNode) treeModel.getRoot();
    treeModel.addSubTree(testPlanTree, root);

    SearchByClass<ReplaceableController> replaceableControllers =
            new SearchByClass<>(ReplaceableController.class);
    testPlanTree.traverse(replaceableControllers);
    Collection<ReplaceableController> replaceableControllersRes = replaceableControllers.getSearchResults();
    for (ReplaceableController replaceableController : replaceableControllersRes) {
        replaceableController.resolveReplacementSubTree(root);
    }
    HashTree clonedTree = JMeter.convertSubTree(testPlanTree,true);
    jmeter.configure(clonedTree);


    String summariserName = JMeterUtils.getPropDefault("summariser.name", "TestSummary");
    if (summariserName.length() > 0) {
        summer = new Summariser(summariserName);
    }
    results = new JMeterResultCollector(summer);
    testPlanTree.add(testPlanTree.getArray()[0], results);

    jmeter.runTest();

    while (jmeter.isActive())
    {
        System.out.println("StandardJMeterEngine is Active...");
        Thread.sleep(3000);
    }

    if (results.isFailure())
    {
        TestAutomationLogger.error("TEST FAILED");
        Assert.fail("Response Code: " + JMeterResultCollector.getResponseCode() + "\n" + "Response Message: " + JMeterResultCollector.getResponseMessage() + "\n" + "Response Data: " + JMeterResultCollector.getResponseData());
    }
}

}

When executing ABC.jmx script using above code, xyz.jmx which is called in ABC.jmx script should also get called and gets executed successfully.

1

1 Answers

1
votes
  1. If this is your full code it will not work as you need to call JMeterUtils.loadJMeterProperties() function
  2. It is also essential to call JMeterUtils.setJMeterHome() function
  3. Your .jmx and test fragment scripts are relative to what? Currently JMeter will look for them in the current user.dir folder, if the scripts live in JMeter's "bin" folder - you need to call FileServer.setBaseDir() function pointing to your ABC.jmx script and make sure that in the Include Controllers paths are relative to this ABC.jmx

Check out Five Ways To Launch a JMeter Test without Using the JMeter GUI article for more information on various ways of launching JMeter tests, most probably it would be easier to use i.e. Apache Maven for orchestrating your test execution, however you will be able to find some info on programmatic execution of JMeter tests as well.