Custom Keyword vs Method

Hello,

I really hope that I my question has been already covered by someone else. What I’d like to know is when to use Custom Keywords and when Methods. What is their special purpose?

Some differences I figured out so far are

  • A Custom Keyword cannot call another one <-> methods can call other methods

  • Keywords need an anotation, methods not

  • When calling a Custom Keyword the sysntax looks a bit complex with

CustomKeywords.'package.class.keyword'(params)  

Due to this it seems to be better to use methods all the time. But Im sure there are reasons to use the Custom Keywords :slight_smile:

1 Like

The only reason to use @Keyword methods is so that non-programmers can choose them from a list in Manual View. If you’re comfortable in Script View, create regular methods (perhaps make them static) and import your package/classes where you need them.

BTW, you can call one @Keyword from another but then you’re mixing intentions and purposes (rarely a good idea).

2 Likes

Is there a way to create a public static method that you can call in any test case? I figured keyword was the only way to do this. I don’t see the package structure for creating test cases.

B L said:

Is there a way to create a public static method that you can call in any test case? I figured keyword was the only way to do this. I don’t see the package structure for creating test cases.

Create a package, my_package.

Add a new class to the package, my_class:

package my_package

public class my_class {
  ...
}

Add a new static method my_method to my_class:

public class my_class {

  static void my_method(/* params here */) {
    // do remarkable things
  }
}

In your test cases:

import static my_package.my_class.*

// call my_method
my_method()
4 Likes

where would this go though? I don’t see where packages and methods are stored in katalon. I know how to do this with eclipse IDE but in Katalon, we have test cases and keywords. do I need to add something for having packages?

It’s a keyword. Define a new keyword but don’t use “@Keyword”.

Crazy and confusing, but that’s how it’s done.

3 Likes

This has opened Katalon SO MUCH more for me. Thank you :slight_smile:

1 Like

Is there a way to use shortform of the CustomKeyword function? like we use it for WebUI?

Thanks
Sriker Paidy

What do you mean by “shortform”? Isn’t my_method() short enough?

Hi Thomas, Thanks for your reply…

I would like to use it as the import statement we have in katalon like the complete import statement I am using by alias name right, I am not really sure, if its possible or not… just wanted to know.

Thanks & Regards,
Sriker Paidy

Did you try it? What happened?

As its a keyword, it is asking for the parameters.

I’m not following what you mean.

Following my advice above for creating a method (note: not an actual @Keyword), this works as expected:

import static my_package.my_class.my_method as mm

// call my_method
mm( /* your args here */)
1 Like

Thanks Thomas!! Got my answer :slight_smile: