How would I assign each data row to each test suite?


Currenlty, I would like to assign each row of data to each test suite. How would I do that? Katalon Studio mobile-testing

1 Like

How about creating a Global Variable that is set to 0 (zero) in the Setup (found on the Script tab of your Test Suite; see sample below) and then, at the start of each Test Case, you increment the Global Variable by one and have it control your data row?

@SetUp(skipped = false) // Please change skipped to be false to activate this method.
def setUp() {
	// Put your code here.
	GlobalVariable.gDataRow = 0;
}

Edit: if you want to do something like above but you are want the data row to be used for multiple Test Suites, then you will have to save the “data row” to file or spreadsheet and then read the reference number for your next TS. There are several reference on how to save and then read a reference from spreadsheet on this forum.

Edit2: I brought a sample to you.

Writing to spreadsheet:
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;

GlobalVariable.gDataRowPathWay = "G:\\Katalon Test Cases\\Katalon\\Spreadsheets\\DataRow.xlsx"

FileInputStream fis = new FileInputStream (GlobalVariable.gDataRowPathWay);
XSSFWorkbook workbook = new XSSFWorkbook (fis);

XSSFSheet sheet = workbook.getSheet("Sheet1");

Row row = sheet.createRow(1);
Cell cell = row.createCell(0);

cell.setCellType(cell.CELL_TYPE_STRING);
cell.setCellValue(GlobalVariable.gDataRow);

FileOutputStream fos = new FileOutputStream(GlobalVariable.gDataRowPathWay );
workbook.write(fos);
fos.flush()
fos.close();
fis.close();
Reading from spreadsheet:
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;

GlobalVariable.gDataRowPathWay = "G:\\Katalon Test Cases\\Katalon\\Spreadsheets\\DataRow.xlsx"

FileInputStream fis = new FileInputStream (GlobalVariable.gDataRowPathWay);
XSSFWorkbook workbook = new XSSFWorkbook (fis);

XSSFSheet sheet = workbook.getSheet("Sheet1");

Row row = sheet.getRow(1);
Cell cell = row.getCell(0);
GlobalVariable.gDataRow = cell.getStringCellValue();

fis.close();

Edit3: You might need an if statement to reset the data row back to zero if the data row is higher than the last row you are testing.

1 Like

I like @grylion54 's answer, but we can do even better…

Enter this modular way to do data-driven testing…!

Create your own model class, and model handler like so:

MerchantModel

public class MerchantModel { 
	int id;

	String category, merchantName, description;

	public MerchantModel() {
		super();
	}

	public MerchantModel(int id, String category, String merchantName, String description){
		super();
		this.id = id;
		this.category = category;
		this.merchantName = merchantName;
		this.description = description;
	}
}

MerchantModelBuilder

public class MerchantModelBuilder extends BaseModelBuilder<MerchantModel> {
	public MerchantModelBuilder() {
		super(findTestData("Test Data"));
	}

	@Override
	MerchantModel createModelFromRow(List<Object> row) {
		return new MerchantModel(
			Integer.parseInt(row[0]),
			row[1],
			row[2],
			row[3],
		);
}

Next, let’s create a singleton class to iterate over these:

public class MerchantModelIterator implements Iterator<MerchantModel> {
	private static MerchantModelIterator _instance;

	private List<MerchantModel> merchantModels;

	private int index = 0;

	public static MerchantModelIterator GetInstance() {
		if (_instance == null)
			_instance = new MerchantModelIterator();

		return _instance;
	}

	private MerchantModelIterator() {
		super();
		this.merchantModels = new MerchantModelBuilder().createModels();
	}

	@Override
	public boolean hasNext() {
		return (index < merchantModels.size());
	}

	@Override
	public MerchantModel next() {
		if (!hasNext()) 
			throw new IllegalStateException("No next");

		return merchantModels[index++];
	}
}

Finally, let’s use it in your test suite hook:

def setUp() { 
	GlobalVariable.currentMerchantModel = MerchantModelIterator.GetInstance().next();
}

Make sure you have Global Variable called currentMerchantModel (and use it in your test cases).

But all of this is just a good idea unless…

…you have access to Custom Keywords. Do you happen to have access to that?