Category Archives: JMETER

Jmeter – Share cookie across thread groups

Objectives:  Get cookie data from the request in first thread group, store it to csv file. so requests in other thread groups can use that cookie.

Requirement:

  • Create new csv file. It needs to be clean up before running test suite.
  • Extract cookie data from thread group , save it as variables and store them to csv file.
  • In other thread group, fetch data from csv file and save them as variables. Then Insert cookie to the request.

Solutions:

  1. create new csv file:

Manually create a new csv file. Eg : D:\DailyRun\Jmeter\cookievar.csv

  1. Clean up csv file before getting and writing cookie into it. Otherwise, multiple values will append to csv file and it causes wrong cookie inserted to requests in other groups.

Use BSF Sampler to execute the command: 

exec(“cmd.exe /C D:\\thachhoangngoc\\deleteCSV.bat”);

This bat file contains commands to copy fresh file and overwrite the old one.

echo Delete cookievar.csv

copy /Y D:\thachhoangngoc\cookievar.csv D:\DailyRun\Jmeter\cookievar.csv

(Actually, this is a work around. I couldn’t find a solution to clean up csv file directly in Jmeter. I tried method  file.clear() in Beanshell script but it didn’t work.)BSF Sampler

  1. extract cookie data from first request. cookie will be generated in first request. You can find cookie data in response header.

Use Regular Expression Extractor to extract cookie data and save it to variables. In this example, cookie data contains JSESSIONID and JSESSIONIDSSO. So I added two regular expression Extractor.Regular Expression Extractor1Regular Expression Extractor2

  1. write two cookie variables to csv file.

Use BeanShell PostProcessor. File path here is D:\DailyRun\Jmeter\cookievar.csv.beanshellPostProcessor

  1. fetch cookie data from csv file in other thread groups:CSV data set config

Insert cookie into request using  BeanShell PreProcessor.

 InsertCookieusing BeanshellPreProcessorTestPlanJMX