I finally got my “Create Employee” test to pass, and I was so happy! But then I tried to run it again ten minutes later, and it failed immediately. The app showed an error saying “Employee ID 12345 already exists.” I realize now that because I typed “12345” into the setText box in my script, it’s trying to use that same ID every single time I run the test. I have to manually go into the script and change the ID to “12346” just to get it to pass again. This is going to be impossible if I want to run this test every day or as part of a big suite. How do I make Katalon “smart” enough to use a different ID every time so I don’t have to keep editing the code manually? Is there a way to just generate a random number?
Go to execution profile and create a global variable: employee
Then in your test case, use the code below:
String charSet = ‘abcdefghijklmno’
int length = 8
GlobalVariable.employee = (‘lastname’ + RandomStringUtils.random(length, charSet.toCharArray()))
WebUI.setText(findTestObject(‘your path/path1/path2’), GlobalVariable.employee)
hi @wazir
generate a random number at the start of your test and pass it into the setText call. Using a timestamp is the easiest way to guarantee uniqueness without collision.
String employeeId = System.currentTimeMillis().toString()
WebUI.setText(findTestObject('Your/Page/Input_EmployeeID'), employeeId)
if you need it to look more like your original format, use Random to generate a number in a specific range.
String employeeId = (10000 + new Random().nextInt(89999)).toString()
WebUI.setText(findTestObject('Your/Page/Input_EmployeeID'), employeeId)
if you need to reuse that ID across multiple test cases in a suite, then store it in a GlobalVariable after generating it.
Duplicate Employee ID blocks re-runs—generate unique ID at script start.
Inline Fix
import java.util.Random
String empId = "EMP" + (10000 + new Random().nextInt(90000)) // EMP12345-99999
WebUI.setText(empIdField, empId)
Or timestamp (guaranteed unique):
String empId = "EMP" + System.currentTimeMillis() // EMP1714234567890
Global Reuse
Profiles > default.glbl → Add employeeId var.
Script:
GlobalVariable.employeeId = "EMP" + (10000 + new Random().nextInt(90000))
Next tests: Use GlobalVariable.employeeId
Verify: println empId before submit. Clean DB post-test if needed
To solve this, we move away from hardcoded values and use Dynamic Data Generation. Usually, we use a unique timestamp or a Random UUID (Universally Unique Identifier). This ensures that every time the script hits that “ID” field, it generates a value that has never existed before.
The Solution: Timestamp or Randomization Logic
The simplest industry standard is to append the current date and time (down to the second) to a string. For example, Employee_202604281030 will always be unique.
Custom Keyword Helper: The Unique ID Generator
This keyword allows you to generate a fresh ID or Email address whenever you need one, ensuring your tests never collide with old data.
import com.kms.katalon.core.annotation.Keyword
import java.text.SimpleDateFormat
class DataGenerator {
/**
* Generates a unique string based on the current timestamp
* Example output: EMP-202604280152
* @param prefix The text you want before the number (e.g., "EMP-")
*/
@Keyword
def generateUniqueId(String prefix) {
// Create a date format: YearMonthDayHourMinuteSecond
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss")
String timestamp = sdf.format(new Date())
return prefix + timestamp
}
/**
* Generates a random email address
*/
@Keyword
def generateRandomEmail() {
return "testuser_" + System.currentTimeMillis() + "@example.com"
}
}
How to use it in your Script:
Instead of typing a fixed number in your test, call the generator and store it in a variable:
// 1. Generate the unique ID
def newID = CustomKeywords.'DataGenerator.generateUniqueId'("ST-")
// 2. Use that variable in your test steps
WebUI.setText(findTestObject('Object Repository/Input_EmployeeID'), newID)
// 3. (Optional) You can use 'newID' later to search for the record you just created!
WebUI.setText(findTestObject('Object Repository/Search_Box'), newID)
If you hardcode your id like below
WebUI.setText(testobeject(‘EMPID’), ‘12345’) then it will surely enter 12345 only every time.
So better generate the random iD every time
int randomID = new Random().nextInt(90000) + 10000 // This will generate a random number between 0 and 89999
WebUI.setText(testobejct(‘EMPID’), randomID.tostring())
To solve this, we move away from hardcoded values and use Dynamic Data Generation. Usually, we use a unique timestamp or a Random UUID (Universally Unique Identifier). This ensures that every time the script hits that “ID” field, it generates a value that has never existed before.
The Solution: Timestamp or Randomization Logic
The simplest industry standard is to append the current date and time (down to the second) to a string. For example, Employee_202604281030 will always be unique.
Custom Keyword Helper: The Unique ID Generator
This keyword allows you to generate a fresh ID or Email address whenever you need one, ensuring your tests never collide with old data.
import com.kms.katalon.core.annotation.Keyword
import java.text.SimpleDateFormat
class DataGenerator {
*/\*\*
\* Generates a unique string based on the current timestamp
\* Example output: EMP-202604280152
\* @param prefix The text you want before the number (e.g., "EMP-")
\*/*
*@Keyword*
def generateUniqueId(String prefix) {
*// Create a date format: YearMonthDayHourMinuteSecond*
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss")
String timestamp = sdf.format(new Date())
return prefix + timestamp
}
*/\*\*
\* Generates a random email address
\*/*
*@Keyword*
def generateRandomEmail() {
return "testuser\_" + System.currentTimeMillis() + "@example.com"
}
}
How to use it in your Script:
Instead of typing a fixed number in your test, call the generator and store it in a variable:
// 1. Generate the unique ID
def newID = CustomKeywords.‘DataGenerator.generateUniqueId’(“ST-”)
// 2. Use that variable in your test steps
WebUI.setText(findTestObject(‘Object Repository/Input_EmployeeID’), newID)
// 3. (Optional) You can use ‘newID’ later to search for the record you just created!
WebUI.setText(findTestObject(‘Object Repository/Search_Box’), newID)
When your test ran last time,you added the Employee ID 12345. It made the database of the target system dirty. If you repeat creating new Employee IDs many times, then it is likely that the tareget system acts undetarministic, which would be hard to “test”.
A dirty target moves. Moving target is always difficult to test. You should create a stable “test fixture” that does not change over time.
What does the term “test fixture” mean? — See Test fixture - Wikipedia
You should rather clean up the target system’s database with a stable fixture before you run your test next time.
If you could set up the target system clean, then you would be able to apply the fixed set of testing script without one-off edits.
How you can clean up your database? — I don’t know. It is completely up to you.
Typically, you would want to execute Database Backup and Restore. Stop/restart the target web app may be required. This would deserve a CI/CD pipleline processing.
Is it too big job for you? Then, does the target system allows you to retrieve if the Employee ID 12345 is present or not (boolean)? Does it allow you to remove the existing Emplyee ID 12345 safely? If the target system allows you to do clean up, then you can write a script to remove the existing Employee ID 12345 in the Test Listener
The others have given you the main idea to use a variable instead of using a ‘hard-coded’ value–which is an excellent idea. However, over time, even with the random generation of values, you may encounter a situation that you again repeat an employee id that already exists. So, what I would suggest is to use a ‘while’ statement surrounding your formula that generates your ‘id’ and breaks out if the error message does or does not occur.
boolean isNewClient = false
while (isNewClient == false)
{
// create your new client id
// blah blah blah
WebUI.waitForPageLoad(10)
WebUI.delay(1.5)
if (WebUI.verifyTextNotPresent('Employee ID .* already exists.', true)) {
isNewClient = true
}
}
Edit: I’m using a wild card RegEx to compare your id number, but you can also more closely use other digit replacement that is in your id’s specific format to better compare, such as \\d{5-7} or \\s\\d{5-7}\\s.
e.g.
if (WebUI.verifyTextNotPresent('Employee ID\\s\\d{5-7}\\salready exists.', true)) {
Stop hardcoding. The fix is to use a Timestamp.
Hardcoded IDs fail because the database already has them. Instead of typing “12345,” use a small piece of code to grab the current system time in milliseconds. Since time never goes backward, the number will be unique every single time you hit “Run.”
The logic: WebUI.setText(findTestObject('your/object'), System.currentTimeMillis().toString())
This turns your script into a “fire and forget” tool. You’ll never have to manually update an ID again, and your tests won’t collide with old data.
@wazir did you try the solutions listed? worked?