Is it possible to call one custom keyword function from another Custom keywords?

Hi ,

I am trying to call method from one custom keywords to another Custom keywords functions

But i am getting below error,

05-30-2018 02:59:02 PM - [START] - Start action : mandatoryfield_Validation.Mandatoryfieldvalidation.textfieldvalidation

05-30-2018 02:59:02 PM - [INFO] - Finding Test Object with id ‘Object Repository/HelpDeskModule/Master/Team/Page_eFACiLiTY/input_Txt_Code’

05-30-2018 02:59:02 PM - [ERROR] - No such property: CustomKeywords for class: mandatoryfield_Validation.Mandatoryfieldvalidation

05-30-2018 02:59:02 PM - [END] - End action : mandatoryfield_Validation.Mandatoryfieldvalidation.textfieldvalidation

05-30-2018 02:59:02 PM - [ERROR] - Test Cases/HelpDeskModule/Master/Team_/Team FAILED because (of) Variable ‘CustomKeywords’ is not defined for test case.

Here is my snippet:

public class Mandatoryfieldvalidation {

//

private static Connection connection = null;

private static String columntype;

private static int num;

private static int datatypevalue

def driver = DF.getWebDriver()

KeywordLogger logger = new KeywordLogger()

JavascriptExecutor js = ((driver) as JavascriptExecutor)

//Method for validating textfield

@Keyword

def void textfieldvalidation()

{

CustomKeywords.‘alerthandling.verifyalertpresent.verifyalert’()

}

Hello @7481-Divya ,

The first issue I noticed with your code snippet is the single-quotes after CustomKeywords. The documentation here shows they are not needed and would probably cause your keyword reference to be treated as a string. CustomKeywords is not a function, so it would not take parameters.

Second, you might need to reference the “alerthandling” package within the new package. Check the import section of a Test Case that uses one of your custom keywords to see if it has a line for CustomKeywords.

Another idea would be to combine the two packages. That should guaranty that you can reference “verifyalertpresent.verifyalert” within Mandatoryfieldvalidation…

I’m no expert in Katalon Studio, but since you had no other replies yet, I though I would offer some ideas. Best of luck!

1 Like

Divya,

Try to call directly _alerthandling.verifyalertpresent.verifyalert() _method. CustomKeywords is not a class, it is just a kind of Groovy wrapper for custom keywords.

1 Like

As Marek mentions, CustomKeywords are not really a class - they are a set of auto-generated functions (from your custom classes) that live in a CustomKeywords.groovy file. That file is loaded by Katalon test files at run time.

It appears that you only need to access CustomKeywords using the quotes when using them in test files.

From other CustomKeyword files, you can access the classes/functions directly, just like you would if you were programming any other groovy library or application.

I was hoping i could get more clarification on this issue. I read the following thread, and the documentation here; https://docs.katalon.com/display/KD/Import+Custom+Keywords+classes+recursively

However, I am getting an error when i try this example:

I have a package ‘utilities’ that has a keyword file ‘core’, with the following keyword definition:

package utilities

public class core {

_ @Keyword //also doesn’t work without the keyword_
_ static def takeScreenShot(String pref = ‘YES’) {_
_ if (pref.toUpperCase() == ‘YES’){_
_ WebUI.takeScreenshot()_
_ }_
_ }_
}

However, it doesn’t work when I try to use the keyword in another keyword file, information below:

package mainS3

public class portal {

@Keyword

def navigateBookmarks(List menu_path, takeScreenShot = YES)

. code

. code

. code, etc

utilities.core.takeScreenShot(takeScreenShot)

}

I receive the following error on a test case using the keyword

Test Cases/playground/RQC_navigate FAILED because (of) Variable ‘utilities’ is not defined for test case.

Any help? Sorry, i am new to the Java world. I have had a lot of success using keywords, but this is my first time trying to call one from a different package.

Thanks!

hello,
i find solution for calling Custom keywords from each other in your case:

(new alerthandling.verifyalertpresent()).verifyalert()

@benjamin

(new utilities.core()).takeScreenShot(takeScreenShot)

will do the magic

18 Likes

@Andrej Podhajský

Your solution worked perfectly, I can’t thank you enough!!! This should be placed into the official documentation.

1 Like

you are welcome

Andrej Podhajský said:

hello,
i find solution for calling Custom keywords from each other in your case:

(new alerthandling.verifyalertpresent()).verifyalert()

@benjamin

(new utilities.core()).takeScreenShot(takeScreenShot)

will do the magic

It works like magic, Thanks @"Andrej Podhajský"

Or you can use

UserInfoGeneraror ug = new UserInfoGeneraror();ug.name_gen(findTestObject('OKAL/BO/AddNewSender_Page/id_DateOfBirth'))

Rustam Ismayilov said:

Or you can use

UserInfoGeneraror ug = new UserInfoGeneraror();
ug.name_gen(findTestObject('OKAL/BO/AddNewSender_Page/id_DateOfBirth'))

errr… what?

@Andrej Podhajský your solution worked like magic for me too! thank you :slight_smile:

HI, I ran into the same issue, but when i apply the magic solution below, it seems that the function/method is not ran and the test case passes. It ignores this line it seems
new clientAccount.ClientAccount().getRowColumnTable(GlobalVariable.switchToClientAccount)

@Keyword
def verifySuccessfulLogin(String username){
//if(WebUI.verifyOptionPresentByLabel(findTestObject(adminIdentityProvider), ‘Yahoo!’, false, 10, FailureHandling.OPTIONAL)){
if (WebUI.verifyElementPresent(findTestObject(cloudAdmin), 0,FailureHandling.OPTIONAL) && (WebUI.verifyElementPresent(findTestObject(username), 0,FailureHandling.OPTIONAL))) {
KeywordUtil.markPassed(‘SUCCESS: Login to the web portal was successful’)
} else {
println(‘ERROR: Login to the web portal was unsuccessful’)
//switch to Cloud admin account in case another account type is logged in using the same email address
new clientAccount.ClientAccount().getRowColumnTable(GlobalVariable.switchToClientAccount)
}
}

Solved my problem. It was referencing the wrong table and the block of code never got executed.