Sending a JSON Parameter to Jmeter from pom-maven

Hello all,

Today I am going to post a quick snippet that I used recently and it quite made me spent a lot of time just because I didn’t read the Jmeter documentation in the first place 😅

In the pom.xml you will need to set some jmeter configurations:

 <?xml...    
 <project...
               <properties>                            
                  <jsonValue>[{"name": "Rafa", "age": 20},{"name": "Robin", "age": 5}]</jsonValue>                                     
               </properties>
...
<build>
   <plugins>
                 <plugin>
                   </configuration>
...
                        <propertiesUser>                            
                            <jsonValue>${jsonValue}</jsonValue>                                     
                        </propertiesUser>                                      
                   </configuration>
                </plugin>

How it should look like:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>1.0.0</modelVersion>

    <artifactId>jmeter-json-param-example</artifactId>
    <groupId>com.azevedorafaela.jmeter</groupId>
    <version>1.0</version>
    <packaging>pom</packaging>

    <properties>
        <host>www.google.co.uk</host>
        <jsonValue>[{"name": "Rafa", "age": 20},{"name": "Robin", "age": 5}]</jsonValue>
        <duration>30</duration>
        <threads>10</threads>
        <targetThroughput>1500</targetThroughput>
    </properties>

    <profiles>
        <profile>
            <id>performance</id>
            <build>
                <plugins>
                    <!-- execute JMeter test -->
                    <plugin>
                        <groupId>com.lazerycode.jmeter</groupId>
                        <artifactId>jmeter-maven-plugin</artifactId>
                        <version>2.9.0</version>
                        <executions>
                            <execution>
                                <id>test</id>
                                <goals>
                                    <goal>jmeter</goal>
                                </goals>
                            </execution>
                        </executions>
                        <configuration>
                           <generateReports>true</generateReports>
<ignoreResultsFailures>false</ignoreResultsFailures>
<testResultsTimestamp>false</testResultsTimestamp>
                            <propertiesUser>
                              <threads>${threads}</threads>
                              <targetThroughput>${targetThroughput}</targetThroughput>
                             <duration>${duration}</duration>
<!-- json parameter, host and port -->
                             <jsonValue>${jsonValue}</jsonValue>                                 
                             <server>${host}</server>
                            </propertiesUser>
                        </configuration>
                    </plugin>

                    <plugin>
                        <groupId>com.lazerycode.jmeter</groupId>
                        <artifactId>jmeter-analysis-maven-plugin</artifactId>
                        <version>1.0.6</version>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>analyze</goal>
                                </goals>
                                <phase>post-integration-test</phase>
                            </execution>
                        </executions>
                        <configuration>
                            <!--
                            source file that contains jmeter result data. Needs to be XML format or a GZIPed XML format
                            -->
<source>${project.build.directory}/results/*</source>
                        </configuration>
                    </plugin>

                </plugins>
            </build>
        </profile>
    </profiles>
</project>

On the Test Plan configuration you will need to set the parameters getting it from the pom.xml:

  • The default value for this parameter is: [{"name": "Rafa", "age": 20},{"name": "Robin", "age": 5}]
  • It is optional to set a default value in case it is not sent.
  • Note that we will need to escape the commas when adding the value to the jmeter script:
${__P(jsonValue,[{"name": "Rafa"\, "age": 20}\,{"name": "Robin"\, "age": 5}])}

Now you can run your jmeter tests passing a json code to the maven command line and if you don’t pass this parameter, then Jmeter is going to use the default one.

Resources:

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.