Introduction to Custom Keywords


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

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

1 Like

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

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:

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.

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

I hope that helps

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.

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

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?

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.

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.

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

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!

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

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?

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

1 Like

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?

You can write as this:

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

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

1 Like

@kazurayam Thanks, works as expected.

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.

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?

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!