Importing Custom Keywords classes recursively (within a Custom Keyword)

Hi,

is there an officially supported way in Katalon Studio to call a Custom Keyword within a Custom Keyword that is defined in another class (and possibly also in another package)?

Even the test case method CustomKeywords.‘com.myCompany.myKeyword’() does not seem to work within Custom Keyword code. Or do I have to import some other class first?

Thanks+regards

I was interested in this issue.

Let me show you what I tried:

I made a single Test Case and 2 .groovy files in the Keywords directory.

Keywords/my.a/Greeting.groovy

package my.a
import com.kms.katalon.core.annotation.Keyword
class Greeting {
    @Keyword
    def greet(String someone) {
        return my.b.AI.sayHelloTo(someone)
    }
}

Keywords/my.b/AI.groovy

package my.b
class AI {
    static def sayHelloTo(String someone) {
        return "Hello, ${someone}."
    }
}

TC1:

import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
def str = CustomKeywords.'my.a.Greeting.greet'("Drunda")
WebUI.comment(">>> ${str}")

The TC1 ran fine:

6971.PNG

Hello.png

Hi kazurayam,

oh interesting, thank you!
As I already learned from you in this post even things like following also work - in principle:

Keyword file 1 (more general utils):

package com.myCompany

import regular.stuff.here

public class MyGeneralKeywords {
    @Keyword
    def sayHelloTo(String someone) {
        return "Hello, ${someone}."
    }
}


Keyword file 2 ****(more special utils):

package com.myTeam

import regular.stuff.here
import com.myCompany.MyGeneralKeywords.*

public class MySpecialKeywords extends MyGeneralKeywords {
    @Keyword
    def greet(String someone) {
        return sayHelloTo(someone)
    }
}


But in my particular case, where I try to determine the test executing browser by recursively calling my more general Custom Keyword getBrowserAndVersion (look the 3rd post), this does not work (I get an empty string). - Strangely enough, it works in turn with the Custom Keywords getOperatingSystem and getScreenResolution (which are defined in the same Keyword file). Do you have any idea what might be the cause?

But I still have to try out your approach from above…

Edit: Tried it now. Your approach above seems basically to work the same as extending the general Keyword class with the special. That means, my “general” Custom Keyword getBrowserAndVersion causes the same problems when called recursively from a “special” Custom Keyword: I get an empty string.

getBrowserAndVersion is calling “DriverFactory.getWebDriver()”, and it does not work.

getOperatingSystem and getScreenResolution are not calling “DriverFactory.getWebDriver()”, and these do work.

This would indicate that you need be more careful about the DriveryFactory object. Who instanciates the DriverFactory? In which CLASSLOADER the DriverFactory object is running?

getBrowserAndVersion is calling “DriverFactory.getWebDriver()”, and it does not work.

getOperatingSystem and getScreenResolution are not calling “DriverFactory.getWebDriver()”, and these do work.

Yeah, that’s what I’ve noticed.

This would indicate that you need be more careful about the
DriveryFactory object. Who instanciates the DriverFactory? In which
CLASSLOADER the DriverFactory object is running?

But that’s what I don’t know enough about. I just copied the code together somewhere without understanding exactly why it works like that. Anyway, as you can see in the head of this Keyword file I’ve imported the DriverFactory which comes with Katalon itself:

import com.kms.katalon.core.webui.driver.DriverFactory

Okay, I think it has something else to do with after all: I usually call my Custom Keyword getBrowserAndVersion at the beginning of each Katalon Studio test session and write the result into a global map variable. But if for some reason the test is interrupted in the middle of it, I didn’t want to start a new test session to go through the entire test again up to the current step, and instead resume the test from the current step by instructing Katalon Studio to use the browser that was already open in its current state. And then, if needed (i.e., if the global map variable key does not yet exist), I’ll try to call getBrowserAndVersion recursively. But obviously, however, the Katalons own DriverFactory WebDriver can no longer access the browser object of this interrupted session in this case. Does that sound plausible to you? How can I manage this better?

the Katalons own DriverFactory WebDriver can no longer access the browser object of this interrupted session in this case. Does that sound plausible to you?

Well, I can not examine it myself. Difficult to say anything.

How can I manage this better?

I wouldn’t try to manage any irregularities in tests. I would simply stop the test by the following line. :stuck_out_tongue: :

WebUI.verifyMatch(GlobalVariable.TestingConfig_BrowserAndVersion, '^(Chrome|Firefox).*'),    true, FailureHandling.STOP_ON_FAILURE)

Just Declare another keyword instance in your Keyword class. And then invoke its keyword method. More details

public class Dialog { @Keyword def click() {  WebUI.click(findTestObject('Common/Components/Nav/link_HomePage')) }}public class Page { final dialog = new Dialog() def run() {  ...    this.dialog.click()  ...
 }}