WebDriverEventListener

Want to setup Highlight web element globally, instead of calling the custom keyword (highlight function mentioned) before every step.

To use this globally, am trying to implement WebEventDriverListener under folder Listener and have entered javascript code to highlight under afterfindby method of it.

But to use this, we need to register this in other java class file, I tried creating a java class under keyword and tried to register this, it’s not working.

Want to know how to implement WebEventDriverListener

To implement the WebEventDriverListener interface and use it to highlight web elements globally in Katalon Studio, you can follow these steps:

  1. Create a new Java class in your project and implement the WebEventDriverListener interface.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.events.WebDriverEventListener;

public class MyWebEventListener implements WebDriverEventListener {
    // Implement the methods of the WebDriverEventListener interface
}

  1. Override the afterFindBy method of the WebDriverEventListener interface and add the JavaScript code to highlight the web element.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.events.WebDriverEventListener;

public class MyWebEventListener implements WebDriverEventListener {

    @Override
    public void afterFindBy(WebElement element, WebDriver driver) {
        // Add JavaScript code to highlight the web element
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("arguments[0].style.border='3px solid red'", element);
    }

    // Other methods of the WebDriverEventListener interface
}

  1. In your test script, you can register the MyWebEventListener class as an event listener for the WebDriver instance.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.events.EventFiringWebDriver;

WebDriver driver = DriverFactory.getWebDriver();
EventFiringWebDriver eventDriver = new EventFiringWebDriver(driver);
MyWebEventListener eventListener = new MyWebEventListener();
eventDriver.register(eventListener);

  1. Use the eventDriver instance instead of the driver instance to perform web actions in your test script. The afterFindBy method of the MyWebEventListener class will be called after every web element is found, and the web element will be highlighted.

I hope this helps. Let me know if you have any further questions or need more assistance.