Read a text file and write the content to a Katalon project test case

Hi All,

I want to read a text file and write the content of the file to a Katalon project test case.
Can I do that?

I tried using powershell command read the file and write to .tc test case file in the project folder, it updates the .tc file. After that test case is no longer there in the project. Seems file gets corrupted and will not be available.

Glad if someone can response.

Thanks,
Thushar

2 Likes

So what you want is to read a file in.

Different ways of Reading a text file in Java - GeeksforGeeks

And then write the file out. The issue is getting the name of the test case file name.

Java Create and Write To Files

Thanks for your reply. Mostly I am doing some testing on using StudioAssist feature in Katalon. Text file should contains text like,
/* 1. Open the browser using the GlobalVariable.URL

    1. Fill in the username field with ‘testuser’
    1. Fill in the password field with ‘password’
    1. Click in login button
    1. Wait for loginlogo object present with 20 seconds timeout
      */
      I want to read this file and write to a test case in the katalon project(TC_Test)
      ,
      image

Thanks.

1 Like

I tried this approach but didn’t work.

1 Like

A Test Case in Katalon Studio is not a single file on disk.
A Test Case in Katalon Studio is implemented as a pair of 2 files on disk.

Let me show you an example.
I made a new project with a single Test Case TC1.

Please open the project folder using Windows Explorer or Visua Studio Code.

I found 2 files have been created by Katalon Studio to implement a single Test Case TC1:

  1. Test Cases/TC1.tc — an XML text that contains metadata of the Test Case (name, testCaseGuid, etc)
  2. Scripts/TC1/Script1736550729267.groovy — a Groovy script

Katalon Studio GUI intentionally hides the fact that a single Test Case is implemented as a pair of 2 physical files. Because of that, @thushar.ameen misunderstood.

The file name Script1736550729267.groovy looks mysterious. Katalon Studio allocated it when I created the Test Case manually.

How was the cryptic string 1736550729267 derived? What’s the rule? How can I derive the identification myself without Katalon Studio? — I don’t know. It is not documented. It is not open-sourced. Therefore, I suppose @thushar.ameen would find difficulties in scripting his PowerShell.


Most probably, the 1736550729267 is derived from the long getTime() of the java.util.Date class object. The method returns the number of milliseconds since January 1, 1970, 00:00:00 GMT. Possibly that is the timestamp when the Groovy script was generated by Katalon Studio.

For example, try the following code:

printn new Date().getTime()

You will see something like this:

...
2025-01-14 18:50:15.225 DEBUG testcase.TC1               - 3: println(time)
1736848215220
2025-01-14 18:50:15.256 INFO  c.k.katalon.core.main.TestCaseExecutor   - END Test Cases/TC1

The value returned by getTime() is stringified as 1736.......... This looks very similar to the Test Case script file name Script1736550729267.groovy.

I don’t know the StudioAssist product at all.

Why not you do just manually copy&paste the AI-generated script into your designated test case script? Why did you introduce PowerShell here? I can’t guess why.

I wonder if the GUI of StudioAssist product supports you to copy&paste the AI-generated script into your designated Test Case by a simple UI operation. Possibly not.

Probablly, Katalon thought that manual copy&paste operation would be enough. They didn’t imagin what you, @thushar.ameen, wanted to achieve.

Do you really need AI to make such a simple test case?
It seem to me that AI just bothers you.
Why not you write the test case code manually without disturbance by AI?

This design is fragile: it sometimes causes a serious problem when you rename (or move) a test case in Katalon Studio GUI.

Katalon Studio has a long-outstanding bug. Katalon Studio sometimes fails to move the script code from the old ScriptXXXXXXXXXXXXX.groovy file into a new ScriptYYYYYYYYYYYYY.groovy file when you rename a Test Case. When it fails to move the script, you would lose the entire script. This is the worst bug. A programming tool like Katalon Studio should never erase the code that a user created. See more info about it.

Even in the v9.0 of Katalon Studio I sometimes encounter this bug recurring. Recently I published a topic where I developed a Test Case script that transforms an XML into another XML using XSLT. While I was writing the code, I carelessly renamed the Test Case once. Then, Katalon Studio erased my script due to the bug :tired_face: . Unfortunately I had not yet saved the project into a Git repository. So I lost the script. I had to type the whole script again :face_with_symbols_over_mouth:.

Test Case in Katalon Studio is such fragile. Be careful in manipulating it !

Hi Kazurayam,

Thanks for your response. We are trying to leverage Katalon tool for our manual testers. So, we are looking into a way to use AI generated test cases feed into katalon test case automated without manual copy and paste.
If I know the .grrovy file(name & location), can I update that file? I have not tested anything, would like to know what you think?

Updating the .groovy file is what you want to do generally, like Kazurayam mentioned, as long as you’re not renaming the test case files, editing the content shouldn’t break them
the .tc file is essentially just an xml with some test case properties. It’s similar with Test Suites as well; two files are generated, one with properties and one with content.

Just try it yourself and see.


I suppose that you would ask how to find the exact path of Groovy script file of a given Test Case.

I mean, given with the Test Case id Test Cases/TC1, then you need to find the exact path of Scripts/TC1/ScriptXXXXXXXXXXXXXX.groovy. But how can you do it?

The answer is

  1. The given Test Case id Test cases/TC1 will be always paired with a folder Scripts/TC1.
  2. The Scripts/TC1 folder will contain only one .groovy file. No more, no less.
  3. Therefore your PowerShell script should search the folder to find the list of child files with the extension .groovy in the Script/TC1. The resulting list will have only 1 member if successful. That is the file into which you want to write the text
    /* 1. Open the browser using the GlobalVariable.URL
    and followings.

How can I tell you this algorithm?

This algorithm is quoted from the source code of the getScriptPathByTestCaseId(String) method of the com.kms.katalon.core.testcases.TestCaseFactory class.

Here I would quote it:

static String getScriptPathByTestCaseId(String testCaseId) throws IOException {
        String testCaseScriptRelativePath = testCaseId.replaceFirst(TEST_CASE_ID_PREFIX, TEST_CASE_SCRIPT_ID_PREFIX)
                .replace(ID_SEPARATOR, File.separator);
        File testCaseScriptFolder = new File(getProjectDirPath(), testCaseScriptRelativePath);
        if (!testCaseScriptFolder.exists()) {
            return StringUtils.EMPTY;
        }

        File[] testCaseScriptFiles = testCaseScriptFolder.listFiles();
        if (testCaseScriptFiles == null) {
            return StringUtils.EMPTY;
        }

        for (File file : testCaseScriptFiles) {
            if (TEST_CASE_SCRIPT_FILE_EXTENSION.equals(FilenameUtils.getExtension(file.getName()))) {
                return file.getAbsolutePath();
            }
        }
        return StringUtils.EMPTY;
    }

The source code is bundled with all Katalon Studio installations. See <Katalon Studio installation folder>/Contents/configuration/resources/source/com.kms.katalon.core/com.kms.katalon.core-sources.jar which contains the source code.

When your PowerShell script updates the script on disk file, Katalon Studio would not automatically refresh the view of a Test Case script in the editor window

Users have to do “Refresh” operation manually, or close/reopen the script to refresh the display.

Thank you so much again kazurayam! Really appreciated your help.