2

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.

Simulant
  • 19,190
  • 8
  • 63
  • 98
Arnold
  • 181
  • 3
  • 12
  • 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: https://wiki.jenkins-ci.org/display/JENKINS/JMeter+Plugin – timbre timbre Apr 16 '17 at 22:03
  • 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 Apr 17 '17 at 00:20
  • @Corey Goldberg, OP question (running jmeter test from junit) makes sense, it's just too big to be answered – timbre timbre Apr 17 '17 at 01:26
  • 1
    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" – timbre timbre Apr 18 '17 at 16:39
  • Possible duplicate of [How to create and run Apache JMeter Test Scripts from a Java program?](http://stackoverflow.com/questions/19147235/how-to-create-and-run-apache-jmeter-test-scripts-from-a-java-program) – timbre timbre Apr 18 '17 at 16:40

1 Answers1

4

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.

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • thanks, this is exactly what I wanted! is there any way i can put an assertion against the results of the report as well, using the same API? for instance the test should pass if the total runtime is less than 2 seconds etc. – Arnold Apr 17 '17 at 09:25
  • In JMeter test plan you can use [Duration Assertion](http://jmeter.apache.org/usermanual/component_reference.html#Duration_Assertion), check out [How to Use JMeter Assertions in Three Easy Steps](https://www.blazemeter.com/blog/how-use-jmeter-assertions-3-easy-steps) for details. If you wrap JMeter-related code into JUnit or TestNG test you can do something like: `long before = System.currentTimeMillis(); //jmeter code long after = System.currentTimeMillis(); if ((after - before) > 2000) { Assert.fail("Duration of 2 seconds exceeded");}` – Dmitri T Apr 17 '17 at 14:29