Change a global variable value permanently

Hello Katalon Community, new user here!

I am wondering if it is possible to change a global variable value permanently with groovy.

When I set GlobalVariable.variable1 = “value” it does not change its value in the profile, instead it only change its value while the test case is running.

Is there a way to permanently change a global variable value?

Thanks!

1 Like

The values stored in the profile are best viewed as “starting values”. When a test runs, you can modify them (as you have discovered). However, when the next test runs, they start with the starting values again.

That’s how they work.

If you want TestCase2 to use a value modified by TestCase1, then call TestCase2 from TestCase1 using callTestCase.

1 Like

Hey @Russ_Thomas thanks for your answer.

I figure out a time ago how to do this inside Soap UI and I think it worked as the best solution for my usecase needs: I can login one time, with one test case, store all the essential authentication credentials and then use those same credentials on later test cases.

I think Katalon Studio is a more friendly choice to non-programmers, that’s why I trying to replicate what I did in Soap UI into Katalon.

I work with very complex API systems that needs a custom authentication even for just testing.

I think there is a way to do this in Soap UI isn’t? If not, I will try to store values outside instead. What you think?

I’m not sure I can advise in a meaningful way. However…

I find that many “globals” are better stored outside of the Katalon, in a separate file. In my case, I store a bunch of global things in a json file which I read in before the tests/suite are executed. I have different execution properties for different profiles all of which are stored in the json file.

Don’t know if that helps you decide?

Hi @Russ_Thomas

I want to use a value calculated in TestScript1 and use it in TestScript2. Is there a way to do this without calling TestScript2 within TestScript1. The reason I can not use this approach is b/c TestScript1 happens before a version upgrade and TestScript2 happens after the version upgrade. I will give my specific case below.

To recreate.

  1. Global variable documentSignOffDateAndTimeForPretestStep14 is created and set to " " value.

  2. TestScript1 execution starts.

  3. In TestScript1, the following calculation is done.
    GlobalVariable.documentSignOffDateAndTimeForPretestStep14 = WebUI.getText(findTestObject(‘Page_ACE - DP-2 Second Uploaded Doc/Date and time element positon for Pretest Step 14’))

  4. TestScript1 execution ends.

  5. Software upgrade occurs.

  6. TestScript2 execution begins.

  7. In TestScript2, the following is executed.
    WebUI.verifyTextPresent(GlobalVariable.documentSignOffDateAndTimeForPretestStep14, false)


In step 7, I would like to use the value calculated in Step 3. Is there anyway to do that?

Thank you for your time. It is much appreciated.

Step7 looks nonsence:

Is the following line what you intended?

def v = WebUI.getText(findTestObject(‘Page_ACE - DP-2 Second Uploaded Doc/Date and time element positon for Pretest Step 14’))
assert v == "false"

Why does Step 7 look like nonsense? I am verifying if the string value in the global variable ‘documentSignOffDateAndTimeForPretestStep14’ exists on the page. The step executes correctly but it does not use the value in Step 3; it uses the value in Step 1. I am a newbie to katalon studio; please excuse my lack of knowledge.


The following

"def v = WebUI.getText(findTestObject(‘Page_ACE - DP-2 Second Uploaded Doc/Date and time element positon for Pretest Step 14’))
assert v == “false” "

is similar to what I intended except I used a global variable instead of “def v”; which I think would be considered a local variable?
And I am not sure why you have “assert v == false”? In my step 3, the returned value should be a string; not a boolean?

Thank you for your time.

Sorry, I misunderstood.

Do you have a Test Suite which executes step1, 2, 3, 4, (5?), 6 and 7 in a squence?
Or do you run each step indivisually by clicking “run test case” button?

I run them individually by clicking the “run test case” button. I execute the portion of TestScript1 where the get.Text function executes. Then TestScript1 finishes. Then I execute a portion of TestScript2 containing the verifyTextPresent with the “Run from here” function.

OK.

Before writing my proposal, let me ask a question.

What do you mean “Software” here?

Do you mean:

  1. you will stop your Web server. The URL will become unavailable then.
  2. you will install new application to the Web server
  3. you will restart your Web server, and wait for the URL to become available again

The Web application under test upgrades to the next version. For example, say we are testing the application Excel Version 1.0. We input data in Excel Version 1.0. Then we upgrade the Excel software application to Excel Version 2.0. That is what I mean by software upgrade. Let me know if more clarification is needed (insert thumbs up).

Opps, Excel is not a web application. Microsoft outlook would be a better example.

Why you set blank value to the GLobalVariable as initial state?
Can’t you type the appropriate value manually?
Does the value changes frequently? or too long too type manually?

Why not your TestScript2 do the same retrieval as TestScript1 does? The retrieval is just one line:

def v = WebUI.getText(findTestObject(‘Page_ACE - DP-2 Second Uploaded Doc/Date and time element positon for Pretest Step 14’))

The value changes each time the test script is run.

I can not do the same retrieval as TestScript1 because TestScript2 occurs after the web application version upgrade. I am verifying the time that an action took place while the application was Version 1.0 is recorded correctly in the System Log after the application upgrades to Version 2.0.

Step 3 is a get text action and Step 7 is a verify text present action. Not sure if there was some confusion there.

vweerasinghe,

If you have a TestSuite which contains your TestScript1 and TestScirpt2 together, then you can pass data from TestScript1 to TestScrit2 via GlobalVariable.documentSignOffDateAndTimeForPretestStep14. TestScript1 updates it , TestScrit2 reads it. This means that a GlobalVariable is scoped TestSuite-wide.

But you do not have a TestSuite. You execute TestSuite1 to end. After that you execute TestSuite2 to end. In this scinario, GlobalVariable.documentSignOffDateAndTimeForPretestStep14 in TestScript1 and GlobalVariable.documentSignOffDateAndTimeForPretestStep14 in TestScript2 are 2 independent entities. You can not pass anything via the GlobalVariable.

Then how to pass stuff?

Your TestScript1 should store the value of WebUI.getText(findTestObject(‘Page_ACE - DP-2 Second Uploaded Doc/Date and time element positon for Pretest Step 14’) into a file. And Your TestScrip2 should read the file to retrieve the value.

Please try the following set of test case codes. These worked for me.

TestScript1

import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths

import com.kms.katalon.core.configuration.RunConfiguration
import com.kms.katalon.core.testobject.ConditionType
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

// parameters
def URL = "https://www.google.com/search?q=katalon"
def testObject = new TestObject().addProperty("xpath", ConditionType.EQUALS, "//div[@id='resultStats']")

// create directory to locate a temporary file
Path projectDir = Paths.get(RunConfiguration.getProjectDir())
Path tmpDir = projectDir.resolve('tmp')
if (!Files.exists(tmpDir)) {
	Files.createDirectory(tmpDir)
}

// Prepare File object 
File dataFile = tmpDir.resolve('data.txt').toFile()

// open a web page
WebUI.openBrowser('')
WebUI.navigateToUrl(URL)
WebUI.verifyElementPresent(testObject, 10)

// retrieve a text labeled "result stats" from the Google Search Result page 
String value = WebUI.getText(testObject).trim()

// save the text into a file under <project dir>/tmp directory
dataFile.text = value

WebUI.closeBrowser()

TestScript2

import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths

import com.kms.katalon.core.configuration.RunConfiguration
import com.kms.katalon.core.testobject.ConditionType
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

// parameters
def URL = "https://www.google.com/search?q=katalon"
def testObject = new TestObject().addProperty("xpath", ConditionType.EQUALS, "//div[@id='resultStats']")

// create directory to locate a temporary file
Path projectDir = Paths.get(RunConfiguration.getProjectDir())
Path tmpDir = projectDir.resolve('tmp')
if (!Files.exists(tmpDir)) {
	Files.createDirectory(tmpDir)
}

// read the file to retrieve the data prepared by TestScript1
File dataFile = tmpDir.resolve('data.txt').toFile()
String previousValue = dataFile.text.trim()

// open a web page
WebUI.openBrowser('')
WebUI.navigateToUrl(URL)
WebUI.verifyElementPresent(testObject, 10)

// verify if the same value as the data is displayed in the web page

//WebUI.verifyTextPresent(value, false)        // this line often fails with no meaningful diagnostics 

def currentValue = WebUI.getText(testObject).trim()
assert currentValue == previousValue

WebUI.closeBrowser()

2 Likes

Thanks @kazurayam !

It worked for me :+1:t5:

Hi
In my test case 1 if i am creating a booking and a booking id is getting generated
can it pass the booking id automatically to test case 2
Can some one help me out in this?
how can i do that?