it is far from being concrete, it is jus mud.
any bling bling introduced into the testing process it is just another bias, reason for additional headache and flaky tests.
just saying…
I edited the post by Bob_H presented at How to highlight an element on the webpage? - #5 by Bob_H
- I encoded the code with a pair of triple backticks to present it using Code Formating
- I removed redundant Newline characters, to make it compact
- I changed the indentation of lines for better readability
The Bob_H’s code needs a slight change.
wrong:
public class HighlightElement {
static WebDriver driver = DriverFactory.getWebDriver()
@Keyword
public static void run(TestObject objectto) {
try {
WebElement element = WebUiCommonHelper.findWebElement(objectto, 20);
...
right:
public class HighlightElement {
@Keyword
public static void run(TestObject objectto) {
try {
WebDriver driver = DriverFactory.getWebDriver()
WebElement element = WebUiCommonHelper.findWebElement(objectto, 20);
...
My proposal
The driver variable should not be declared as a static class-scoped variable. If you use the keyword in a Test Suite, the keyword will be called multiple times by constituant Test Cases; while the driver varible is initialized only once when the first Test Case is loaded to Java VM. The second and third call to the keyword will refer the driver variable with “Session ID is null”, and will fail.
Rather, You want to use the active instance of WebDriver in each Test Cases. So you want to initialize it everytime you call the run method. Therefore the driver variable should be declared inside the run method.
@grylion54 @kazurayam @anon46315158 Im getting this error whenver I run a Test Suite. And I know that Test Suite is just a collection of Test cases. Hope you can help me with this. The Blinker Code is running If I run it in a test case, but if I Run a test suite Im having this error.
@kazurayam this actually works,.
Can anyone guide me that how to use @Bob_H 's solution in free version of katalon studio. because when I am trying to create a new package or keyword in keywords folder then ask me for enterprise version.
Thanks in advance
Hi there,
Thank you very much for your topic. Please note that it may take a little while before a member of our community or from Katalon team responds to you.
Thanks!
In KS version 9, the use of Keywords is now part of the Enterprise version. You can try the below, but obviously, getting a license is what is preferred by Katalon.
- You should be able to put a method at the top of your Test Case and then use it within your TC without having an Enterprise license. The method should be below the list of import statements at the top of your TC.
Maybe like:
import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
import org.openqa.selenium.WebDriver as WebDriver
import org.openqa.selenium.WebElement as WebElement
import com.kms.katalon.core.webui.driver.DriverFactory as DriverFactory
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import com.kms.katalon.core.webui.common.WebUiCommonHelper as WebHelper
'start of test1 part3'
WebDriver driver = DriverFactory.getWebDriver();
/**
* Put a highlight around the Test Object
* @param to = our test object
*/
def highlightObject(TestObject to){
try {
WebElement element = WebHelper.findWebElement(to, 10)
WebUI.executeJavaScript("arguments[0].setAttribute('style', 'background: yellow; border: 2px solid red;');", Arrays.asList(element))
WebUI.delay(0.5)
WebUI.executeJavaScript("arguments[0].setAttribute('style','border: solid 2px white');", Arrays.asList(element))
} catch (Exception) {
WebUI.comment("Could not find 'element'")
}
}
// Test Case code starts here
- You might be able to use a ‘called Test Case’ to have the method in, or have an ugly Switch-Case block, or multiple if statements, in a ‘called Test Case’ that has multiple routines.
The post of @grylion54 above inspired me to find another path.
I used Katalon Strudio Free v9.4.0.
I made a Groovy class file “Test Listeners/HighlightElement”:
import org.openqa.selenium.JavascriptExecutor
import org.openqa.selenium.WebDriver
import org.openqa.selenium.WebElement
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.webui.common.WebUiCommonHelper
import com.kms.katalon.core.webui.driver.DriverFactory
import com.kms.katalon.core.util.KeywordUtil
class HighlightElement {
public static void run(TestObject objectto) {
WebDriver driver = DriverFactory.getWebDriver()
try {
WebElement element = WebUiCommonHelper.findWebElement(objectto, 20);
for (int i = 0; i < 5; i++) {
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].setAttribute('style','background: yellow; border: 5px solid red;');", element);
}
} catch (Exception e) {
KeywordUtil.markFailed(e.getMessage());
}
}
}
This is essentially a copy of @Bob_H. @Bob_H located his class in the “Keywords” folder. I located the class in the “Test Listeners”. That’s the only significant difference.
I made a Test Case script Test Cases/TC1:
import com.kms.katalon.core.testobject.ConditionType
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
TestObject makeTestObject(String id, String xpath) {
TestObject tObj = new TestObject(id)
tObj.addProperty("xpath", ConditionType.EQUALS, xpath)
}
WebUI.openBrowser('https://katalon-demo-cura.herokuapp.com/')
WebUI.setViewPortSize(800, 600)
TestObject tObj = makeTestObject("ankor Make Appointment", "//a[@id='btn-make-appointment']")
WebUI.verifyElementPresent(tObj, 3)
HighlightElement.run(tObj) // here the Bob_H code is called
WebUI.delay(30)
WebUI.closeBrowser()
When I ran the test case, it worked. The target <a id="btn-make-appointment>" element was highlighted with the border in red, the background in yellow, the characters in white:
Discussion
In Katalon Stduio Free v9.4.0, I could make a plain old Groovy class HighlightElement in the Test Listener folder. The class lacked the annotations for TestListener such as @AfterTestCase; it did not matter. My test case script had no problem in calling the run method of HighlightElement class in the Test Listener folder.
It seems that Katalon team forgot to turn the Test Listener folder to be an Enterprise feature.
I guess that in future they might change the Test Listener folder to be priced. Then the above sample would stop working in Katalon Studio Free. ![]()
@Bob_H’s code had the following package statement:
package com.reusableComponents
Katalon Studio did not allow me to create a package in the "Test Listeners" folder. Therefore my sample code belongs to the default package.
Thank you for your help. By Using your suggestions I solved this for my use.
At first, I create a new test case for reuse named HighlightElement, then call this test case from where I need.
import com.kms.katalon.core.webui.common.WebUiCommonHelper as WebUiCommonHelper
import org.openqa.selenium.WebElement as WebElement
WebElement element = WebUiCommonHelper.findWebElement(highlightObjects, 30)
for (int i = 0; i < 3; i++) {
WebUI.executeJavaScript('arguments[0].style.background=\'yellow\'', Arrays.asList(element))
WebUI.delay(2)
WebUI.executeJavaScript('arguments[0].style.background=\'initial\'', Arrays.asList(element))
}
then I call it from my original script…
WebUI.callTestCase(findTestCase('Utils/HighlightElement'), [('highlightObjects') : findTestObject('Object Repository/Academy_lms_screen/overview_title')],
FailureHandling.STOP_ON_FAILURE)
It is working for me


