Global variable value need to be updated from the keywords written

You cannot write back to the Global Variable table, however, you can write the Global Variable to “disk” and read it back again with your next test case.

Example

I created a spreadsheet, in this case, TestId 001.xlsx, which I placed in the “Data Files” folder of the Katalon Studio project. I saved the reference to the spreadsheet in the first TC and retrieved the reference in the second TC. Note that you cannot have the spreadsheet open to view when you are running the TC as that will put a lock on the file and cause an error.

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import internal.GlobalVariable as GlobalVariable

GlobalVariable.gTestIdPathWay = "G:\\Katalon Test Cases\\Data Files\\TestId 001.xlsx";

/* open connection to MS Excel, save Your Reference to sheet 1, cell A2, close connection */
FileInputStream fis = new FileInputStream (GlobalVariable.gTestIdPathWay);
XSSFWorkbook workbook = new XSSFWorkbook (fis);

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

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

cell.setCellType(cell.CELL_TYPE_STRING);
cell.setCellValue(GlobalVariable.SetID);   // ** write your reference out here **

FileOutputStream fos = new FileOutputStream(GlobalVariable.gTestIdPathWay);
workbook.write(fos);
fos.close();
fis.close();

Now, read the reference back into the TC.

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import internal.GlobalVariable as GlobalVariable


GlobalVariable.gTestIdPathWay = "G:\\Katalon Test Cases\\Data Files\\TestId 001.xlsx";

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

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

Row row = sheet.getRow(1);
Cell cell = row.getCell(0);
GlobalVariable.SetID = cell.getStringCellValue();  // ** read your reference back in here **

fis.close();
2 Likes