Tests are failing with "cannot be cast to groovy.lang.GroovyObject"

hi,
I am trying to develop test scripts using Katalon. Currently I have 6.12 on my local(tried with the latest version it failed so had to revert to 6.12 version).

The test scripts were working fine. Now all of a sudden all the tests are failing with below error.

2019-08-01 18:51:53.902 ERROR k.k.c.m.CustomKeywordDelegatingMetaClass - ❌ aem_api._Author_Api cannot be cast to groovy.lang.GroovyObject
2019-08-01 18:51:53.924 ERROR c.k.katalon.core.main.TestCaseExecutor   - ❌ Test Cases/LoginAndCreatePage FAILED.
Reason:
java.lang.ClassCastException: aem_api_Author_Api cannot be cast to groovy.lang.GroovyObject
	at com.kms.katalon.core.main.CustomKeywordDelegatingMetaClass.invokeStaticMethod(CustomKeywordDelegatingMetaClass.java:45)
	at LoginAndCreatePage.run(LoginAndCreatePage:16)
	at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
	at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
	at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:336)
	at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:327)
	at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:306)
	at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:298)
	at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:232)
	at com.kms.katalon.core.main.TestSuiteExecutor.accessTestCaseMainPhase(TestSuiteExecutor.java:129)
	at com.kms.katalon.core.main.TestSuiteExecutor.accessTestSuiteMainPhase(TestSuiteExecutor.java:112)
	at com.kms.katalon.core.main.TestSuiteExecutor.execute(TestSuiteExecutor.java:81)
	at com.kms.katalon.core.main.TestCaseMain.startTestSuite(TestCaseMain.java:157)
	at com.kms.katalon.core.main.TestCaseMain$startTestSuite$0.call(Unknown Source)
	at TempTestSuite1564665709091.run(TempTestSuite1564665709091.groovy:35)

I have tried to pull the latest code from GIT repo, tried to reinstall Katalon 6.12. But all my efforts are in vain.

Any help would be appreciated.

Thanks and regards,
Karthik

Please share the test code for LoginAndCreatePage

Please find the keyword as shown below:

        package cmd_api
        import com.kms.katalon.core.annotation.Keyword
        import com.kms.katalon.core.util.KeywordUtil


        import aem_login.LoginToAEM
           import internal.GlobalVariable


        /**
         * This class is used to hold all the keywords which are related to AEM Author API's
         *
         */
        class Aem_Author_Api {
        	String cookie;
        	LoginToAEM loginToAem;
        	



        	Aem_Author_Api() {
         
        		loginToAem=new LoginToAEM()
        	}


        	/**
        	 * This method is used to login to aem and fetch the login_token cookie which later would be used across all the API's
        	 * @param userName
        	 * @param password
        	 * @param authorUrl
        	 * @param loginResource
        	 * @return
        	 */
        	@Keyword
        	public String LoginToAemUsingApiAndFetchLoginCookie(String userName,String password,String authorUrl,String loginResource) {
        		String [] responseHeaders=loginToAem.loginAndFetchCookieUsingApi(userName,password,authorUrl,loginResource)
        		GlobalVariable.cookie=cookie=responseHeaders[0]
        		KeywordUtil.logInfo("Got the cookie login_token for the user "+userName+" and the value is: "+GlobalVariable.cookie)
        		return cookie;
        	}
        }

package aem_login

import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject

import com.kms.katalon.core.testobject.ResponseObject
import com.kms.katalon.core.util.KeywordUtil
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS

import internal.GlobalVariable

public class LoginToAEM {

	/**
	 * This method is used to send the request to AEM author and fetch the cookie from the response headers
	 * @param userName
	 * @param password
	 * @param authorUrl
	 * @param loginResource
	 * @return
	 */
	public String[] loginAndFetchCookieUsingApi(String userName,String password,String authorUrl,String loginResource) {
		KeywordUtil.logInfo("Request recieved : "+authorUrl+"/"+loginResource)
		ResponseObject response = WS.sendRequestAndVerify(findTestObject('login_to_aem', [('userName') : userName, ('password') :password
			, ('authorUrl'):authorUrl,('loginResource'):loginResource]))
		String [] responeHeaders=response.getHeaderFields().get('Set-Cookie').get(0).split(";")
	}
}

The error is coming from LoginAndCreatePage. Again, can you share this code?

hi,
Below is the test case which you had asked for.

import internal.GlobalVariable as GlobalVariable
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile
import com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords as CucumberKW
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint
import com.kms.katalon.core.model.FailureHandling as FailureHandling
import com.kms.katalon.core.testcase.TestCase as TestCase
import com.kms.katalon.core.testdata.TestData as TestData
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.checkpoint.Checkpoint as Checkpoint

GlobalVariable.cookie = CustomKeywords.'aem_api.Aem_Author_Api.LoginToAemUsingApiAndFetchLoginCookie'(userName, password, 
    authorUrl, loginResource)

cookie = GlobalVariable.cookie

CustomKeywords.'aem_api.Aem_Author_Api.createPageUsingApiBasedOnTemplateAndLocationSpecified'(authorUrl, cookie, template, 
    parentPath, pageName, pageTitle, createPageResource)

CustomKeywords.'aem_api.Aem_Author_Api.logoutOfAemUisngApi'(authorUrl, logoutResource, cookie, userAgent)

So it’s complaining about this line:

GlobalVariable.cookie = CustomKeywords.'aem_api.Aem_Author_Api.LoginToAemUsingApiAndFetchLoginCookie'(userName, password, authorUrl, loginResource)

Yet the error message is complaining about a resource identifier that differs from this:

cms_api._Author_Api cannot be cast to groovy.lang.GroovyObject

So there’s a discrepancy between what the compiler thinks you are trying to do vs. what your current script shows:

cms_api._Author_Api

vs.

aem_api.Aem_Author_Api

Have you recently renamed any packages/classes in your Custom Keywords? The best quick fix I can recommend is this:

… which basically recompiles all of your project code.

1 Like

Hi @Brandon_Hein,

I tried the above approach.

  1. Deleted bin,classpath,libs,project file from the explorer. Opened katalon and opened the project again still i faced the same issue.
  2. Tried to clone the project in a different folder and also install katalon in a different folder. Still same issue.

My observation is for some reason the Custom keywords in the test case in the editor were highlighting in pink now it appears in black. Not sure if this has anything to do with the error.

logs:
2019-08-02 10:04:15.451 ERROR k.k.c.m.CustomKeywordDelegatingMetaClass - :x: aem_api.Aem_Author_Api cannot be cast to groovy.lang.GroovyObject
2019-08-02 10:04:15.468 ERROR c.k.katalon.core.main.TestCaseExecutor - :x: Test Cases/LoginAndCreatePage FAILED.
Reason:
java.lang.ClassCastException: aem_api.Aem_Author_Api cannot be cast to groovy.lang.GroovyObject
at com.kms.katalon.core.main.CustomKeywordDelegatingMetaClass.invokeStaticMethod(CustomKeywordDelegatingMetaClass.java:45)
at LoginAndCreatePage.run(LoginAndCreatePage:16)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:336)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:327)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:306)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:298)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:232)
at com.kms.katalon.core.main.TestSuiteExecutor.accessTestCaseMainPhase(TestSuiteExecutor.java:129)
at com.kms.katalon.core.main.TestSuiteExecutor.accessTestSuiteMainPhase(TestSuiteExecutor.java:112)
at com.kms.katalon.core.main.TestSuiteExecutor.execute(TestSuiteExecutor.java:81)
at com.kms.katalon.core.main.TestCaseMain.startTestSuite(TestCaseMain.java:157)
at com.kms.katalon.core.main.TestCaseMain$startTestSuite$0.call(Unknown Source)
at TempTestSuite1564720451317.run(TempTestSuite1564720451317.groovy:35)

Stuck with this. Any suggestion?

Resolved the issue finally.

I compared the files with sample tests. Deleted bin, libs. Deleted the test scripts and test cases(had saved a copy of it earlier).

Changed the project file to previous version. Finally when i opened the project and added back the test scripts and test cases the script started to work.

Not sure what was the reason of failure and why it got resolved

This issue is recurring. After executing the tests again i got the same error.
Seems like a bug with Katalon 6.1.2

Attaching the katalon log for reference.

katalon.log (229.3 KB)

Hi @chicool.viper

So I am trying to reproduce your issue.

Here’s the custom keyword’s content and the package structure.

Here’s the test case, I simplified some code since the important point here is the custom keyword is being able to be called correctly.

Here’s the result of executing the test. It is successfully executed.

Can you tell me if any difference stands out to you ?

One more thing to try: In your test case, right-click anywhere to open a context and menu and choose Source > Organize Imports. I think it’s related in some ways, but I can’t think of a concrete relation yet, but please do try.

Cheers !

hi @ThanhTo,

I tried with “Organize import” option, it did not resolve my issue.

For the sake of testing i just created a helloKeyword as shown below and tried to call that in my test case. Observed the same issue.

Keyword:

Test case:

Console log:
2019-08-03 19:09:25.841 DEBUG testcase.LoginAndCreatePage - 1: aem_api.Aem_Author_Api.helloKeyWord()
2019-08-03 19:09:26.235 ERROR k.k.c.m.CustomKeywordDelegatingMetaClass - :x: aem_api.Aem_Author_Api cannot be cast to groovy.lang.GroovyObject
2019-08-03 19:09:26.254 ERROR c.k.katalon.core.main.TestCaseExecutor - :x: Test Cases/LoginAndCreatePage FAILED.
Reason:
java.lang.ClassCastException: aem_api.Aem_Author_Api cannot be cast to groovy.lang.GroovyObject
at com.kms.katalon.core.main.CustomKeywordDelegatingMetaClass.invokeStaticMethod(CustomKeywordDelegatingMetaClass.java:45)
at LoginAndCreatePage.run(LoginAndCreatePage:1)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:336)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:327)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:306)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:298)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:232)
at com.kms.katalon.core.main.TestSuiteExecutor.accessTestCaseMainPhase(TestSuiteExecutor.java:129)
at com.kms.katalon.core.main.TestSuiteExecutor.accessTestSuiteMainPhase(TestSuiteExecutor.java:112)
at com.kms.katalon.core.main.TestSuiteExecutor.execute(TestSuiteExecutor.java:81)
at com.kms.katalon.core.main.TestCaseMain.startTestSuite(TestCaseMain.java:157)
at com.kms.katalon.core.main.TestCaseMain$startTestSuite$0.call(Unknown Source)
at TempTestSuite1564839561744.run(TempTestSuite1564839561744.groovy:35)

Thanks and regards,
Karthik

Hi @chicool.viper

That’s weird. May I ask for a sample project of you where this problem occurs so I can see what’s going on ?

Cheers !

hi @ThanhTo,

please find the the zip file of the project for your reference.

katalon.zip (44.7 KB)

Cheers!!

Hi @chicool.viper

I used 6.1.2 to open and run your test but it still passes successfully. Can you try to download 6.3.0 and try it out ? It’s best if you try several different versions to see if any of it works, then we can track down the changes that could have caused this problem.

Cheers !

Hi @ThanhTo,

I have already tried with 6.12, 6.06, 6.2 (Test execution is failing on starting the test suite) and 5.6. it was the same issue in all these versions.

Haven’t tried with 6.3

Will do that and let you know.

Regards,
Karthik

Precisely which files and directories deleted? Could you list the names?

Whole folder of bin, libs, classpath file, and build folder.

I am using Gradle to covert the keywords to jar and also I am using rest assured external jar. Not sure that is causing issue.

It must be something in that chain. @ThanhTo said “That’s weird” and I agree. It has to be something unusual because no one else is seeing an issue anything like this.

Perhaps you should document all the steps you take with Gradle and jar production because something (or more than one thing) is not being recompiled/rebuilt correctly. And please, be very specific about all the build steps so that @ThanhTo and the team can try to replicate the issue.

@Russ_Thomas @ThanhTo,

Below are the steps I perform:

  1. Checkout to a branch from repo
  2. Create a package under keyword
  3. Create a class and add a method in the package created previously
  4. Create object of the class in Aem_author_api class present in aem_api package
  5. Create a method and add @keyword annotation to the method
  6. Call method created in step 3 within the method created in step 5
  7. Call the method created in step 5 as part of the test case under test case folder

if everything works fine. Go to the folder location which has gradle file and execute (the zip i shared earlier has the gradle file)“gradle katalonpluginpackage”

Once build is successful, copy the jar created in gradle folder and paste that into plugin folder.
Then push the changes into git repo.

This is the usual step i followed.

Hope that helps.

which gradle folder? first time i heard katalon has such feature suported officialy

i have a strong feeling somebody is mixing docs of creating plugins with running testcases …