My test works perfectly the first time, but fails every time after that!

Hey everyone, I’m pretty new to Katalon Studio and automation in general, and I’ve run into a roadblock that’s driving me crazy.

I recorded a test case to create a ‘New Employee’ record. The first time I ran it, it worked beautifully! The green bar made me so happy. But when I tried to run it a second time to show my teammate, the test failed. It looks like the Employee ID (or email/username) I used the first time is now saved in the system, so the application throws a “Duplicate ID / Record Already Exists” error message.

What I’ve tried so far:

  • I tried changing the ID manually in the Manual View of the Test Case, but that means I can only run it once before I have to change it again. That defeats the whole point of automation, right?

  • I tried deleting the employee from the UI at the end of the test, but sometimes the test fails halfway through, and the “delete” step never gets reached, leaving the bad data behind anyway.

I just want a way to make this test completely repeatable without me having to manually edit the test data every single time I hit the “Run” button. How do beginners handle this?

We just had a discussion about this just a couple of weeks ago on this forum so you can look for that post for assistance. Then, the suggestion was to create a variable and then create a “New Employee” based on either a random set of characters or on a date. You could put the whole into a method that returned your new employee reference.

I personally use an incrementing counter onto a default name, like “TestmanxxA3”, where the xx increments. I have the whole in a “while” method that starts at 1 and if the “person” does not exist, I use that. If the person does exist, I increment my counter and check again. I only needed something simple.

This type of similar topic got already discussed, please check the link below, may be this helpful to you

Similar type of topic got already discussed, please check the below link may be this help you.

Alternatively, you should do the following dual steps:

  1. At the start of the test, check if the employee ID is already there or not
  2. If already there, delete the ID.

then your test should be able to continue as coded now.

Test Listener is the appropriate place to implement a processing of setting up the test fixtures.

If the validation is on email/username then you can create a random username/email each time you execute the test case.

You can build this by keeping some part common and then appending some random characters (alphabets/numbers whatever is allowed for your app)

Ex. def email = “autoemail”+CustomKeywords.‘random_generators.random_string_generator’(4)+“@gmail.com”

The common keyword would look like

	@Keyword
	def random_string_generator(int length) {

		def charset = (('a'..'z')+ ('A'..'Z') ).join()
		String randomString = RandomStringUtils.randomAlphabetic(length)

		return randomString

	}

Whether now your problem got fixed?

One simple way to make this repeatable is to stop using a fixed Employee ID in the recorded step. The recording is fine for getting started, but test data usually needs to be dynamic.

For example, you can add a timestamp or random number to the ID/email each time the test runs. Something like employee_${System.currentTimeMillis()} is usually enough for a beginner setup, and it avoids the duplicate record problem without manually editing the test.

Cleanup is still useful, but I wouldn’t rely on it as the only fix because, like you said, the test can fail before it reaches the delete step. A unique test user per run is safer, then you can clean old test records later if needed.

Your test script is currently using hardcoded data. In automation, hardcoding unique identifiers causes your tests to rely on a specific state of the application’s database. Once that state changes (i.e., the row is inserted), the test environment is altered, causing subsequent runs to fail.

The Solution: Dynamic Data Generation

The industry-standard solution is to ensure your test creates unique data on the fly during every execution. Instead of typing EMP123 or john.doe@company.com, we can append a dynamic timestamp or a random number to the data. This guarantees that every single test run uses a brand-new, unique value.

Custom Keyword Solution

In Katalon Studio, the cleanest way to handle this is by creating a Custom Keyword that generates a unique string (like a timestamp or unique email) that you can pass into your test steps.

Step 1: Create the Custom Keyword

  1. In Katalon Studio, go to the Tests Explorer on the left.

  2. Right-click on Keywords > New > Package. Name it com.utils.

  3. Right-click on the new package > New > Keyword. Name it DataGenerator.

  4. Paste the following Groovy code:

Groovy

package com.utils

import com.kms.katalon.core.annotation.Keyword

public class DataGenerator {
    
    /**
     * Generates a unique string using the current timestamp.
     * Example output: Emp_20260519130542
     * @param prefix The prefix for your identifier (e.g., "Emp_")
     */
    @Keyword
    def static String generateUniqueId(String prefix) {
        String timestamp = new Date().format("yyyyMMddHHmmss")
        return prefix + timestamp
    }
}

Step 2: Use it in your Test Case (Script View)

Now, instead of hardcoding your text in the setText web element modifier, call this keyword at the beginning of your test case.

Groovy

import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import internal.GlobalVariable as GlobalVariable

// 1. Generate a brand new, unique Employee ID for this specific run
String uniqueEmployeeId = CustomKeywords.'com.utils.DataGenerator.generateUniqueId'("EMP_")

WebUI.openBrowser('')
WebUI.navigateToUrl('http://your-app-url.com/login')

// 2. Use the dynamic variable instead of hardcoded text
WebUI.setText(findTestObject('Object Repository/Page_Dashboard/input_EmployeeID'), uniqueEmployeeId)
WebUI.setText(findTestObject('Object Repository/Page_Dashboard/input_FirstName'), 'John')
WebUI.setText(findTestObject('Object Repository/Page_Dashboard/input_LastName'), 'Doe')

WebUI.click(findTestObject('Object Repository/Page_Dashboard/button_Save'))

// 3. (Optional) Verify the unique ID was successfully added
WebUI.verifyTextPresent(uniqueEmployeeId, false)

WebUI.closeBrowser()

Why this works:

Every time you click Run, Katalon will instantly fetch the current exact second (e.g., EMP_20260519130542). Because time only moves forward, this value will never repeat, ensuring your test passes smoothly on the 2nd, 50th, or 1000th run!

I got to know this was alreayd discussed. will surely look for already discussed topic before posting.