четвртак, 19. децембар 2013.

Extract value from XML response



How to extract value from XML response in Jmeter?


XPath Extractor allows us to extract value from structured response - XML - using XPath query language.

Let’s create a simple Xpath query which will extract the contentLink value from response bellow..

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<contentLinks xmlns="http://www.yourdomain.com/ckkl/content/contentupdate/1.0">
             <contentLink>
                        http://community.test.domain.com/content/csl/map/
             </contentLink>
</contentLinks>

We can extract value between tags "contentLink" using xpath querry: //contentLinks/contentLink and put in in variable URL.



 Add the XPath Extractor as child element of the HTTP Request:




уторак, 17. децембар 2013.

Passing variables between threads




JMeter variables have thread scope. However sometimes there is a need to pass variables between threads. I used two ways to share variables between threads: 

  1.  Set properties (one thread set a property, other thread read property) 
  2.  Export variables to file

Get the directory path of Jmeter script file






Bean Shell Sampler allows us to get the directory path of Jmeter script file.

Put this Java code in Bean Shell Sampler:

String dirPath = FileServer.getFileServer().getBaseDir();



Reading data from file using BSF Sampler


Reading data from file using BSF Sampler


If you derive data in run time, you can export them in run time and later read from that file using BSF Sampler.

How to read data from file Jmeter




Jmeter allows to read data from file on different ways. I will mention some of them I used in practice.


  • If you know data before starting test, it is recommended to save them in CSV file and read using CSV Data Set
  • Read from file using Bean Shell Sampler
  • Read from file using BSF sampler

How to export variables to file in Jmeter

 

 

Jmeter - Print variables to file


It is very useful to export some values to file in Jmeter scripting. It is especially useful if we need to share variables between different threads. Export variables to file can be achieved via scripting in Bean Shell Pre/Post Processor. You should use this if you derive data in run time. If you know data before starting the test, then it may be better to read it using CSV Dataset.

Here is the  Bean Shell script :

import org.apache.jmeter.services.FileServer;
import org.apache.jmeter.services.FileServer;

// Get Jmeter variable value in bean shell script by vars.get method
String sessionId= vars.get("sessionId") + ";";
log.info("sessionId:" + sessionId);

// Here we can get the directory path of Jmeter script file
String DirPath = FileServer.getFileServer().getBaseDir();

// we will create a file under directory of jmeter script file with name
importSessionID using File system True file will be created if not and data will //append into the file False will create a new file with fresh data
f = new FileOutputStream(FileServer.getFileServer().getBaseDir() + "\\importSessionID.txt", false);
p = new PrintStream(f);
// write data into file
p.println(sessionId);
p.close();
f.close();



In one thread, we can export variables to file. In the next thread, we can read variables from this file. 

понедељак, 25. новембар 2013.

How to Fail test from Baen Shell Sampler

How to Fail test from Java Code (Bean Shell Sampler)

Sometimes, we need to fail step from Bean Shell Samplers.

Lets see the next case:

If (A > B)
              Do Something
Else
              Failure 

 

How to do it? It is very easy. 

 

You can set up or use some of BeanShell variables:

  • log - the Logger
  • Label - the Sampler label
  • Parameters - text from the Parameters field
  • bsh.args - the parameters, split as described above
  • SampleResult - pointer to the current SampleResult 
  • ResponseMessage
  • ResponseCode
  • IsSuccess = true
  • ctx - JMeterContext
  • vars - JMeterVariables 
  • props - JMeterProperties (class java.util.Properties)- e.g. props.get("START.HMS"); props.put("PROP1","1234");
  • FileName - the file name, if any  
  •  

Set up IsSuccess variable to false and that's all.

петак, 22. новембар 2013.

How to use java code in jmeter


Bean Shell Sampler - Using Java Code in Jmeter



Bean Shell Sampler allows simple way to use java code in jmeter tests.
Suppose, we extracted value from JDBC response and put in in variable expires_on (the first picture) and we should process it using java code (the second picture). We can do it as it shown




By default, Jmeter does not show values of variables defined in Bean Shell Sampler in View Result Tree listener. If you need to debug your test, and monitor values of variables in Java Code, you can use this little trick. Add the next two lines in Bean Shell Sampler:


import org.apache.jmeter.samplers;
import org.apache.jmeter.samplers.SampleResult;

SampleResult.setResponseData(nonce_expiration_date1 );
SampleResult.setDataType( org.apache.jmeter.samplers.SampleResult.TEXT );



Now, in View Result Tree listener, you can see value of nonce_expiration_date1 variable.




понедељак, 14. октобар 2013.

How to Save Response Data in Jmeter

It is very useful sometimes to save response data to external file, especially when we run Jmeter tests with ANT or Maven.

It is very easy to export response data to external file. Jmeter offers Save Response to a File listener. Click on picture to enlarge.





By default, file is saved in bin directory.

четвртак, 10. октобар 2013.

How to use JMeter's 'IF' Controller


 

In this post I will explain you what should you do if you need to execute Jmeter HTTP Requests based on a specific condition.

It is very easy! Jmeter has If controller allowing to control whether its children are run or not.

Let's suppose our test plan look as on the picture:



Condition is shown in the picture bellow:




Child of If Controller ("Do Something") in this case, will be executed only when variable counter has value 1. This is very useful when we run test with many users, but some steps should not be executed for all users.

Use quotes when you compare strings. Example: "${your_variable}" == "jhsajdhjkashd"

 

среда, 9. октобар 2013.

How to extract value from JDBC response - Jmeter

Jmeter Example - How to extract value from JDBC response

In this post I will explain how to extract an element from the JDBC response using the Regular Expression Extractor and store it in a variable which can be referred from further requests.

1) Create JDBC request in your Jmeter Test Plan and enter your SQL querry. For example:

                 Select ID from USER where USERNAME='DRAGAN'
     


        
 JDBC Response should look as on the picture bellow:


 2) Add Regular Expression Extractor to JDBS requests as it shown on the picture bellow:


Value 45621 should be stored in the variable userId, and you can use this variable in furher requests.