BDD Testing: Step Definition files doesn't recognise Customkeywords


#1

Hi,
I created a customkeyword and when trying to call it in my step def file, Katalon is unable to recognise it as a class.
Whereas when I use it in my test case, Katalon can easily recognise it.
Is Customkeyword currently being supported in step defs? if not, do you have any plans of incorporating it in the near future?
Thanks
Krish


#2

Hi,
I also had this problem, but I managed to find kind of a work around. If you could provide the script of the Step Definition file, I could be able to help you.


#3

Hello,

I am having this problem too.
Not sure if we have to import some library or something like this, but in test case is easily recognized as Natajaran said.

Can anyone help us?

Best regards!


#4

if you consider a custom keyword is a normal method in a class, could you call it in the script?


#5

@Bogdan Alexandru @Brian Ducson @Mateus Gomes Binatti
So this is what I have in my keywords section
******************************************************************************

class completeLogin {

/**

* Click element

* @param to Katalon test object

*/

@Keyword

def clickElement(TestObject to) {

try {

WebElement element = WebUiBuiltInKeywords.findWebElement(to);

KeywordUtil.logInfo(“Clicking element”)

element.click()

KeywordUtil.markPassed(“Element has been clicked”)

} catch (WebElementNotFoundException e) {

KeywordUtil.markFailed(“Element not found”)

} catch (Exception e) {

KeywordUtil.markFailed(“Fail to click on element”)

}

}

/**

* SendKeys element

* @param to Katalon test objects

*/

@Keyword

def enterText(TestObject to,String text){

try {

WebElement element = WebUiBuiltInKeywords.findWebElement(to);

KeywordUtil.logInfo(“Entering text”)

element.sendKeys(text)

KeywordUtil.markPassed(“Entered Text”)

} catch (WebElementNotFoundException e) {

KeywordUtil.markFailed(“unable to enter text”)

} catch (Exception e) {

KeywordUtil.markFailed(“Fail to enter text”)

}

}

}
******************************************************************************
When calling the above keyword using customkeywords inside my BDD step defs, it looks like this
******************************************************************************

CustomKeywords.‘mohcLoginPage.completeLogin.enterText’(findTestObject(‘MOHCLoginPage/usernameField’), ‘rcjclkxearbrumffp@email.com’)

CustomKeywords.‘mohcLoginPage.completeLogin.enterText’(findTestObject(‘MOHCLoginPage/passwordField’), ‘Tester12’)

CustomKeywords.‘mohcLoginPage.completeLogin.clickElement’(findTestObject(‘MOHCLoginPage/loginButton’))

******************************************************************************
And this is the stacktrace
******************************************************************************

Verification FAILED because (of) (Stack trace: com.kms.katalon.core.exception.StepFailedException: Keyword runFeatureFile was failed (Root cause: Feature file ‘Include/features/REG18.feature’ was failed)

at com.kms.katalon.core.keyword.internal.KeywordMain.stepFailed(KeywordMain.groovy:36)

at com.kms.katalon.core.keyword.internal.KeywordMain.stepFailed(KeywordMain.groovy)

at com.kms.katalon.core.keyword.internal.KeywordMain.runKeyword(KeywordMain.groovy:56)

at com.kms.katalon.core.keyword.internal.KeywordMain$runKeyword.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 com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords.runFeatureFile(CucumberBuiltinKeywords.groovy:45)

at com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords$runFeatureFile$0.callStatic(Unknown Source)

at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallStatic(CallSiteArray.java:56)

at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:194)

at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:214)

at com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords.runFeatureFile(CucumberBuiltinKeywords.groovy:95)

at com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords$runFeatureFile.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 WSVerification1540460451991.run(WSVerification1540460451991:2)

at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:183)

at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:108)

at com.kms.katalon.core.main.WSVerificationExecutor.runScript(WSVerificationExecutor.java:145)

at com.kms.katalon.core.main.WSVerificationExecutor.doExecute(WSVerificationExecutor.java:139)

at com.kms.katalon.core.main.WSVerificationExecutor.processExecutionPhase(WSVerificationExecutor.java:122)

at com.kms.katalon.core.main.WSVerificationExecutor.accessMainPhase(WSVerificationExecutor.java:114)

at com.kms.katalon.core.main.WSVerificationExecutor.execute(WSVerificationExecutor.java:102)

at com.kms.katalon.core.main.TestCaseMain.runFeatureFile(TestCaseMain.java:129)

at com.kms.katalon.core.main.TestCaseMain$runFeatureFile$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 TempTempCase1540460446834.run(TempTempCase1540460446834.groovy:21)

at groovy.lang.GroovyShell.runScriptOrMainOrTestOrRunnable(GroovyShell.java:263)

at groovy.lang.GroovyShell.run(GroovyShell.java:518)

at groovy.lang.GroovyShell.run(GroovyShell.java:507)

at groovy.ui.GroovyMain.processOnce(GroovyMain.java:653)

at groovy.ui.GroovyMain.run(GroovyMain.java:384)

at groovy.ui.GroovyMain.process(GroovyMain.java:370)

at groovy.ui.GroovyMain.processArgs(GroovyMain.java:129)

at groovy.ui.GroovyMain.main(GroovyMain.java:109)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:498)

at org.codehaus.groovy.tools.GroovyStarter.rootLoader(GroovyStarter.java:109)

at org.codehaus.groovy.tools.GroovyStarter.main(GroovyStarter.java:131)

)


#6

The stack trace does not say where is wrong yet.


#7

This is what it will say when it cant find the custom keyword property
****************************************************************************

groovy.lang.MissingPropertyException: No such property: CustomKeywords for class: xxxxxxxxxxxxxxxxxxxx

at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53)

at org.codehaus.groovy.runtime.callsite.GetEffectivePogoPropertySite.getProperty(GetEffectivePogoPropertySite.java:87)

at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:307)

at xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.groovy:84)

at ✽.I complete my online consultation(xxxxxxxxxxxxxxxxxxx.feature:8)


#8

Natarajan,

CustomKeywords can be used in Test Case, Test Suite script. It is out of scope of Custom Keyword, and Step Definition (those place we have to create new classes).

In scope of CustomKeyword, or StepDefinition, you can use it as a normal function.
- Create new instance of the class, and call the method
- Declare the method as Static and use it.

Examples
- Custom keyword:

class ParamTypes {
  @Keyword
  def map_variables(Map <String, String> my_map) {
    println my_map
  }
}

- Calling from Test case:

CustomKeywords.'com.common.types.ParamTypes.map_variables'([('last name') : 'ducson', ('first name') : 'brian'])

- Calling from another custom keyword (or in step definition class)

new ParamTypes().map_variables([('last name') : 'ducson', ('first name') : 'brian'])

#9

@Brian_Ducson thanks for your suggestion but it didnt work either. Can you show me a working example of using a keyword in step defs?


#10

They are working code that I copied from my sample. What is the error you got? Could you take screenshot on how you use it?