# Introduction to Custom Keywords

**URL:** <https://forum.katalon.com/t/introduction-to-custom-keywords/16825>\
**Category:** Miscellaneous\
**Tags:** new-doc-request\
**Created:** [January 4, 2019, 11:57am UTC](https://forum.katalon.com/t/introduction-to-custom-keywords/16825 "2019-01-04T11:57:12Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://europe1.discourse-cdn.com/katalon/original/3X/d/5/d55596ab02785a255798e87ede769734747b30c4.svg) [@system](https://forum.katalon.com/u/system)\
**Post date:** [January 4, 2019, 11:57am UTC](https://forum.katalon.com/t/introduction-to-custom-keywords/16825/1 "2019-01-04T11:57:12Z")

</div>

* * *
This is a companion discussion topic for the original entry at [https://docs.katalon.com/katalon-studio/docs/introduction-to-custom-keywords.html](https://docs.katalon.com/katalon-studio/docs/introduction-to-custom-keywords.html)

---

<div class="post-metadata">

**Author:** ![GeneralChingChicken](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/generalchingchicken/32/1398_2.png) [@GeneralChingChicken](https://forum.katalon.com/u/GeneralChingChicken)\
**Post date:** [September 24, 2019, 1:02pm UTC](https://forum.katalon.com/t/introduction-to-custom-keywords/16825/2 "2019-09-24T13:02:16Z")

</div>

If you want to use Custom Keywords in a “Step Definition” .groovy file, what “import” package do you use?

---

<div class="post-metadata">

**Author:** ![Eric\_Montou](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/eric_montou/32/915_2.png) [@Eric\_Montou](https://forum.katalon.com/u/Eric_Montou)\
**Post date:** [November 7, 2019, 4:54pm UTC](https://forum.katalon.com/t/introduction-to-custom-keywords/16825/3 "2019-11-07T16:54:05Z")

</div>

I have run into the same problem. Have you found a solution for this?

---

<div class="post-metadata">

**Author:** ![biskyt](https://avatars.discourse-cdn.com/v4/letter/b/a87d85/32.png) [@biskyt](https://forum.katalon.com/u/biskyt)\
**Post date:** [November 8, 2019, 11:48am UTC](https://forum.katalon.com/t/introduction-to-custom-keywords/16825/4 "2019-11-08T11:48:20Z")

</div>

Say your custom package is in `Keywords/my.keywords/myCustom.groovy` and that the class is called `myCustom` and that there is a method named 'myMethod()` inside the class.

In which case you would add:  
`import my.keywords.myCustom()`

To use `myMethod()` you would then need:

```auto
myCustom x = new myCustom()
x.myMethod()

```

I tend to declare the class at the top of my scripts as a static, then you can use it throughout the script as an alias. Eg.

```auto
private myCustom mc = new myCustom
...
def blahh () {
   ...
   mc.myMethod()
   ...
}
...

```

I hope that helps

---

<div class="post-metadata">

**Author:** ![lokesh.kambhoji](https://avatars.discourse-cdn.com/v4/letter/l/c6cbf5/32.png) [@lokesh.kambhoji](https://forum.katalon.com/u/lokesh.kambhoji)\
**Post date:** [December 26, 2019, 7:33am UTC](https://forum.katalon.com/t/introduction-to-custom-keywords/16825/5 "2019-12-26T07:33:11Z")

</div>

Hi,  
I want to create a custom keyword in Katalon studio using Java to generate random number.please help me on this.  
By default custom keywords are generated in groovy.

Thanks in advance.

---

<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:** [December 26, 2019, 7:46am UTC](https://forum.katalon.com/t/introduction-to-custom-keywords/16825/6 "2019-12-26T07:46:09Z")

</div>

> [@lokesh.kambhoji](#):
>
> I want to create a custom keyword in Katalon studio using Java to generate random number.

please make a search in the Forum with keyword “random”. You will find a lot of previous posts on this issue.

---

<div class="post-metadata">

**Author:** ![lokesh.kambhoji](https://avatars.discourse-cdn.com/v4/letter/l/c6cbf5/32.png) [@lokesh.kambhoji](https://forum.katalon.com/u/lokesh.kambhoji)\
**Post date:** [December 26, 2019, 9:04am UTC](https://forum.katalon.com/t/introduction-to-custom-keywords/16825/7 "2019-12-26T09:04:52Z")

</div>

Hi,

Thank you for your reply, While creating custom keywords by default these are generated in Groovy language. Can i create custom keywords using Java programming language in Katalon studio?

---

<div class="post-metadata">

**Author:** ![ThanhTo](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/thanhto/32/697_2.png) [@ThanhTo](https://forum.katalon.com/u/ThanhTo)\
**Post date:** [December 26, 2019, 9:06am UTC](https://forum.katalon.com/t/introduction-to-custom-keywords/16825/8 "2019-12-26T09:06:52Z")

</div>

Hi @lokesh.kambhoji

Apache Groovy is a Java-syntax-compatible object-oriented programming language for the Java platform, which means you can develop your custom keywords using Java.

---

<div class="post-metadata">

**Author:** ![lokesh.kambhoji](https://avatars.discourse-cdn.com/v4/letter/l/c6cbf5/32.png) [@lokesh.kambhoji](https://forum.katalon.com/u/lokesh.kambhoji)\
**Post date:** [December 26, 2019, 9:39am UTC](https://forum.katalon.com/t/introduction-to-custom-keywords/16825/9 "2019-12-26T09:39:27Z")

</div>

Hi @ThanhTo,

Thank you for the information, As part of client requirement we need to develop automated tests using Java language. While creating custom keywords by default these are generated with “.groovy” extension. Can i create custom keywords with “.java” extension directly?because we’ve code already written in java so that we can copy code to the Katalon studio directly. Thanks in advance.

---

<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:** [December 26, 2019, 12:59pm UTC](https://forum.katalon.com/t/introduction-to-custom-keywords/16825/10 "2019-12-26T12:59:22Z")

</div>

> [@lokesh.kambhoji](#):
>
> because we’ve code already written in java

You do not have to copy the \*.java source files into the Katalon project. You should rather create a jar file of your useful Java library class files (not \*.java source files). And you want to locate the jar into the Drivers directory of a Katalon Studio project. Your test case scripts in Groovy can import and use the classes contained in the jar.

See  
[https://docs.katalon.com/katalon-studio/docs/external-libraries.html](https://docs.katalon.com/katalon-studio/docs/external-libraries.html)

---

<div class="post-metadata">

**Author:** ![lokesh.kambhoji](https://avatars.discourse-cdn.com/v4/letter/l/c6cbf5/32.png) [@lokesh.kambhoji](https://forum.katalon.com/u/lokesh.kambhoji)\
**Post date:** [January 2, 2020, 10:03am UTC](https://forum.katalon.com/t/introduction-to-custom-keywords/16825/11 "2020-01-02T10:03:28Z")

</div>

Thank you for your answer!

I’ve created a method in groovy to generate random number, My requirement is to return this random number and wants to pass a parameter to the Send Keys command(built in web UI keyword) in manual mode(input column).  
How can i pass the method return value to the webUI keyword as input parameter?

@Keyword  
def String getRandomNumber(int num){  
Random random = new Random()  
String number= "Property "+ random.nextInt(10 \*\* num).toString()  
// return "Property "+ random.nextInt(10 \*\* num).toString()  
return number  
}

Thanks in advance!

---

<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:** [January 2, 2020, 1:08pm UTC](https://forum.katalon.com/t/introduction-to-custom-keywords/16825/12 "2020-01-02T13:08:18Z")

</div>

> [@lokesh.kambhoji](#):
>
> I’ve created a method in groovy to generate random number, My requirement is to …

This is a new question, different from the one you raised 7d ago. Please create a new discussion for a new question.

---

<div class="post-metadata">

**Author:** ![deepthi2](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/deepthi2/32/16104_2.png) [@deepthi2](https://forum.katalon.com/u/deepthi2)\
**Post date:** [February 25, 2020, 8:36am UTC](https://forum.katalon.com/t/introduction-to-custom-keywords/16825/13 "2020-02-25T08:36:50Z")

</div>

I have Custom Keyword in package A. I want to add this custom keyword in one of the methods in Package B which is also another custom keyword. How do I use a custom keyword created in package A within the method in package B?

---

<div class="post-metadata">

**Author:** ![anon46315158](https://avatars.discourse-cdn.com/v4/letter/a/73ab20/32.png) [@anon46315158](https://forum.katalon.com/u/anon46315158)\
**Post date:** [February 25, 2020, 4:32pm UTC](https://forum.katalon.com/t/introduction-to-custom-keywords/16825/14 "2020-02-25T16:32:37Z")

</div>

you simply import them and use them like in any java/groovy app.  
a ‘custom keyword’ is just a class.  
the @keyword annotation is just a helper four using the methods in test scripts

---

<div class="post-metadata">

**Author:** ![shilpigs](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/shilpigs/32/16103_2.png) [@shilpigs](https://forum.katalon.com/u/shilpigs)\
**Post date:** [February 4, 2021, 8:06am UTC](https://forum.katalon.com/t/introduction-to-custom-keywords/16825/15 "2021-02-04T08:06:02Z")

</div>

I wanted to parameterize custom keyword (Method / Function Path).

This is the original line of scripts which is working fine :  
CustomKeywords.‘alfha.Offerings.clickCreateNewOffering’()

In the above eg, wanted to parametrize the “alfha” path. and I tried with below script and throwing error.

def feature= ‘alfha’  
println(feature)  
CustomKeywords.’‘feature’Offerings.clickCreateNewOffering’()

Can anyone please help me out to solve this requirement?

---

<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 4, 2021, 8:15am UTC](https://forum.katalon.com/t/introduction-to-custom-keywords/16825/16 "2021-02-04T08:15:51Z")

</div>

You can write as this:

```auto
def packg= 'alfha'
println(packg)
CustomKeywords."${packg}.Offerings.clickCreateNewOffering"()

```

though I do not see why you want to write like this.

---

<div class="post-metadata">

**Author:** ![shilpigs](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/shilpigs/32/16103_2.png) [@shilpigs](https://forum.katalon.com/u/shilpigs)\
**Post date:** [February 4, 2021, 8:27am UTC](https://forum.katalon.com/t/introduction-to-custom-keywords/16825/17 "2021-02-04T08:27:52Z")

</div>

@kazurayam Thanks, works as expected.

---

<div class="post-metadata">

**Author:** ![shilpigs](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/shilpigs/32/16103_2.png) [@shilpigs](https://forum.katalon.com/u/shilpigs)\
**Post date:** [February 24, 2021, 1:52pm UTC](https://forum.katalon.com/t/introduction-to-custom-keywords/16825/18 "2021-02-24T13:52:37Z")

</div>

How to use @BeforeMethod or @BeforeTestStep in Test Listeners or Test case.  
Basically I wanted to call a function for each customkeyword step.

eg:  
Function :  
def flag()  
{  
def flag = ‘alfha’  
}

Test case :  
CustomKeywords.‘beta.Login.OpenBrowser’()  
CustomKeywords.‘beta.Offerings.clickCreateNewOffering’()  
CustomKeywords.‘beta.Offerings.clickSave’()  
CustomKeywords.‘beta.Login.closeBrowser’()

In the above example, I wanted to call ‘flag()’ method / function automatically before each customKeyword step / line while execution.

---

<div class="post-metadata">

**Author:** ![andrew.block](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/andrew.block/32/16104_2.png) [@andrew.block](https://forum.katalon.com/u/andrew.block)\
**Post date:** [March 31, 2021, 9:13pm UTC](https://forum.katalon.com/t/introduction-to-custom-keywords/16825/19 "2021-03-31T21:13:55Z")

</div>

What does the keywordObject category do? I added in a category to my plugin and I don’t see any difference. Where do I see this category in KS?

---

<div class="post-metadata">

**Author:** ![taynguyen](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/taynguyen/32/4078_2.png) [@taynguyen](https://forum.katalon.com/u/taynguyen)\
**Post date:** [July 12, 2021, 1:29pm UTC](https://forum.katalon.com/t/introduction-to-custom-keywords/16825/20 "2021-07-12T13:29:18Z")

</div>

hi there,  
we have

> static WebDriver driver = DriverFactory.getWebDriver()

for custom keyword WebUI

So do we have something like

> static WindowsDriver driver = DriverFactory.getWindowsDriver()

for custom keyword Windows(desktop app)?  
thanks!

[Next page](https://forum.katalon.com/t/introduction-to-custom-keywords/16825.md?page=2)
