How to add global variable to attachments?

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 tried the following but received an error.
<a:ABC xmlns:a=“http://test.sss.aaa/mdr/nb/**${GlobalVariable.Version}**/demo”>

processing error: Error parsing attachment: cvc-elt.1.a: Cannot find the declaration of element 'a:ABC'.

Thanks

Hi,

Has this line of code worked yet? The error is that it cannot find your declaration of a:ABC

The line I have provided here is just a sample. I need to know how to change 1.2.1.3 to global variable.
Thanks

hi,

is this something you will

def  s1 = "<a:ABC xmlns:a'http://test.sss.aaa/mdr/nb/**1.2.1.3**/demo'>"

def user =  GlobalVariable.user

def s2 = "<a:ABC xmlns:a\"http://test.sss.aaa/mdr/nb/**"+user+"**/demo\">"
print s2

I tried have tried this already, this doesn’t work in attachments.

What do you mean by the word attachments?

Though it could be perfectly obvious to you what “attachments” means, others don’t understand you yet.

Have a look at the guidance:

1 Like

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.

Thanks

The following section of Katalon Documentation describes how to use GlobalVariable in HTTP Request for RESTful API.

Does this doc satisifies you? or not? If not, please describe why.

@mqamar kindly re-read and understood what is written there.
as far as i know, to accept this is mandatory for any existing members and new ones:

simply clicking ‘i accept’ does not help you at all

2 Likes

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.

Thanks

Hi @mqamar

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.

<a:ABC xmlns:a=“http://test.sss.aaa/mdr/nb/ + ‘.*’ + '/demo">

I then replace that particular line with the exact same text, but where the ‘.*’ is, is the version number which is stored in a Global Variable

NB: I suggest making a backup of the file before attempting

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.

Thanks