Is there any way to integrate a recorded jmeter script( which tests the load exerted by a recorded series of actions executed by multiple users) in a selenium/junit test case). such that i just execute that selenium/junit test case with java, and it gives me the performance results of the junit report? I have found posts telling how to integrate selenium webdriver into jmeter, but not the other way around.
2
votes
Have never heard of such thing, but generally it's possible with JMeter API and JUnit Runner. Not a small work. I would suggest to use Jenkins plug-in instead: wiki.jenkins-ci.org/display/JENKINS/JMeter+Plugin
– Kiril S.
can you clarify what you are asking? your question as originally written makes absolutely no sense. jmeter and selenium are 2 different tools used for very different things, and junit is just a testing framework.. what exactly are you trying to do?
– Corey Goldberg
@Corey Goldberg, OP question (running jmeter test from junit) makes sense, it's just too big to be answered
– Kiril S.
if accepted answer is what you wanted, then the question is not correct. This solution has nothing to do with "integrating jmeter test into junit"
– Kiril S.
Possible duplicate of How to create and run Apache JMeter Test Scripts from a Java program?
– Kiril S.
1 Answers
3
votes
You can execute existing JMeter test using JMeter Java API, example code would look like:
import org.apache.jmeter.engine.StandardJMeterEngine;
import org.apache.jmeter.reporters.ResultCollector;
import org.apache.jmeter.reporters.Summariser;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.collections.HashTree;
import java.io.File;
public class JMeterFromCode {
public static void main(String[] args) throws Exception {
// JMeter Engine
StandardJMeterEngine jmeter = new StandardJMeterEngine();
// Initialize Properties, logging, locale, etc.
JMeterUtils.loadJMeterProperties("/tmp/jmeter/bin/jmeter.properties");
JMeterUtils.setJMeterHome("/tmp/jmeter");
JMeterUtils.initLogging();// you can comment this line out to see extra log messages of i.e. DEBUG level
JMeterUtils.initLocale();
// Initialize JMeter SaveService
SaveService.loadProperties();
// Load Test Plan
HashTree testPlanTree = SaveService.loadTree(new File("/tmp/jmeter/bin/test.jmx"));
Summariser summer = null;
String summariserName = JMeterUtils.getPropDefault("summariser.name", "summary");
if (summariserName.length() > 0) {
summer = new Summariser(summariserName);
}
// Store execution results into a .jtl file
String logFile = "/tmp/jmeter/bin/test.jtl";
ResultCollector logger = new ResultCollector(summer);
logger.setFilename(logFile);
testPlanTree.add(testPlanTree.getArray()[0], logger);
// Run JMeter Test
jmeter.configure(testPlanTree);
jmeter.run();
}
}
Replace:
- all occurrences of
/tmp/jmeter
- with path to your JMeter installation /tmp/jmeter/bin/test.jmx
- with path to the .jmx file, containing the recorded JMeter script/tmp/jmeter/bin/test.jtl
- with the desired location of the .jtl results file
See Five Ways To Launch a JMeter Test without Using the JMeter GUI article for more information on the possibilities of executing a JMeter test, maybe you will find an easier integration solution as JMeter tests can be executed via Maven plugin or Ant Task along with Selenium tests.