Writing a variable out to a .txt file

Would anyone have a code snippet on how to write code to write the contents of a variable out to a .txt file. then read the contents of the .txt file into another script when needed?

thanks very much

Can I ask why you have this requirement? If it’s just to pass vars between scripts, I’ve got a topic on the best way to do that:

This will be used to pass a URL to another test suite collection. I found a way with help from this forum to use excel, and write the URL to an excel sheet, which does work. However I need to write out to a simple .txt file rather than an excel sheet. This is due to restraints on our server which runs linux.

Use this as a starting point…

static void writeStringToFile(String filePath, String theString) {
  new File(filePath).withWriter("utf-8") { writer ->
1 Like

Russ, is the String(in blue) the actual string, in this case its a URL for example http://test.com ?
is the filepath the actual file path where the .txt file sits for example c://testurl

theString I assume is the file name?

Im getting an error saying: “unexpected token http”

Thanks for your help

Sorry Paul, I’m not quite sure what you’re asking.

This call will write your name to a file in your docs dir on Windows:

writeStringToFile("C:/users/paul/Documents/my-file.txt", "Paul Smith")

When inserting your code snipet

my script will fails with this error:

groovy.lang.MissingMethodException: No signature of method:

Paul, when you say something like this…

it’s fair to assume you would know what to do with such a “code snippet”.

What I gave you was a groovy method. Methods live inside classes. In Katalon, the easiest way to add classes is to use Custom Keywords:


Thanks for your help! I am not a developer, so Im still learning the coding aspect. Thanks for your code snipet(s) as it is extremely helpful as a jumping in point for me, I probably mis-worded my reply earlier, its not that the snipet did not work, I do not know the intracacies to get it to work. All help is always welcome, and im greatful for your timely replies.

1 Like

Well, you came to the right place - not that we can teach you to code, one-on-one, but if you put the effort in and post here when you hit a snag (we all do that, get used to it!) there’s usually someone around that can help. Start by reading that link I gave you, follow the examples - go ahead, surprise yourself.

Oh, and realize this - the only difference between you and an experienced developer is he has (I have) made more mistakes than you have. :sunglasses:

1 Like

Further Context:

my Goal is to write the contents of a Global.variable called simURL out to a .txt file, for use in subsequent Test Suite Collections.

I use the GetURL keyword and out put to a GlobalVariable called simURL.

I would like to write the contents of simURL out to a .txt file

I have been researching custom keywords, however Im not sure how to write the contents of a GlobalVariable that has been defined in the script out to a .txt file?

writeStringToFile("C:/users/paul/Documents/my-file.txt", GlobalVariable.simURL)

Thanks Russ, I figured it out, thanks very much for your help with custom key words, that was the ticket.
I refered to the groovy site in the “File IO” section for how to Write, and Read files. All scripts are now running.


thanks again for your help

1 Like


Obviously, that snippet was just meant to get the ball rolling. The fine detail is entirely your call. For example, I tend to write to my Katalon project folder. I create a subfolder…


And, depending on the file type, more subfolders within that…


The nice thing about doing it that way? They’re listed in the Tests Explorer inside Katalon Studio. :slight_smile: