Custom Keyword plugin with external dependency

Hi,

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 {
  jcenter()
  mavenCentral()
}

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 {
        zipTree(it)
    })

    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(PluginService.java:253)
	at com.kms.katalon.composer.handlers.ReloadPluginsHandler$1.run(ReloadPluginsHandler.java:79)
	at org.eclipse.core.internal.jobs.Worker.run(Worker.java:55)
Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
	at java.util.ArrayList.rangeCheck(ArrayList.java:657)
	at java.util.ArrayList.get(ArrayList.java:433)
	at java_util_List$get$0.call(Unknown Source)
	at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
	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$getInitialExpression.call(Unknown Source)
	at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:141)
	at groovy.tmp.templates.GStringTemplateScript23$_getTemplate_closure1$_closure3$_closure4$_closure5.doCall(GStringTemplateScript23.groovy:18)
etc....

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:

@Keyword
public void KeywordName(String param1,String param 2, String[] param3,String[] param4){
      MainOperation.keywordNameMethod(param1,param2,param3,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++){
              ..... 
param4[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 https://mrhaki.blogspot.com/2009/09/groovy-goodness-parameters-with-default.html 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.