Is there anyway to implement more annotation in Test Event Listener?

- I have some situations that need to take an action (like capture screenshot,…) after each step in a test case.
- I find out that default Test Event Listener of Katalon does not support this situation.
- I don’t want to create a step take screenshot manually after each step.
- Anyone have faced this issue or any idea can help me?

PLEASE HELP.

2 Likes

I need this feature too so badly :frowning:

Please have a look

The purpose of this code is not to take screenshot, but I think you can adapt it to your needs.

2 Likes

Yep thats works fine but still need more custom, its highlight the element before step its take an action any idea how to get the screenshot after step do an action?

@Keyword
public static void pandemic() {
WebUiBuiltInKeywords.metaClass.‘static’.invokeMethod = { String name, args →

  	def result
  	try {
  		result = delegate.metaClass.getMetaMethod(name, args).invoke(delegate, args)

  		if (name in influencedKeywords) {
  			com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.takeScreenshot()
  		}
  	} catch(Exception e) {
  		System.out.println("Handling exception for method $name")
  	}
  	return result
  }

}

This will do the trick

3 Likes

WOW nicee, thanks antoine for help

Hey antoine,

I have a question very appreciate it if you can help me with my question

this code its doesnt work for verify? or i should add somethin in pandemic method?

Thanks

If you are looking at Kazurayam code (https://github.com/kazurayam/HighlightingElementByTestObjectInEachAndEveryStep/blob/master/Keywords/com/kazurayam/ksbackyard/HighlightElement.groovy), simply add the keyword you want to take screenshot after (verify…) in the influencedKeywords list.

private static List<String> influencedKeywords = [
‘click’,
‘selectOptionByIndex’,
‘selectOptionByLabel’,
‘selectOptionByValue’,
‘setEncryptedText’,
‘setText’
]

If you do not want to list every Keyword, you can replace

if (name in influencedKeywords) {
com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.takeScreenshot()
}

by

com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.takeScreenshot()

1 Like

from what i know verfiy keyword need parameter timeout, different with example keyword kazura so what method need to be added parameter timeout?

and also i need the list to because i dont want to screenshot every step in test case only just verfiy things

thanks

Every keyword in this list uses multiple parameters : a testObject and a value at least. You don’t have to care about how many parameters your method has, because they will all be passed to the desired “name” method using “args”.

So i just need to add list of action in influencedKeywords and everything will be screenshot (based on action in influence keywords ) without adding additional param?

and also i got this error when i run the script

2019-02-25 17:04:37.239 DEBUG testcase.SS - 3: verifyElementPresent(findTestObject(“Object Repository/Page_PetClinic a Spring Framework/a_Find owners”))
Handling exception for method verifyElementPresent
java.lang.NullPointerException: Cannot invoke method invoke() on null object
2019-02-25 17:04:37.280 DEBUG testcase.SS - 4: delay(1)
at org.codehaus.groovy.runtime.NullObject.invokeMethod(NullObject.java:91)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:48)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.NullCallSite.call(NullCallSite.java:35)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.reflection.CachedMethod$invoke.call(Unknown Source)

see my error when i tried to run the script, i just add ‘verifyElementPresent’ in the list

You forgot the timeout parameter which is mandatory

I got this error when i add parameter timeout

3: verifyElementPresent(findTestObject(“Object Repository/Page_PetClinic a Spring Framework/a_Find owners”), 0)
2019-02-25 18:19:39.567 WARN c.kms.katalon.core.helper.KeywordHelper - Timeout ‘0’ is invalid. Using default page load timeout: ‘2’
Handling exception for method verifyElementPresent
java.lang.ArrayIndexOutOfBoundsException: 1
at org.codehaus.groovy.runtime.dgmimpl.arrays.ObjectArrayGetAtMetaMethod.invoke(ObjectArrayGetAtMetaMethod.java:41)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)

Then put 10 and you won’t have any warning

ill try tommorow thanks for your help Antoine

hey Antoine

i just realize its not only the warning but the screenshot is not taken the code goes into exception its not invoke my screenshot code

do you have any idea why its go into exception?

full error message is like this

2019-02-26 10:04:38.979 DEBUG testcase.SS - 3: verifyElementPresent(findTestObject(“Object Repository/Page_PetClinic a Spring Framework/a_Find owners”), 10)
TestObject - ‘Object Repository/Page_PetClinic a Spring Framework/a_Find owners’
Handling exception for method verifyElementPresent
java.lang.ArrayIndexOutOfBoundsException: 1
at org.codehaus.groovy.runtime.dgmimpl.arrays.ObjectArrayGetAtMetaMethod.invoke(ObjectArrayGetAtMetaMethod.java:41)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.invoke(PojoMetaMethodSite.java:51)
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:56)
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 com.ss.step.HighlightElement.screenshotAftermethod(HighlightElement.groovy:72)
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.reflection.CachedMethod.invoke(CachedMethod.java:93)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:384)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1024)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:69)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:52)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:154)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:174)
at com.ss.step.HighlightElement$_pandemic_closure1.doCall(HighlightElement.groovy:91)
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.reflection.CachedMethod.invoke(CachedMethod.java:93)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:294)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1024)
at groovy.lang.Closure.call(Closure.java:414)
at org.codehaus.groovy.runtime.metaclass.ClosureStaticMetaMethod.invoke(ClosureStaticMetaMethod.java:62)
at groovy.lang.ExpandoMetaClass.invokeStaticMethod(ExpandoMetaClass.java:1136)
at org.codehaus.groovy.runtime.callsite.StaticMetaClassSite.call(StaticMetaClassSite.java:53)
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:133)
at Script1550474641931.run(Script1550474641931.groovy:19)

can you show us your method screenshotAftermethod please ?

public static void screenshotAftermethod(String action, TestObject testObject){
String a = GlobalVariable.G_Step
String [] b = a.split("-")
int c = b[1].toInteger()
int d = c + 1
GlobalVariable.G_Step = ‘Step-’+d
new File(RunConfiguration.getReportFolder()+"/SS/")
GlobalVariable.RESULTS_DIR = RunConfiguration.getReportFolder()+"/SS/"
WebUI.takeScreenshot(GlobalVariable.RESULTS_DIR+GlobalVariable.G_Step+". “+action.toLowerCase().capitalize()+” - “+getScreenshotName(testObject)+”.png", FailureHandling.STOP_ON_FAILURE)
//WebUI.takeScreenshot("+GlobalVariable.G_Step+". “+action.toLowerCase().capitalize()+” - “+getScreenshotName(testObject)+”.png")
}

This is my method for screenshot Antoine

Your error may come from here. Where do you initialize the value of GlobalVariable.G_Step ?

2 Likes