четвртак, 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.