Jmeter: How to save variables to a file after getting the data using a regular expression extractor

Sometimes we need to export the JMeter runtime variables to a external file, for example, csv, txt.
Now, I provide a example that how to the variables to a external file after getting the data using a regular expression extractor.
Expected that you knows how to get data using Regular Expression Extractor.

Variables of JMeter Runtime to save to external file

  1. Email: email_RegEx
  2. Token: token_RegEx

How to save the variables to external file

We can save the variables via BeanShell which is JMeter built-in component.

For more information about BeanShell, you can check it from JMeter official document and others technical documents, like BlazeMeter.

Where to add BeanShell step to export variable to a external file

  • Add BeanShell handler after Regular Expression Extractor

The tree of thread group similar as below:

Thread group
      |
      - Sampler
          |
          - Regular Expression Extractor
          - BeanShell
      - Other Samplers

Sample as my testing:

The tree of thread group

BeanShell Scripting

How to write BeanShell, please check the link below, in this article we will not relate to the scripting of BeanShell.

https://blazemeter.com/blog/queen-jmeters-built-componentshow-use-beanshell

BeanShell Script for saving variables to a external file

I just provide a sample that how to save the two variables that mention above to a external file.

Task list:

  • Save variables to a external file
  • Variables name: email_RegEx, token_RegEx
  • External file name: argList.csv
  • Output dirctory: /app/jmeter/test_data

Steps:

  1. Add a BeanShell PostProcessor after your Regular Expression Extractor.
    Named this processor as your prefered, no others set for the field of this processor.
BeanShell PostProcessor
  1. Scripting in script edit zone, and add the code below.

     // get variables from regular expression extractor 
     email = vars.get("uemail_RegEx");
     token = vars.get("token_RegEx");
    
     // pass true if want to append to existing file  
     // if want to overwrite, then don't pass the second argument
     FileWriter fstream = new FileWriter("/app/jmeter/test_data/argList.csv", true);
     BufferedWriter out = new BufferedWriter(fstream);
     out.write(email + "," + token);
     out.write(System.getProperty("line.separator"));
     out.close();
     fstream.close();
    
Script
  1. Save the JMeter script, and run.
  2. Goto output directory /app/jmeter/test_data, you will find that the output file named argList.csv was created, and open it, the variables export were all here.

Please note that, in statement email = vars.get("uemail_RegEx");, the variable uemail_RegEx is the variable name that regular expression extract the data save to.

Enjoy it.
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容