Dear all master, i got some problem when trying to send mail with attachment. Here is the flow:
I created a custom keyword with .groovy script sendmail.txt (2.5 KB)
Next i call the keyword on the test case, like this:
And i got error like this:
Test Cases/Email/Email File FAILED.
Reason:
java.lang.NoClassDefFoundError: Could not initialize class com.yeehaw.mail.SendMailWithFile
at com.kms.katalon.core.main.CustomKeywordDelegatingMetaClass.invokeStaticMethod(CustomKeywordDelegatingMetaClass.java:46)
at Email File.run(Email File:18)
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.TestCaseMain.runTestCase(TestCaseMain.java:114)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at TempTestCase1585729436239.run(TempTestCase1585729436239.groovy:23)
when i run the script using groovy console, it work fine:
The script in the sendmail.txt does not have any package statement
package com.yeehaw.mail
You should create a file Keywords/com/yeehaw/mail/SendMailWithFile.groovy. You want paste the sendmail.txt into it. And you want to insert a package statement as above.
A typical source of a class as Keyword looks like this:
package com.yeehaw.mail
import ...
class SendMailWithFile {
/** constructor has nothing todo but should be there */
SendMailWithFile() {}
/** keyword implementation as an instance method, Katalon will call it */
@Keyword
public void execute() {
// your codes currently contained in the main() method comes here
....
}
/** main() is used in commandline only, Katalon Studio does not use main() */
public static void main(String[args]) {
SendMailWithFile instance = new SendMailWithFile()
instance.execute();
}
}
And you testcase script will call it like:
import com.yeehaw.mail.SendMailWithFile
SendMailWithFile instace = new SendMailWithFile()
instance.execute()
Im pretty sure my lack of knowledge about katalon and Java is the main problem. to all master, thank you very much. it worked now, using kazurayam advice. Guess there’s a lot more to learn about!
The difference is data type of the argument to the main(…) method.
Your Keyword implementation had @Keyword public static void main(String[] args). Therefore your testcase script was required to pass an argument as Array of String.
new String[]{""} is an instance of Array of String.
However you gave main([]). In Groovy language, [] means an empty instance of java.util.List. [] is NOT an array of java.lang.String. These two data types behaves similarly in some context, but they behave differently in other context. This ambiguity of language may cause confusion.
You specified an unmatching data type(List) as argument to main(String[] args) method. Therefore Groovy could not determine which class to load and which method to invoke. Therefore Groovy threw a Runtime Exception java.lang.NoClassDefFoundError.
The screenshot you provided to us does not show how your Keyword class is named. The test case scripts indicates that the fully qualified class name of your Keyword is:
tomobile_.SendMailWithFile
On the other hand, you have a directory structure:
Keywords/tomobile_/sendMail.groovy
This directory+file name is against the convention. This is not the correct way of declaring the keyword.
First I would recommend you to rename the package tomobile_ to tomobile; as a underline character _ is rarely used in package names. To me a package name with _ looks to be a mistake.
Then, the directory structure of your Keyword class MUST be:
Keywords/tomobile/SendMailWithFile.groovy
Please study the following article about how package/class in Java+Groovy is named and how they relates to the underlying directory structure.