Where should I store test files that are not data and how do I pass it on to steps?

I managed to get file upload steps working with local files in my computer.
The thing is we handle the project with a github integration so I need my test files to be within the repository.

Considering this. How should I manage test files that are not Data files (PDFs, images, etc). Is there a way to add a folder in object repository for these?

Furthermore, I’m adding paths to my local computer in the variables for file upload, but folks working with me don’t have the same folder structure so it won’t work. Is there a way to have my test files within the project and then pass it on to test steps as a relative reference?

You can create a new folder, e.g. resources under the Include folder in your project and store whatever extra files needed there.

In your testcase, you can provide the relative path to it something like:

filename ='Include/resources/textfile.txt'

File fh1 = new File(filename)
text = fh1.getText('UTF-8')

// read all the lines into a list, each line is an element in the list
File fh2 = new File(filename)
def lines = fh2.readLines()
for (line in lines) {
	println line


2022-10-22 13:34:04.553 INFO  c.k.katalon.core.main.TestCaseExecutor   - --------------------
2022-10-22 13:34:04.555 INFO  c.k.katalon.core.main.TestCaseExecutor   - START Test Cases/Read from file
before test case
2022-10-22 13:34:04.767 DEBUG testcase.Read from file                  - 1: filename = "Include/resources/textfile.txt"
2022-10-22 13:34:04.768 DEBUG testcase.Read from file                  - 2: fh1 = new java.io.File(filename)
2022-10-22 13:34:04.772 DEBUG testcase.Read from file                  - 3: text = fh1.getText("UTF-8")
2022-10-22 13:34:04.774 DEBUG testcase.Read from file                  - 4: fh2 = new java.io.File(filename)
2022-10-22 13:34:04.774 DEBUG testcase.Read from file                  - 5: lines = fh2.readLines()
2022-10-22 13:34:04.781 DEBUG testcase.Read from file                  - 6: for (def line : lines)
2022-10-22 13:34:04.783 DEBUG testcase.Read from file                  - 1: println(line)
some text here 
2022-10-22 13:34:04.796 INFO  c.k.katalon.core.main.TestCaseExecutor   - END Test Cases/Read from file
1 Like

Looks like picking up the file works fine, but the file upload keyword is still not working.

I’m setting up your suggestion like this:

filename ='Include/resources/SAMPLE PDF.pdf'
File fh1 = new File(filename)

and then passing it on to the test step like this:
WebUI.uploadFile(findTestObject('My App/Page_Basic flow/Page_upload form/input_browse files'), fh1)

But it doesn’t seem to like it.

add_new_document_with_notification FAILED. Reason: groovy.lang.MissingMethodException: No signature of method: static com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.uploadFile() is applicable for argument types: (com.kms.katalon.core.testobject.TestObject, java.io.File)

i have no idea how the file upload keyword is working and what is the signature of it.
and i have no intention to look into it.
i just provide the solution for your original topic, now you are extending the scope of it.

my best advice, look into the api docs.
perhaps to pass the path as a string, and not as a file object?

The doc of WebUI.uploadFile is here:

It writes:

Param Param Type Mandatory Description
to TestObject Required Represent a web element.
fileAbsolutePath String Required Absolute path of the file on local machine.

It requires @naresh to give a string as “Absolute” path of the file on local machine. This requires @naresh to find out how to get “Absolute” path of the Include/resources/SAMPLE PDF.pdf. But how to?

A hint:

import com.kms.katalon.core.configuration.RunConfiguration

println RunConfiguration.getProjectDir()

This will give you the absolute path of the project’s folder, like

1 Like

Yes Have solved this one, by setting up variables like @bionel suggested here:

filename ='Include/resources/SAMPLE PDF.pdf'
File fh1 = new File(filename)

and then calling absolute path to fileUpload like this:

WebUI.uploadFile(findTestObject('My App/Page_Basic flow/Page_upload form/input_browse files'), fh1.getAbsolutePath())

Thank you both!

1 Like

@naresh the approach you took is a bit ‘overkill’ since you create first a file object and retrieve the needed string from it…
but i love that also, it works :slight_smile:
a combination from both of our advises may be more optimized, but i will let you to figure it out


When you run your code above on Katalon Studio GUI, the fh1 file will be located in the project directory. That is no surprise.

However when you run it on Katalon Runtime Enginein the command line, the fh1 file will be, most probably, located in the directory where Katalon Runtime Engine’s executable binary is installed. That will be a surprise, will confuse you. In the worst case on Linux, you may encounter some permission problem.

The point is, where is the current working directory set?

In Katalon Studio GUI, the current working directory is always set to be equal to the project’s directory.

In KRE, value of the current working directory is not fixed; it depends how you set it in the OS shell you write to activate KRE. For example, It is most likely you do
$ cd <the directory where KRE is installed>
then you execute

$ ./katalonc ...

This difference makes your code unstable.

How to make your code robust? How to make your code run on both environments the same?

Answer: use RunConfiguration.getProjectDir() as I told you. By this API call, Katalon Studio tells the location of the project directory. So you can use it to always store your custom files inside the project.

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

Path fh1path = Paths.get(RunConfiguration.getProjectDirectory()).resolve("Include/resources/SAMPLE PDF.pdf")
File fh1 = fh1path.toFile()
1 Like

@naresh In addition to what Kazu’ already said … actually you don’t need at all a File object (unless you intend to further manipulate the file from your code, but with your pdf example I don’t think it is the case)

This confusion has been caused because your original question was misleading.

I used the File object example only to demo how you can access a relative path, but actually you need the absolute path, and only as a string (Path), for the upload keyword to work.

You took the approach of creating the file object from the relative path and retrieve the absolute path from it. It works … but that object is useless now, stays resident in memory for nothing.
Therefore, it will be more elegant to use RunConfiguration … and more robust.

1 Like