How to define Global Variables within Scripts, i.e. "on the fly"?

Because I would like to reuse my general keyword scripts directly in every project, without first having to make presettings in several Katalon Studio menus.

I have ever had a similar requirement: want to reuse a set of custom keywords in multiple projects. I developed my own solution using the External Libraries feature of Katalon Studio.

I would explain my solution by an example. In my Katalon project I have a Keyword: verifyAccessiblity(String urlString)

package com.happymigration
import com.kazurayam.ksbackyard.AbstractHttpURLVerifier
import com.kazurayam.ksbackyard.DefaultVerifiableHttpURLConnection
import com.kazurayam.ksbackyard.HttpURLVerifier
import com.kazurayam.ksbackyard.VerifiableHttpURLConnection
import com.kms.katalon.core.annotation.Keyword
import com.kms.katalon.core.util.KeywordUtil
class HMHttpURLVerifier extends AbstractHttpURLVerifier implements HttpURLVerifier {
  @Override
  @Keyword
  public boolean verifyAccessibility(String urlString) {
    StringBuilder sb = new StringBuilder()
    Optional<VerifiableHttpURLConnection> opt = this.connect(urlString, sb);
    VerifiableHttpURLConnection conn = opt.orElse(DefaultVerifiableHttpURLConnection.EMPTY);
    boolean result = conn.isAccessible();
    if (!result) {
      KeywordUtil.markFailed(sb.toString())
    } else {
      KeywordUtil.markPassed(sb.toString())
    }
    return result
  }
  @Override
  protected VerifiableHttpURLConnection createVerifiableHttpURLConnection(HttpURLConnection conn, StringBuilder sb) {
    return new HMVerifiableHttpURLConnection(conn, sb)
  }
}

You should not too much worry about 4 classes of com.kazurayam.ksbackyard package. These are my custom classes.

Important point is that I developped the com.kazurayam.ksbackyard classes outside Katalon Studio.

I made a project in Eclipse where I developed these classes. I made a jar file which contains the com.kazurayam.ksbackyard package. Then I added the jar into my Katalon projects using External Libraries feature. Once the jar is added, all the com.kazurayam.ksbackyard classes become available to my custom keywords.

My custom keyword implementation is a thin layer which has just a few lines of codes. On the other hands my classes in the com.kazurayam.ksbackyard are large and complexed.

I can reuse the jar of com.kazurayam.ksbackyard classes in as many Katalon Projects as I want, because the jar is created outside a Katalon Studio.

Another reason why I developed my classes outside Katalon Studio is that they are large and complicated enough so that they deserved thorough unit testing with testing frameworks such as JUnit and Spock.

I found that I can not test my classes for custom keywords using JUnit in Katalon Studio. So I decided to design 2 layers of my classes:

  1. @keyword classes which are tightly coupled with Katalon Studio API
  2. classes which are independent from Katalon Studio API, and will be called by the classes of (1)

I develop the 1) classes in Katalon Studio — just a few lines. Yes I need to repeat presetting keywords in the Katalon Studio’s menu, but it is not so difficult.

I develop the 2) classes outside Katalon Studio — bunch of lines thoroughly tested with JUnit. These classes are reused in many Katalon projects.

This design (keywords implemented with 2 layers) worked for me.

To be honest, my way was hard to achieve. Not easy at all. It required a few weeks of programming efforts. My implementation is still immature. I have found many outstanding design issues in order to make this design useful for others. But I suppose it would be better than manipulating GlobalVariables.glbl file runtime.

2 Likes