如何使用Java API在Jmeter项目中添加RecordingController?

时间:2018-03-01 05:23:02

标签: jmeter jmeter-plugins jmeter-3.2 jmeter-4.0

如何使用Java API在Jmeter项目中添加 NonTestElement 代理记录控制器?我试图在THIS GITHUB PROJECT中这样做,但它无效。我能够添加一个RecordingController,它是一个存储记录项目的容器,但是我在将非测试记录元素添加到测试计划根元素时遇到了问题。当我尝试添加代理控制器时,它显示为常规目标RecordingController,这不是我的意图。

我正在使用Jmeter 4.0:

// this RecordingController works fine
RecordingController recordingController = new RecordingController();
recordingController.setName("Recordings");
recordingController.setProperty(TestElement.TEST_CLASS, RecordController.class.getName());
recordingController.setProperty(TestElement.GUI_CLASS, TestPlanGui.class.getName());

// this non-test ProxyControl is the one i cant get working
ProxyControl proxyController = new ProxyControl();
proxyController.setName("Proxy Recorder");
proxyController.setProperty(TestElement.TEST_CLASS, ProxyControl.class.getName());
proxyController.setProperty(TestElement.GUI_CLASS, TestPlanGui.class.getName());

ThreadGroup threadGroup = new ThreadGroup();
threadGroup.setName("Sample Thread Group");
....

TestPlan testPlan = new TestPlan("Create JMeter Script From Java Code");
testPlan.setProperty(TestElement.TEST_CLASS, TestPlan.class.getName());
testPlan.setProperty(TestElement.GUI_CLASS, TestPlanGui.class.getName());
testPlan.setUserDefinedVariables((Arguments) new ArgumentsPanel().createTestElement());

testPlanTree.add(testPlan);
testPlanTree.add(proxyController);

HashTree threadGroupHashTree = testPlanTree.add(testPlan, threadGroup);
threadGroupHashTree.add(recordingController);

注意:当我寻找答案时,我通过在Jmeter 4.0中打开生成的.jmx项目文件(从此Java代码生成;请参阅我的项目的链接)来验证这实际上是有效的。如果这不起作用,那么我不认为这个问题得到了回答。

1 个答案:

答案 0 :(得分:1)

您的代码无效,使用正确的GUI类添加代理控制器的正确方法proxyController.setProperty(TestElement.GUI_CLASS, ProxyControlGui.class.getName());

以下是基于您的代码的工作示例:

    RecordingController recordingController = new RecordingController();
    recordingController.setName("Recordings");
    recordingController.setProperty(TestElement.TEST_CLASS, RecordController.class.getName());
    recordingController.setProperty(TestElement.GUI_CLASS, RecordController.class.getName());

    ProxyControl proxyController = new ProxyControl();
    proxyController.setName("Proxy Recorder");
    proxyController.setProperty(TestElement.TEST_CLASS, ProxyControl.class.getName());
    proxyController.setProperty(TestElement.GUI_CLASS, ProxyControlGui.class.getName());

    ThreadGroup threadGroup = new ThreadGroup();
    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();
    // Thread Group

    threadGroup.setName("Sample 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());

    TestPlan testPlan = new TestPlan("Create JMeter Script From Java Code");
    testPlan.setProperty(TestElement.TEST_CLASS, TestPlan.class.getName());
    testPlan.setProperty(TestElement.GUI_CLASS, TestPlanGui.class.getName());
    testPlan.setUserDefinedVariables((Arguments) new ArgumentsPanel().createTestElement());
    testPlan.addTestElement(proxyController);
    HashTree testPlanTree = new HashTree();
    testPlanTree.add(testPlan);

    HashTree threadGroupHashTree = testPlanTree.add(testPlan, threadGroup);
    threadGroupHashTree.add(recordingController);