Dynamic test data using Global Variable

Hi All,

I am wondering how to pass the test data from a Global Variable which increases it’s value by it’s own whenever I run the test case. E.g. I want to enter the username to be entered as

Test 1
Test 2
Test 3
.
.
.
Test n, each time I run the test case. How do I define that under Global variable?

I use only Global Variable to pass my test data and do not use excel or csv. Please help.

Thanks

I made a Test Case TC1:


import internal.GlobalVariable as GlobalVariable

// we assume GlobalVariable.SEQ is defined with initial value 0

println GlobalVariable.SEQ

GlobalVariable.SEQ += 1
println GlobalVariable.SEQ

GlobalVariable.SEQ += 1
println GlobalVariable.SEQ

When I ran TC1, I got:

2021-02-15 23:01:40.609 INFO  c.k.katalon.core.main.TestCaseExecutor   - START Test Cases/incrementGlobalVariable
2021-02-15 23:01:41.696 DEBUG testcase.incrementGlobalVariable         - 1: println(SEQ)
0
2021-02-15 23:01:41.891 DEBUG testcase.incrementGlobalVariable         - 2: SEQ += 1
2021-02-15 23:01:41.894 DEBUG testcase.incrementGlobalVariable         - 3: println(SEQ)
1
2021-02-15 23:01:41.912 DEBUG testcase.incrementGlobalVariable         - 4: SEQ += 1
2021-02-15 23:01:41.917 DEBUG testcase.incrementGlobalVariable         - 5: println(SEQ)
2
2021-02-15 23:01:41.932 INFO  c.k.katalon.core.main.TestCaseExecutor   - END Test Cases/incrementGlobalVariable

This shows that you can change the value of a GlobalVariable by script.

Thank you for the reply. The above example works if the Value Type is number but I want to concatenate with String. When I set the Value Type to string, I am geting the following output.

<
2021-02-18 16:01:32.552 INFO c.k.katalon.core.main.TestCaseExecutor - START Test Cases/New Test Case
2021-02-18 16:01:33.594 DEBUG testcase.New Test Case - 1: println(username)
test
2021-02-18 16:01:33.817 DEBUG testcase.New Test Case - 2: username += 1
2021-02-18 16:01:33.827 DEBUG testcase.New Test Case - 3: println(username)
test1
2021-02-18 16:01:33.842 DEBUG testcase.New Test Case - 4: username += 1
2021-02-18 16:01:33.847 DEBUG testcase.New Test Case - 5: println(username)
test11
2021-02-18 16:01:33.852 INFO c.k.katalon.core.main.TestCaseExecutor - END Test Cases/New Test Case

The username I create should be incremented with the numbers. Example, test+1@test.com
test+2@test.com

an so on. Also I want to store this value in a separate Global Variable. Please help.

Thanks

Can somebody help me on this please?

Thanks

Do you have some way of getting the last reference that you used? I can’t see any means of incrementing some reference unless you know what the last one was–was it 6 or 56. If you can get the last reference, then Split and Substring will allow you to add.

import internal.GlobalVariable as GlobalVariable

// we assume GlobalVariable.SEQ is like: Test 0056@Test.com

def temp = GlobalVariable.SEQ.toString().split("@")[0];
WebUI.comment("old reference  is: " + temp)

"old GV value; get the last FOUR characters/numbers in the GV"
def ref = temp[-4..-1].toInteger();
ref++;
WebUI.comment("new test data is: " + ref.toString())

def newRef = "000" + ref.toString();

"make new GV value"
GlobalVariable.SEQ = "Test "+ newRef[-4..-1] + "@Test.com";
WebUI.comment("new reference  is: " + GlobalVariable.SEQ)
Another way
import internal.GlobalVariable as GlobalVariable

// we assume GlobalVariable.SEQ is like: Test 56@Test.com

def temp = GlobalVariable.SEQ.toString().split("@")[0];
WebUI.comment("old reference  is: " + temp)

"old GV value; get the numbers in the GV starting in the 5th position"
def ref = temp[5].toInteger();
ref++;
WebUI.comment("new test data is: " + ref.toString())

"make new GV value"
GlobalVariable.SEQ = "Test "+ ref.toString() + "@Test.com";
WebUI.comment("new reference  is: " + GlobalVariable.SEQ)

Thank you. This one is working fine, but I want to update this GlobalVariable.SEQ in the execution profile during runtime. I have the GV set to “test0@test.com” and when I execute it I get “test1@test.com”. I want to update the GV with the value “test1@test.com”.

My suggestion would be to read the reference from a text file in the data folder rather than from the GV. Create a text file with “text0@test.com” at the top. Read the reference from the text file, manipulate the reference, write the new reference back out to the text file. Once you have read in the reference of the text file, equate it to your GV.

The alternative is to read and write the reference to Excel. That has been discussed many times on this forum. You need to persist the reference between runs and it does not look like GV are a means to do that.

This is based on the following:

My suggestion is to stop overthinking.
Katalon is already bloated.
So let us make it overcomplicated.

Thanks a lot @grylion54 for your help. For updating the GV permanently I implemented the keyword given in this post. :slight_smile: