Custom Keyword plugin with external dependency


Im trying to create an Custom keywords plugin. It turns out that my keyword will use some external jars. In this case i will use org.apache.poi jar that i already added to Driver folder. My build.gradle looks like

 plugins {
  id 'java'
  id 'groovy'
  id 'com.github.johnrengelman.shadow' version '4.0.4'
  id "com.katalon.gradle-plugin" version "0.0.6"

repositories {

sourceSets {
  main {
    groovy {
      srcDirs = ['Keywords', 'Include/scripts/groovy']
      srcDir 'Libs'

shadowJar {
  exclude 'Temp*.class'

katalon {
  dependencyPrefix = "com.katalon"
  minimize = false

jar {
    from fileTree("$buildDir/dependencies/")

task extractDependencies(type: Sync) {
    dependsOn configurations.compile

    from (configurations.compile.collect {

    exclude 'META-INF/**'

    into "$buildDir/dependencies/"

jar.dependsOn extractDependencies

and my katalon-plugin.json look like

  "keywords": ["keywordName"]

I do not added an package because is to ugly when we call “Custom Keyword” - com.test.name1.name2.etc.keywordName

Note that in build folder i create the folder “dependencies” and added the same jar’s that i added into Katalon Driver folder.

But when i add the generated plugin into “Plugins” folder from another project an error always appear "

com.kms.katalon.plugin.models.ReloadPluginsException: Unexpected error occurs during executing reload plugins
	at com.kms.katalon.plugin.service.PluginService.reloadPlugins(
	at com.kms.katalon.composer.handlers.ReloadPluginsHandler$
Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
	at java.util.ArrayList.rangeCheck(
	at java.util.ArrayList.get(
	at java_util_List$get$ Source)
	at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(
	at com.kms.katalon.custom.generation.CustomKeywordTemplate.getParameterName(CustomKeywordTemplate.groovy:177)
	at com.kms.katalon.custom.generation.CustomKeywordTemplate$getParameterName$0.callStatic(Unknown Source)
	at com.kms.katalon.custom.generation.CustomKeywordTemplate.getInitialExpression(CustomKeywordTemplate.groovy:155)
	at com.kms.katalon.custom.generation.CustomKeywordTemplate$ Source)
	at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(
	at groovy.tmp.templates.GStringTemplateScript23$_getTemplate_closure1$_closure3$_closure4$_closure5.doCall(GStringTemplateScript23.groovy:18)

Can anyone help me with this problem?

Have a nice day.

Some how i did manage to create. This error is kinda stupid, cuz in my Custom Keyword i did have like this:

public void KeywordName(String param1,String param 2, String[] param3,String[] param4){

Then in MainOperation the same method signature as the keyword.

 public void keywordNameMethod(String param1,String param 2, String[] param3,String[] param4){
        for(int i=0; i<param3.length; i++){

Then i did change the signature to be an Map<String, String> and with the for loop i did ```
for (Map.Entry<String, String> entry : map.entrySet())

So the main reason for the Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 i think was that param3.length some how.

I did make an test making a keyword that called the same method but without that for loop and it worked.

I hope this solution helps other people with the same issue.

1 Like

You have a single whitespace between ‘param’ and ‘2’, but why?

This single whitespace will confuse Groovy. Unfortunately Groovy failed to blame your coding mistake, got panicked, threw an Exception that doesn’t make sense.

You should write:

, String param2,
1 Like

Just for your interest. Groovy allows you to assign default values to method arguments. Following snippet is valid in Groovy:

public void keywordNameMethod(String param1 = "", String param2 = "", String[] param3 = new String[0], String[] param4 = new String[0]) {

It is a goodness of Groovy as describes. Java doesn’t support this.

I think that String param 2, which is invalid, looks too similar to String param = "", which is valid. That is the reason why Groovy got panicked.

Hi @kazurayam ,

Yes i know. I really don’t know why i post like String param 2, when in my code i have String param2.

Have a nice weekend.

Thank you.