0

Is it possible to write data to a file inside a JSR223 Assertion using javascript as the language? How can it be done?

Rafael Colucci
  • 5,712
  • 4
  • 45
  • 114

2 Answers2

6

It is somehow possible.

Due to security reasons you cannot access file system in JavaScript, but according to JSR223 specification is is not only possible to call scripts from Java but you can do the opposite thing - call Java from scripts, in your case from JavaScript. The relevant code would be:

var writer = new java.io.PrintWriter('your_file.txt')
writer.write('hello')
writer.close()

Reference material and code snippets:

Dmitri T
  • 119,313
  • 3
  • 56
  • 104
1

I'd rather use Groovy (and check cache checkbox) to follow best-practices:

It's just this:

import org.apache.commons.io.FileUtils;
FileUtils.writeStringToFile(new java.io.File("/data/jmeter/test.log"), 
     "String to append", 
     "UTF-8", 
      true);

enter image description here

UBIK LOAD PACK
  • 31,356
  • 4
  • 58
  • 100
  • I would like to use Groovy, but I know nothing about the language and my javascript is quite complex. But thank you anyway! – Rafael Colucci Dec 02 '16 at 11:56
  • I've found that groovy scripts don't work well with Windows paths that have backslashes in them. – user2023861 Aug 23 '19 at 18:48
  • Don't use backslashes then, use forward slashes. I've found in most cases Windows is perfectly happy with c:/temp/jmeter/bin/ and so on. And it makes coding easier. – Slimy43 Feb 07 '20 at 08:25