Custom Keyword java.lang.ClassNotFoundException

I added a custom Keyword for Reading a PDF Document to test, if my application created a correct pdf report.

But sometimes I get the following error: java.lang.ClassNotFoundException: tools.PDFReader

Maybe another important note I only get the error by executing by a jenkins task or in console mode and I use Firefox as browser.

My Katalon Studio Version: 6.3.2 Build: 4

I used this example for my custom Keyword: https://github.com/Tigger99/ReadPdf-Documents

My code for my keyword:

package tools

import org.apache.pdfbox.pdmodel.PDDocument
import org.apache.pdfbox.text.PDFTextStripper
import org.testng.Assert

import com.kms.katalon.core.annotation.Keyword

public class PDFReader {

@Keyword
def ReadLocalPDF(String PDFFILE)
{
File pdf = new File(PDFFILE);

  PDDocument doc = PDDocument.load(pdf);
  String pdfText = new PDFTextStripper().getText(doc);
  println(pdfText);
  doc.close();

  Assert.assertTrue(pdfText.contains("Radverkehr"));
  Assert.assertTrue(pdfText.contains("Mobilität"));
  Assert.assertTrue(pdfText.contains("xyz"));
  println "PDF IS GOOD TO GO...\r";

}

Code in my Testcase:

import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import static org.junit.Assert.*

import com.kms.katalon.core.model.FailureHandling as FailureHandling
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

import internal.GlobalVariable as GlobalVariable

// Klick the Print-Button
WebUI.click(findTestObject(‘Object Repository/Message_Toolbar/btn_print’), FailureHandling.STOP_ON_FAILURE)

WebUI.delay(GlobalVariable.G_Timeout_Small)

CustomKeywords.‘tools.PDFReader.ReadLocalPDF’(“C:\myfile.pdf”)

Can anyone give me a hint, how to prevent such an error?

Thanks

Andreas

You need to import the following at the top of your script:

import CustomKeywords.‘tools.PDFReader.ReadLocalPDF’()

......................

Or, if you wish to simplify your life a bit:

import tools.PDFReader

and than call the method inside of the script with

PDFReader.ReadLocalPDF(“C:\myfile.pdf”)

Sadly it doesn’t work for me.

First solution with "import CustomKeywords.‘tools.PDFReader.ReadLocalPDF’()!

I got a error by executing the test case.

Console output:

import CustomKeywords.‘tools.PDFReader.ReadLocalPDF’()
^
1 error

Second solution with “import tools.PDFReader”

Elapsed time: 16,298s

Test Cases/Record specific/Meldungen_drucken FAILED.
Reason:
groovy.lang.MissingMethodException: No signature of method: static tools.PDFReader.ReadLocalPDF() is applicable for argument types: (java.lang.String) values:

[C:\Users\xyz.zx\Downloads\2020000012_Oberflaeche_Am_Buttegraben_7_65326_Aarbergen_9aaf27f287e908c4_-AnMelder.pdf]

Possible solutions: ReadLocalPDF(java.lang.String), ReadPDF(java.lang.String)

Now I posted the realname of the pdf file, maybe it matters for the error?

In your TestCase, did you escape the \ ?
You have to use C:\\Users\\username\\...

Yes

CustomKeywords.‘tools.PDFReader.ReadLocalPDF’(“C:\Users\xyz.zx\Downloads\2020000012_Oberflaeche_Am_Buttegraben_7_65326_Aarbergen_9aaf27f287e908c4_-AnMelder.pdf”)

Hi.

You have to escape special character " \ ". Try this: CustomKeywords.'tools.PDFReader.ReadLocalPDF'("C:\\Users\\xyz.zx\\Downloads\\2020000012_Oberflaeche_Am_Buttegraben_7_65326_Aarbergen_9aaf27f287e908c4_-AnMelder.pdf")

Intressting, I have escaped the character, but by copy Paste to this forum it changed from "\\" to only "\".

That’s not the issue.

Again. It works, but sometime I get the java.lang.ClassNotFoundException .

Could it be an issue of memory?

I set up the memory configuration like “[TIP] How to help reduce memory consumption in Katalon Studio v6.3.0 and above” and monitor, if I’ll get the error again.

1 Like

Sadly the java.lang.ClassNotFoundException still appears.

Seems to be a known bug:

@Andreas_Voit,

When you execute katalon command, it requires the <projectName>/bin folder and the <projectName>/Libs to be filled with the *.class files. For example, you should have <projectName>/bin/keyword/tools/PDFReader.class file on the machine where Jenkins task runs.

You said you sometimes see a message java.lang.ClassNotFoundException: tools.PDFReader. This message seems to me implying that somehow <projectName>/bin/keyword/tools/PDFReader.class file is missing on the machine where Jenkins task runs

How do you build the <projectName>/bin/keyword/tools/PDFReader.class file on the machine?

Does your jenkins job clones a repository from your Git remote repository? Does the Git remote repository includes the bin/ directory commited into?

@devalex88, @ThanhTo

I have a simple question.

Does Katalon Studio console mode perform a full-build everytime it is invoked so that it guarantees the <projectName>/bin directory is fulfilled with the latest *.class binaries?

If Katalon Studio console mode DOES NOT perform a full-build, then I think that a Jenkins job would require a Git repository of Katalon project SHOULD NOT ignore bin/ and Libs/. Am I right?

My setting is a little bit tricky :wink:

Katalon Studio is installed on a Windows Client.

Jenkins is running on a Linux Server.

Therefore I created a Jenkins Node for my Windows Client and set up a Remote Directory in which Jenkins could write it files.

I set up the Build-Prozess Number to 1.
It uses as Startmethode Java Web Start via Agent.

In the jenkins task I set up to run a Windows Batch Process with the following command:

C:
cd “C:_D\Katalon_Studio_Windows_64”

katalon -noSplash -runMode=console -projectPath=“C:\Users\andreas.voit\Katalon_Studio\MPF-Test\MPF-Test.prj” -reportFolder=“C:_D\guitests\reports-print” -reportFileName=“JUnit_Report” -retry=0 -testSuitePath=“Test Suites/Print_Record” -executionProfile=“default” -browserType=“Firefox” -apiKey=#####

I have no problems by running build in functions of katalon studio, but sometimes it has problems with the custom keywords.

Hope that helps?

Does your jenkins job clones a repository from your Git remote repository? Does the Git remote repository includes thebin/directory commited into?

In my understanding jenkins is just executing the katalon studio command on my windows client and doesn’t copy any files to the linux server.

OK, I understand it.

What do you mean by this? When you open Katalon GUI on your PC, no problem. When jenkins executes katalon command remotely on your PC, problems sometimes. — right?

Yes, sometime it works.

image

Maybe you are true with the idea that the console mode doesn’t perform a full-build everytime?

Hmm, very strange. I have no more idea why. Perhaps need to find some clue in the Jenkins logs where failure occurred.

Do you mean the console output?

I tried to make a screenshot of the output:

oh shit, seems to be to tiny for reading :wink:

Here is the output of JavaClass Exception Error case:


Test Suites/Print_Record - Firefox - 20190827_175343…0/3(0%)

2019-08-27 17:54:07.832 INFO c.k.katalon.core.main.TestCaseExecutor - END Test Cases/Generell/Login
2019-08-27 17:54:07.839 INFO c.k.katalon.core.main.TestCaseExecutor - --------------------
2019-08-27 17:54:07.848 INFO c.k.katalon.core.main.TestCaseExecutor - START Test Cases/Record specific/Meldungen_drucken
2019-08-27 17:54:07.972 DEBUG testcase.Meldungen_drucken - 1: delay(G_Timeout_Medium)
console.error: BroadcastService:
receivedBroadcastMessage: handler for
remote-settings/monitor_changes
threw error:
Message: Error: Polling for changes failed: http://%(server)s/dummy/blocklist//buckets/monitor/collections/changes/records?_expected=%221566864005222%22 is not a valid URL…
Stack:
remoteSettingsFunction/remoteSettings.pollChanges@resource://services-settings/remote-settings.js:201:13


Test Suites/Print_Record - Firefox - 20190827_175343…1/3(33%)

2019-08-27 17:54:17.980 DEBUG testcase.Meldungen_drucken - 2: if (waitForElementVisible(findTestObject(“Object Repository/ListView/Arrow_3rd_Entry”), G_Timeout_Medium, OPTIONAL))
2019-08-27 17:54:18.127 DEBUG testcase.Meldungen_drucken - 1: click(findTestObject(“Object Repository/ListView/Arrow_3rd_Entry”), STOP_ON_FAILURE)
2019-08-27 17:54:18.499 DEBUG testcase.Meldungen_drucken - 4: click(findTestObject(“Object Repository/Message_Toolbar/btn_print”), STOP_ON_FAILURE)
2019-08-27 17:54:18.818 DEBUG testcase.Meldungen_drucken - 5: delay(G_Timeout_Small)
JavaScript error: , line 0: uncaught exception: 2147746132
JavaScript error: , line 0: uncaught exception: 2147746132
2019-08-27 17:54:23.828 DEBUG testcase.Meldungen_drucken - 6: tools.PDFReader.ReadLocalPDF(“C:\Users\andreas.voit\Downloads\2020000012_Oberflaeche_Am_Buttegraben_7_65326_Aarbergen_9aaf27f287e908c4_-AnMelder.pdf”)
2019-08-27 17:54:23.841 ERROR k.k.c.m.CustomKeywordDelegatingMetaClass - ? tools.PDFReader
2019-08-27 17:54:23.843 ERROR c.k.katalon.core.main.TestCaseExecutor - ? Test Cases/Record specific/Meldungen_drucken FAILED.
Reason:
java.lang.ClassNotFoundException: tools.PDFReader
at com.kms.katalon.core.main.CustomKeywordDelegatingMetaClass.getCustomKeywordClassAndSetMetaClass(CustomKeywordDelegatingMetaClass.java:100)
at com.kms.katalon.core.main.CustomKeywordDelegatingMetaClass.invokeStaticMethod(CustomKeywordDelegatingMetaClass.java:45)
at Meldungen_drucken.run(Meldungen_drucken:40)
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:337)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:328)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:307)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:299)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:233)
at com.kms.katalon.core.main.TestSuiteExecutor.accessTestCaseMainPhase(TestSuiteExecutor.java:169)
at com.kms.katalon.core.main.TestSuiteExecutor.accessTestSuiteMainPhase(TestSuiteExecutor.java:142)
at com.kms.katalon.core.main.TestSuiteExecutor.execute(TestSuiteExecutor.java:91)
at com.kms.katalon.core.main.TestCaseMain.startTestSuite(TestCaseMain.java:157)
at com.kms.katalon.core.main.TestCaseMain$startTestSuite$0.call(Unknown Source)
at TempTestSuite1566921223033.run(TempTestSuite1566921223033.groovy:35)

2019-08-27 17:54:23.845 INFO c.k.katalon.core.main.TestCaseExecutor - END Test Cases/Record specific/Meldungen_drucken

Here is the output if it works:


Test Suites/Print_Record - Firefox - 20190829_083022…0/3(0%)

2019-08-29 08:30:37.848 DEBUG testcase.Login - 10: verifyElementVisible(findTestObject(“Page_Login/Filter_Button_GUI”), STOP_ON_FAILURE)
2019-08-29 08:30:39.612 DEBUG testcase.Login - 11: delay(G_Timeout_Medium, STOP_ON_FAILURE)
2019-08-29 08:30:49.633 INFO c.k.katalon.core.main.TestCaseExecutor - END Test Cases/Generell/Login
2019-08-29 08:30:49.643 INFO c.k.katalon.core.main.TestCaseExecutor - --------------------
2019-08-29 08:30:49.643 INFO c.k.katalon.core.main.TestCaseExecutor - START Test Cases/Record specific/Meldungen_drucken
2019-08-29 08:30:49.788 DEBUG testcase.Meldungen_drucken - 1: delay(G_Timeout_Medium)


Test Suites/Print_Record - Firefox - 20190829_083022…1/3(33%)

2019-08-29 08:30:59.800 DEBUG testcase.Meldungen_drucken - 2: if (waitForElementVisible(findTestObject(“Object Repository/ListView/Arrow_3rd_Entry”), G_Timeout_Medium, OPTIONAL))
2019-08-29 08:30:59.961 DEBUG testcase.Meldungen_drucken - 1: click(findTestObject(“Object Repository/ListView/Arrow_3rd_Entry”), STOP_ON_FAILURE)
2019-08-29 08:31:00.402 DEBUG testcase.Meldungen_drucken - 4: click(findTestObject(“Object Repository/Message_Toolbar/btn_print”), STOP_ON_FAILURE)
2019-08-29 08:31:00.943 DEBUG testcase.Meldungen_drucken - 5: delay(G_Timeout_Small)
JavaScript error: , line 0: uncaught exception: 2147746132
JavaScript error: , line 0: uncaught exception: 2147746132
2019-08-29 08:31:05.949 DEBUG testcase.Meldungen_drucken - 6: tools.PDFReader.ReadLocalPDF(“C:\Users\andreas.voit\Downloads\2020000012_Oberflaeche_Am_Buttegraben_7_65326_Aarbergen_9aaf27f287e908c4_-AnMelder.pdf”)
PDF IS GOOD TO GO…

2019-08-29 08:31:07.060 INFO k.k.c.m.CustomKeywordDelegatingMetaClass - tools.PDFReader.ReadLocalPDF is PASSED
2019-08-29 08:31:07.061 INFO c.k.katalon.core.main.TestCaseExecutor - END Test Cases/Record specific/Meldungen_drucken

Seems to be a web-driver issue: https://stackoverflow.com/questions/54091162/message-error-polling-for-changes-failed-networkerror-when-attempting-to-fetc

The solution from the other post is to insert a WebDriverWait Command.

But I’m not sure how I could insert WebDriverWait into my testcase confortable. I mean, I don’t want to insert the raw selenium code into each testcase. And using a custome keyword is not an option, because it is the problem.

I want to confirm if the PDRReader.class file is there when the test failed.

In the windows *.bat file, how about calling dir command to list the <projectName>/bin/keyword/tools directory, as follows

cd “C:_D\Katalon_Studio_Windows_64”

dir /S "C:\Users\andreas.voit\Katalon_Studio\MPF-Test\MPF-Test\bin\keyword"

katalon -noSplash -runMode=console -projectPath=“C:\Users\andreas.voit\Katalon_Studio\MPF-Test\MPF-Test.prj” -reportFolder=“C:_D\guitests\reports-print” -reportFileName=“JUnit_Report” -retry=0 -testSuitePath=“Test Suites/Print_Record” -executionProfile=“default” -browserType=“Firefox” -apiKey=8fa89a0d-f308-4cd0-b2a9-abec092356ec

… the class file may not be there somehow. …

Yes, you are correct.

After a failed execution by executing “dir /S”-Statement I got the error, that the directory cannot be found.

Really? I can’t believe it. Why?

It is unlikely that Katalon Studio deletes the bin directory. What else?