Change Test Data file's name during the runtime

Hello,
Is it possible to somehow change file’s name, which is being used by TestData, during the run-time instead of specifying in TestData’s object as shown below?

In my case, file’s name will be different every time I run the test case so I need to find a way how to tell my testdata object that file name is changed.

I don’t think you can change the name of a pre-defined TestData object at runtime. Instead, you can create a new TestData (ExcelData in your case) object programmatically at runtime as follows:

String fileName = "someDynamicName";
ExcelData data = ExcelFactory.getExcelData("D:/" + fileName, true);

Got ya, thanks for your response.
One more question, is it possible to read CSV data the same way ?

Hmmm never tried it. But looking at the same package that ExcelFactory lives in, it looks like you could do something like:

String fileName = "someDynamicName";
CSVReader reader = new CSVReader("D:/" + fileName, ",", true);
List<List<String>> data = reader.getData();

Alternatively, you can do what I do and skip the Katalon libs altogether and import the Apache Commons CSV jar from here:

https://commons.apache.org/proper/commons-csv/download_csv.cgi

With this lib, along with its Apache POI counterpart, you have full control over test data presented in Excel or CSV formats. Depends on how much elbow grease you want to put into it though :wink:

Hi Egor,

In reading the CSV, you can also try this approach:

import com.kms.katalon.core.testdata.CSVData

CSVData csvData = new CSVData(“C:\yourCSV.csv”, true, CSVSeparator.COMMA)

//to access or to get the .csv file values refer to some commonly used commands
//this is to get all the used rows or total number of rows
csvData.allData or csvData.getAllData()
csvData.getValue(‘columnname’, int rowNumber)
csvData.rowNumbers 
csvData.columnNumbers
csvData.columnNames

Hope that helps . . :slight_smile:

@Arnel @Brandon_Hein
Thanks guys! I appreciate it.

@Arnel

Not sure if you will see this, but any idea what I’m doing wrong. I’m following your basic instructions (import command) + defining where my csv file is located, and every time I run this I get an error:

Test Cases/ReadCSVExample FAILED.
Reason:
groovy.lang.MissingPropertyException: No such property: CSVSeparator for class: Script1567532257335
at ReadCSVExample.run(ReadCSVExample:19)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:337)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:328)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:307)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:299)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:233)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:114)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:105)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at TempTestCase1567536185335.run(TempTestCase1567536185335.groovy:21)

my code looks like:
import com.kms.katalon.core.testdata.CSVData

CSVData csvData = new CSVData(“C://test_files//matter_data_download.csv”,false,CSVSeparator.COMMA)

I’ve tried without the //, I’ve tried with \ characters, nothing I do works. I also tried replacing the CSVSeparator with just “,”

Just looking for a simple solution to read the contents of a very basic CSV file and I’m getting nowhere. Appreciate any feedback you can provide.

I see your code example on many different topics so I have to assume this should still work.

Try adding the following import to your list:

import com.kms.katalon.core.testdata.reader.CSVSeparator

OMG, nevermind, I don’t know what I had done wrong but it must be a copy paste issue. I made a few tweaks and used Katalon’s hints and it magically started working :grinning:

Thanks for the feedback, honestly I’m not sure what I did but picking some values from the code hints Katalon provided seems to have fixed my issue, I must have had something wrong with the CSVSeparator argument. Feel like a dummy but I really was loosing my mind here.

1 Like

Thank you! I’m moving in the right direction now.