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

**URL:** <https://forum.katalon.com/t/new-to-keyword-groovy-need-advice-how-to-convert-existing-java-code-into-katalon-format/39435>\
**Category:** API Testing\
**Tags:** api-testing, katalon-studio\
**Created:** [February 4, 2020, 2:03pm UTC](https://forum.katalon.com/t/new-to-keyword-groovy-need-advice-how-to-convert-existing-java-code-into-katalon-format/39435 "2020-02-04T14:03:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Katalonbetaguy](https://avatars.discourse-cdn.com/v4/letter/k/e47c2d/32.png) [@Katalonbetaguy](https://forum.katalon.com/u/Katalonbetaguy)\
**Post date:** [February 4, 2020, 2:03pm UTC](https://forum.katalon.com/t/new-to-keyword-groovy-need-advice-how-to-convert-existing-java-code-into-katalon-format/39435/1 "2020-02-04T14:03:58Z")

</div>

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();
    
}
```

---

<div class="post-metadata">

**Author:** ![huynguyen](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/huynguyen/32/552_2.png) [@huynguyen](https://forum.katalon.com/u/huynguyen)\
**Post date:** [February 4, 2020, 3:52pm UTC](https://forum.katalon.com/t/new-to-keyword-groovy-need-advice-how-to-convert-existing-java-code-into-katalon-format/39435/2 "2020-02-04T15:52:18Z")

</div>

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](https://docs.katalon.com/katalon-studio/docs/introduction-to-custom-keywords.html#custom-keywords-in-manual-view) for more details.

---

<div class="post-metadata">

**Author:** ![Timo\_Kuisma1](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/timo_kuisma1/32/16103_2.png) [@Timo\_Kuisma1](https://forum.katalon.com/u/Timo_Kuisma1)\
**Post date:** [February 4, 2020, 3:58pm UTC](https://forum.katalon.com/t/new-to-keyword-groovy-need-advice-how-to-convert-existing-java-code-into-katalon-format/39435/3 "2020-02-04T15:58:46Z")

</div>

```
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;
		   }
	   }
   }
```

---

<div class="post-metadata">

**Author:** ![Katalonbetaguy](https://avatars.discourse-cdn.com/v4/letter/k/e47c2d/32.png) [@Katalonbetaguy](https://forum.katalon.com/u/Katalonbetaguy)\
**Post date:** [February 5, 2020, 2:07am UTC](https://forum.katalon.com/t/new-to-keyword-groovy-need-advice-how-to-convert-existing-java-code-into-katalon-format/39435/4 "2020-02-05T02:07:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [February 5, 2020, 2:24am UTC](https://forum.katalon.com/t/new-to-keyword-groovy-need-advice-how-to-convert-existing-java-code-into-katalon-format/39435/5 "2020-02-05T02:24:38Z")

</div>

> [@Katalonbetaguy](#):
>
> If I put my java codes directly to test case

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).

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [February 5, 2020, 2:33am UTC](https://forum.katalon.com/t/new-to-keyword-groovy-need-advice-how-to-convert-existing-java-code-into-katalon-format/39435/6 "2020-02-05T02:33:13Z")

</div>

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

> <https://github.com/kazurayam/MultipleTestCasesSafelyWriteIntoExcelFile/blob/master/Scripts/TC1/Script1535664852968.groovy>

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 { ... }`.
