I was not aware of this issue up until now.
I looked into the source of Katalon Studio, and found a way to implement your idea.
com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain has a method StepFailed.
The current source looks like this:
public static stepFailed(String message, FailureHandling flHandling, Throwable t, boolean takeScreenShot)
throws StepFailedException {
KeywordMain.stepFailed(message, flHandling, new StepFailedException(message, t),
new WebUIScreenCaptor().takeScreenshotAndGetAttributes(takeScreenShot));
}
I found that, when WebUI.verifyElementPresent() keyword has got a failure (for example, a HTML element searched for was not found in the page), it will call WebUIKeywordMain.stepFailed() with the argument takeScreenshot with value of true. Therefore a screenshot will be taken regardless which type of FailureHandling is selected.
If Katalon Team changes the code as follows, @Lakshminarayana_D and other people would be happy.
public static stepFailed(String message, FailureHandling flHandling, Throwable t, boolean takeScreenShot) throws StepFailedException {
boolean requireScreenShot = takeScreenShot
if (flHandling == FailureHandling.OPTIONAL) {
requireScreenShot = false
}
KeywordMain.stepFailed(message, flHandling, new StepFailedException(message, t),
new WebUIScreenCaptor().takeScreenshotAndGetAttributes(requireScreenShot));
}
This code will not take screenshot when FailureHandling.OPTIONAL is assigned.
I tried to find out a way to change the method implementation dynamically using Groovy’s MetaProgramming technique. I thought it should be possible in theory. But unfortunately, I could not achieve it. I am still struggling. Anyway, I could not provide an easy-to-use solution for this issue. We need to wait for Katalon Team to address this, and tackle it.
→
==========
update at 19 JUNE 2021
I have found out a thing. I would conclude that it is impossible to modify, by Groovy MetaProgramming technique, WebUI.verifyElementPresent() keyword with FailureHandling.OPTIONAL NOT to take screenshot.
The reason is that the WebUI.verifyElementPresent() keyword’s source is annotated with groovy.transform.CompileStatic as the following:
@CompileStatic
public boolean verifyElementPresent(TestObject to, int timeOut, FailureHandling flowControl) throws StepFailedException {
...
According to the documetation of CompileStatic:
This will let the Groovy compiler use compile time checks in the style of Java then perform static compilation, thus bypassing the Groovy meta object protocol.
I made a custom keyword that dynamically modifies the implementation of com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain using Groovy MetaProgramming technique. Yes, I could do it.
But the WebUI.verifyElementPresent() calls the original(unmodified) WebUIKeywordMain. I could find no way to force WebUI.verifyElementPresent() to call the modified WebUIKeywordMain. It’s because WebUI.verifyElementPresent() is compiled with the @CompileStatic annotation. I will give up.
… I have found out the reason why my codes doesn’t work; now I feel I have understood how Groovy MetaProgramming works in depth.