Hi,
I need to add global variable to my attachments. For example, in the line below,
I want to replace the version number 1.2.1.3 to global variable.
<a:ABC xmlns:a=“http://test.sss.aaa/mdr/nb/**1.2.1.3**/demo”>
I assume by “attachment” you mean request body. And it should work as you try. Make sure that you set the right profile for the variable. And, uh, it matches the case. You know “Version” vs. “version”
Other than that, you can also assign the global variable value to a request variable. In the Variables tab of the request object create a new variable, assign the “Global Variable” type to it and pick the desired global variable as the default value.
Let’s say you named this variable “version”. In that case, you would simply call it from inside the request body as ${version}.
That also gives you the benefit of renaming global variables more safely, as their name doesn’t change inside the request body automatically. And you could optionally set a different value to the request variable when you call the request from inside a test case.
Attachment is a .txt file that I’m reading in the request. In the text file there is a line with the version number. I need to change the version number to global variable.
I hope this clears the confusion about the attachment.
I don’t see anything about adding global variable to attachment(.txt files) in the ldocs.katalon.com link.
I’m sorry but i can’t attach my text file here.
If I understand correctly, you have a text file with a certain line in it containing a version number. This version number needs to change depending on your Global Variable
Something I would do to update the version number is to read the text file in Katalon studio and then update the version number and write back into the text file.
To do this I would use the following method:
Remember to include the import at the top of your script
import org.apache.commons.io.FileUtils
//Location of the Text File
File file = new File("C:\\testFolder\\testFile.txt")
//Set String Equal to contents of file
String text = FileUtils.readFileToString(file)
//Replace line with version number to version stored in Global Variable
String text = text.replaceAll('<a:ABC xmlns:a=“http://test.sss.aaa/mdr/nb/**' + '.*' + '**/demo">', '<a:ABC xmlns:a=“http://test.sss.aaa/mdr/nb/**' + GlobalVariable.version + '**/demo">')
//Write String back into text file
FileUtils.writeStringToFile(file, text)
Note that .replaceAll takes in the following parameters:
(String regex, String replacement)
So you can see that my first parameter looks for the line containing the text before and after the version number. You will see ‘.*’ between two strings which is a regular expression that means any characters, and this is where the version number is.
Thank you for your response. I have thought of this but as a last resort. Because there are close to 100 files, I don’t want to update each file all the time. My second option is to have two sets of attachments but again I don’t want to maintain multiple set of files.