'JMeter get specific node from testplan in java

In my JMeter test file i have multiple user-defined-variables-nodes and i would like to activate or deactivate them under specific circumstances. Therefore i need to get them as a JMeterTreeNode Object in Java. So instead of treeModel.getRoot(); i want something like treeModel.getNodeByName() so that my code could look somewhat like this:

JMeterTreeModel treeModel = new JMeterTreeModel(new Object());
JMeterTreeNode variablesNode= (JMeterTreeNode) treeModel.getNodeByName("jmeterNodeNameOrId");
if (true)        
{        
  variablesNode.setEnabled(true);
}


Solution 1:[1]

So, i found a working solution. Dmitris answer led me to take a look at the jmx as the xml. UserDefinedVariables is a "Arguments" node.

So first of all i had to get the Arguments Section

SearchByClass searcher = new SearchByClass(Arguments.class);
testPlanTree.traverse(searcher); //testPlanTree is a param
Object[] o = searcher.getSearchResults().toArray();
return o[0] //just the first one

In another Method i could use this like

JMeterTreeModel treeModel = new JMeterTreeModel(new Object());
JMeterTreeNode root = (JMeterTreeNode) treeModel.getRoot();
treeModel.addSubTree(testPlanTree, root);
            
JMeterTreeNode argumentNode = treeModel.getNodeOf(getArgumentsMethodFromAbove(testPlanTree));
String vars = argumentNode.getTestElement().getName();

And then you can do things like

if (vars.equals("foo"))
{
   argumentNode.setEnabled(false);
}

Solution 2:[2]

  1. You won't be able to "deactivate" a "node" in the runtime

  2. Moreover User Defined Variables are processed before any JSR223 Test Element so all of them will be "activated" long before your code starts executing.

    Note that all the UDV elements in a test plan - no matter where they are - are processed at the start.

The only way to "deactivate" the node is disabling it before the test starts, the most convenient way I can think of is Taurus automation framework which:

Solution 3:[3]

There are JMeter enhancements to add disabled/enable capabilities on runtime you can upvote or contribute to.

Bug 65702 - Add field disabled on all component

Or Bug 62495

Add option to disable View Results Tree/Listeners

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 dinkelbrot
Solution 2 Dmitri T
Solution 3