Get the next record from a data file

Hello, I have a question, how can I make a method that takes the first record and then runs and takes the second? I have a test where I have to register several computers and I want it to take all the records that it has in that list

This article tells you that there is 2 approaches of “Data-driven testing”

  • A. Execution from test suites
  • B. Execute from a test case

I guess, you @miguelbenzam are using the “A. Execution form test suites” approach. By the “A” approach, Katalon Studio does opening a file and reading records out of it. KS will pass records one by one to your test case. Your test case code has no control over accessing the data.

“B. Execute from a test case” — I am afraind, it is proorly titled. The title “use
com.kms.katalon.core.testdata.InternalData object explicitly” would be better.

In the “B” approach, your test case is totally responsible opening a file and reading records out of it.

So you should try the “B”.

I guess, you should write a preprocessor script. The preprocessor transform the data file(s) to create a simpler list of data which your test can consume just one by one record.

1 Like

It sounds like you wish to loop through the models of that TestData file…

If so, and you are willing to parse each of the rows into models, I start you off with this BaseModelBuilder, straight from my actual project:

public abstract class BaseModelBuilder<T> {

	protected TestData testData;

	public BaseModelBuilder() {
		this.init();
	}

	public BaseModelBuilder(TestData testData) {
		this.testData = testData;
		this.init();
	}

	protected void init() {
		// implementation go here
	}

	public TestData getTestData() {
		return this.testData;
	}

	public List<T> createModels() {
		return this.testData.getAllData()
				.stream()
				.filter { row ->
					row.stream().anyMatch { String cell ->
						cell != null && !cell.isEmpty()
					}
				}
				.map { row -> this.createModelFromRow(row) }
				.collect(Collectors.toList())
	}

	public T createModelFromRowNum(int rowNum) {
		return this.createModelFromRow(this.testData.getAllData().get(rowNum))
	}

	public abstract T createModelFromRow(List<Object> row)
}

to use it, simply:

  • create and implement your model class
  • extend BaseModelBuilder with a builder for YourModel (let’s call it YourModelBuilder) like so:
public class YourModelBuilder extends BaseModelBuilder<YourModel> { 
	public YourModelBuilder() { 
		super(findTestData(yourDataFile)); // TODO: replace yourDataFile with whatever you named it in Katalon Studio
	}

	@Override
	public YourModel createModelFromRow(List<Object> row) { 
		// TODO: implement mapping of the 0-based row entries to the properties of YourModel
	}
}
  • and then, in your data-driven loop-driven test, you can just be like :
for (YourModel model : new YourModelBuilder().createModels()) { 
	// your model-driven test code goes here
}

NOTE: you should save these models and builders as Keywords