New to keyword groovy need advice how to convert existing java code into katalon format

I’m new to katalon keyword.
I have existing java codes which is working but I’m not sure how to bind into katalon keyword groovy file.

@keyword def xxxxx should be inside the
class ExcelDateReader???

My codes:

public class ExcelDateReader {

public static void main(String[] args) throws FileNotFoundException, IOException {
    writeIntoExcel("birthdays.xlsx");
    readFromExcel("birthdays.xlsx");
}

public static void readFromExcel(String file) throws IOException{
    XSSFWorkbook myExcelBook = new XSSFWorkbook(new FileInputStream(file));
    XSSFSheet myExcelSheet = myExcelBook.getSheet("Birthdays");
    XSSFRow row = myExcelSheet.getRow(0);
    
    if(row.getCell(0).getCellType() == HSSFCell.CELL_TYPE_STRING){
        String name = row.getCell(0).getStringCellValue();
        System.out.println("NAME : " + name);
    }
    
    if(row.getCell(1).getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
        Date birthdate = row.getCell(1).getDateCellValue();
        System.out.println("DOB :" + birthdate);
    }
    
    myExcelBook.close();
    
}

Hi @Katalonbetaguy,
I don’t know exactly what you want to extract into custom keywords. However, for example, if you want to convert your method readFromExcel to a keyword, you can define a groovy keyword file as the following:

package your_package

// define your imported packages here

public class ExcelKeywords {
       @Keyword
       def readFromExcel(String file) throws IOException {
                //put your code here
       }
}

Please refer to https://docs.katalon.com/katalon-studio/docs/introduction-to-custom-keywords.html#custom-keywords-in-manual-view for more details.

hi,
it's not necessary to use keywords (recommendation), so you can use pure java classes in Katalon testcase script
like this way
for (CrunchifyEnumExample.Company cName : CrunchifyEnumExample.Company.values()){
	//System.out.println("Company Value: " + cName.value + " - Company Name: " + cName);
	
	switch(cName.value){
		
		case 30:
			println "Company Name: "+cName
			break	
		case 10:
			println "Company Name: "+cName
			break
		case 15:
			println "Company Name: "+cName
			break
		case 20:
			println "Company Name: "+cName
			break
		case 25:
			println "Company Name: "+cName
			break
		default:
			println "no values!!!"
			break
	}
}


public class CrunchifyEnumExample {
	
	   public enum Company {
		   EBAY(30), PAYPAL(10), GOOGLE(15), YAHOO(20), ATT(25);
		   private int value;
	
		   private Company(int value) {
			   this.value = value;
		   }
	   }
   }

If I put my java codes directly to test case then compiler complains there’s no step to run.
Thats why I thought using keyword would be better.

No. That is not Katalon-way.

You can put your Java class into the /Keywords/ directory and rename them from *.java to *.groovy. It would work.

But all files contained in the <project>/Test Cases/ directory will be regarded as code written in Katalon-original Domain Specific Language (similar to Groovy script, but not Groovy class).

Do you want to see a Test Case script as an example of calling Apache POI API? Then have a look at this:

You can easily see that this code does not have class XXX { ... }. As you know, Java requires class everywhere. In Test Case script, you can not write class XXX { ... }.