Friday, March 8, 2013

How to Run Jmeter test script(.jmx) using WSO2 Test Automation Framework

Most of time Jmeter tests are executed from Jmeter tool GUI. Once you want to execute a test script without Jmeter GUI, There is a option called Jmeter command-line arguments or you can find some maven plugging to do it.

 Test Automation Framework also provide the ability to execute the Jmeter test without GUI. This post help you to run Jmeter script programatically. Test Automation framework look after underline Jmeter test execution. It use the Jmeter command-line options to execute the test and initialize a jmeter in the target directory. No Jmeter installation is required to execute the test.

I have a jmeter script which tests the service list page of the WSO2 ESB. Script contains following requests

1) Go to login page
2) Authenticate using username and password
3) Go to List Services page
4) SingOut
 
Create a maven project with bellow dependency
<dependencies>  
     <dependency>  
       <groupId>org.testng</groupId>  
       <artifactId>testng</artifactId>  
       <version>6.1.1</version>  
     </dependency>  
     <dependency>  
       <groupId>org.wso2.carbon.automation</groupId>  
       <artifactId>org.wso2.carbon.automation.tools.jmeter</artifactId>  
       <version>4.0.8</version>  
     </dependency>  
   </dependencies>  

Create a testng test class to execute the test. You need to point out your .jmx file path
package com.test;  
 import org.testng.annotations.Test;  
 import org.wso2.automation.tools.jmeter.JMeterTest;  
 import org.wso2.automation.tools.jmeter.JMeterTestManager;  
 import java.io.File;  
 public class JMeterTestToListServicesTestCase {  
   @Test()  
   public void listServices() throws Exception {  
     JMeterTest script = new JMeterTest(  
         new File("/home/nuwanw/projects/JmeterIntegrationWithFramework" +  
              "/src/test/resource/jmeter/ESB-list-service.jmx"));  
     JMeterTestManager manager = new JMeterTestManager();  
     manager.runTest(script);  
   }  
 }  

Then you have added a test to execute the jmeter test.
Add following surefire configuration in the pom file to run test while building the project

<build>  
     <plugins>  
       <plugin>  
         <artifactId>maven-surefire-plugin</artifactId>  
         <version>2.12.3</version>  
         <inherited>false</inherited>  
         <configuration>  
           <argLine>-Xms512m -Xmx1024m -XX:MaxPermSize=128m</argLine>  
           <testFailureIgnore>true</testFailureIgnore>  
           <disableXmlReport>false</disableXmlReport>  
           <parallel>false</parallel>  
           <includes>  
             <include>**/*TestCase.java</include>  
           </includes>  
         </configuration>  
       </plugin>  
     </plugins>  
   </build>  

You can see the test result after building the project if thee is no failures
Results :  
 Tests run: 1, Failures: 0, Errors: 0, Skipped: 0  

Jmeter test executor generate a separate log file and report(.jtl) file for each and every test script executed. If you need any Further Result, go to target directory. Then there is a directory called jmeter. under jmeter directory you can find logs and reports directories. inside those directories you can see a .log files and .jtl files. log and report files are generated from you test script name + timeStamp 

Now you can execute the Jmeter test which ensure a functionality of a product with proper assertions with the product integration test while building the product.