How to call a customkeyword in to a test case

Hello there,
I am trying to call a customkeyword into a test case and using the methods in that keyword in test case script. this is my script

import ciautils.ciaUtilities

utilityPage = new ciaUtilities()
when I use something like this it’s working
utilityPage.navigateToPage(GlobalVariable.careersHomeMenu)

But when I try to use that class within any methods in the test case it’s not working

@Keyword
def clickonLearnMore() {
utilityPage.navigateToPage(GlobalVariable.careersHomeMenu)
}

This is the error I am getting

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script1580321498397.groovy: 104: Apparent variable ‘utilityPage’ was found in a static scope but doesn’t refer to a local variable, static field or class. Possible causes:
You attempted to reference a variable in the binding or an instance variable from a static context.
You misspelled a classname or statically imported field. Please check the spelling.
You attempted to use a method ‘utilityPage’ but left out brackets in a place not allowed by the grammar.
@ line 104, column 5.
utilityPage.navigateToPage(GlobalVariable.careersHomeMenu)

How about initializing utilityPage first before using it ?

@Keyword
def clickonLearnMore() {
   utilityPage = new ciaUtilities();
   utilityPage.navigateToPage(GlobalVariable.careersHomeMenu)
}

I was doing the same but I have to do that in every method where ever I need to use the keyword. instead I thought of initializing at the top of the page similar to how we define something like

public JavascriptExecutor getJs() {
JavascriptExecutor js = (JavascriptExecutor)driver;
return js;
}

@Keyword
def somekeyword(){
js.executeScript(“window.scrollBy(0,5)”,"")
}

Will it be possible to do so for the keywords.

I am a little confused about the topology you intended for your code structure. What follows is a suggestion based on how I do things, using your names (toponyms, I guess).

Make your custom Keywords “static”. Import your package as “static”.

Test Case:

import static ciautils.ciaUtilities.* // import everything statically

// ...

navigateToPage(GlobalVariable.careersHomeMenu)

Custom class

package ciautils
public class ciaUtilities {

  //@Keyword is not needed - can be deleted
  def static navigateToPage(String URL) {
    // perhaps WebUI.navigateToURL(URL)
  }
}

Note: Using static methods (and If you don’t use Manual View) you don’t need @Keyword.

1 Like

@Russ_Thomas and @ThanhTo, thanks for your help. that worked me and also now I am able to clean up a lot of my script that can be understandable by others too.

2 Likes

Follow the steps below in order to use your defined custom keywords in Manual view of a Test Case:
Open a test case in Manual view, then select to add Custom Keyword from command toolbar.
A new test step is added with a list of all defined custom keywords. Select your recently created keyword here.
Admin

1 Like

Hi @Lewis-H, I was doing the same before, but when ever I want to open declaration on particular method its not opening the custom keyword class page. the above solution helped me as how I exactly wanted.