I wanted to find out a solution to the following points:
I want my Katalon Studio project to generate a single Excel file where my Test Cases store anonymous data.
I want to name the Excel file in such format. If I start a Test Suite named TS_a then it creates a file named TS_afollowed by the timestamp when it was started. For example, it would be TS_a.20180901_091020.xls. If I select a Test Case named TC1 then it will create a file named TC1.xls without timestamp.
I want 2 or more Test Cases in a Test Suite share a file and update it mutually. Test Case TC2 should not carelessly erase data written in the file by preceding Test Case TC1.
I want to create sheet per each test case. If I have a Test Suite which contains 2 Test Cases TC1 and TC2, then running the Test Suite should result a Excel file containing 2 sheets: TC1 sheet and TC2 sheet.
How can I make multiple Test Cases in a Test Suite safely share a single Excel file? Let me suppose a preceding Test Case TC1 creates a sheet TC1 in a Excel file, I do not want following Test Case TC2 carelessly discard the TC1 sheet.
I found that combination of the following points solve the problem.
Each Test Case on start-up should look for the target Excel file. If it find the file, it should open the file and update it. If it does not find the file, it should create new file.
Use GlobalVariable.WORKBOOK. In the GlobalVariable I would put the object instance of HSSFWorkbook class (representation of Excel book by Apache POI). The Test Cases of a Test Suite can share the HSSFWorkbook object in the GlobalVariable during a Test Suite run.
Passing the HSSFWorkbook via GlobalVariable enables the Test Case TC3 updates the sheet created by preceding TC1 and TC2. Please read the source of TC3.
Each Test Case should serialize the HSSFWorkbook object into file when they shutdown.
All of I/O processing to the Excel file are centralized in the MyTestListener. No Test Case makes I/O to the file. This design much simplifies the project and makes it easy to understand.
When I run the project with Katalon Studio 5.10.1, I got the following messages in the Consle:
2019-01-12 06:42:19.205 INFO c.k.katalon.core.main.TestCaseExecutor - --------------------
2019-01-12 06:42:19.212 INFO c.k.katalon.core.main.TestCaseExecutor - START Test Cases/TC1
Caught: BUG! exception in phase 'semantic analysis' in source unit '/Users/urayamakazuaki/katalon-workspace/MultipleTestCasesSafelyWriteIntoExcelFile/Test Listeners/MyTestListener.groovy' ClassNode#getTypeClass for MyTestListener is called before the type class is set
BUG! exception in phase 'semantic analysis' in source unit '/Users/urayamakazuaki/katalon-workspace/MultipleTestCasesSafelyWriteIntoExcelFile/Test Listeners/MyTestListener.groovy' ClassNode#getTypeClass for MyTestListener is called before the type class is set