How implement event listener?

Hi,
Does anyone knows how to use and implement AbstractWebDriverEventListener or smth like this? I want to make that after every click will check if there is no errors in console.
So the best way I can find out is event listener. But I do not find how to use it. I even can`t create class only keyword.
PLEASE, HELP

May I ask what is your end goal? What sort of errors are you checking for and if they occur what are you wanting to happen?

.

Edita Markauskaitė said:

Hi,
Does anyone knows how to use and implement AbstractWebDriverEventListener or smth like this? I want to make that after every click will check if there is no errors in console.
So the best way I can find out is event listener. But I do not find how to use it. I even can`t create class only keyword.
PLEASE, HELP

Can I ask where do you get this ‘AbstractWebDriverEventListener’? Currently this listener is not supported directly in Katalon Studio, so I can’t give solutions for this question :slight_smile:

I found it on Selenium. My goal is to call my custom function after every click.

Edita Markauskaitė said:

I found it on Selenium. My goal is to call my custom function after every click.

So it’s not applicable in Katalon Studio because Katalon Studio doesn’t support this Test Listener :slight_smile:

Maybe there is other listener which can help me? Or some other way?

Anyone can help me?

I have the same interests with Edita Markauskaitė. Is there any way to put more annotation in event listener?

I could imagine that it is possible to implement this Selenium method AbstractWebDriverEventListener with your own code by using your own WebDriver according to this guide. If you make any progress in this matter, I would be very interested in your approach.

I tried what Drunda mentioned but got no success.

See my project on Github

I made a test case:

import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import org.openqa.selenium.WebDriverimport org.openqa.selenium.WebElementimport org.openqa.selenium.chrome.ChromeDriverimport org.openqa.selenium.firefox.FirefoxDriver
import org.openqa.selenium.support.events.AbstractWebDriverEventListenerimport org.openqa.selenium.support.events.EventFiringWebDriver
import com.kms.katalon.core.driver.DriverTypeimport com.kms.katalon.core.webui.driver.DriverFactory
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
/**
 * copied from https://www.seleniumeasy.com/selenium-tutorials/webdriver-event-listener-example
 */
class WebEventListener extends AbstractWebDriverEventListener {
	public void beforeClickOn(WebElement element, WebDriver driver) {
		System.out.println("Trying to click on: " + element.toString());
	}
	public void afterClickOn(WebElement element, WebDriver driver) {
		System.out.println("Clicked on: " + element.toString());
	}
}
WebDriver createWebDriver() {
	DriverType dt = DriverFactory.getExecutedBrowser()
	WebUI.comment("executed browser is ${dt.getName()}")
	WebDriver driver = null
	switch (dt.getName()) {
		case 'FIREFOX_DRIVER':
			System.setProperty('webdriver.gecko.driver', DriverFactory.getGeckoDriverPath())
			driver = new FirefoxDriver()
			break;
		case 'CHROME_DRIVER':
			System.setProperty('webdriver.chrome.driver', DriverFactory.getChromeDriverPath())
			driver = new ChromeDriver()
			break;
	}
	return driver
}
// create WebDriver which can fire events
WebDriver driver = createWebDriver()
EventFiringWebDriver e_driver = new EventFiringWebDriver(driver)
e_driver.register(new WebEventListener())
// let Katalon to use the event-firing WebDriverDriverFactory.changeWebDriver(e_driver)
WebUI.navigateToUrl('https://katalon-demo-cura.herokuapp.com/')
WebUI.verifyElementPresent(findTestObject('Object Repository/Page_CURA Healthcare Service/a_Make Appointment'), 10)
WebUI.click(findTestObject('Object Repository/Page_CURA Healthcare Service/a_Make Appointment'))
WebUI.closeBrowser()

I got the flowing error:

11-23-2018 03:54:10 PM - [ERROR]  - Test Cases/TC1 FAILED because (of) (Stack trace:
    java.lang.ClassCastException:       org.openqa.selenium.support.events.EventFiringWebDriver         cannot be cast to org.openqa.selenium.remote.RemoteWebDriver	at
 com.kms.katalon.core.webui.driver.DriverFactory.logBrowserRunData(DriverFactory.java:477)	at
 com.kms.katalon.core.webui.driver.DriverFactory.changeWebDriver(DriverFactory.java:192)	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
 org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite$StaticMetaMethodSiteNoUnwrapNoCoerce.invoke(StaticMetaMethodSite.java:151)	at
 org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite.call(StaticMetaMethodSite.java:91)	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
 Script1542945386835.run(Script1542945386835.groovy:50)	...)

It is failing at

    DriverFactory.changeWebDriver(e_driver)

DriverFactory.changeWebDriver() method requires a RemoteWebDriver object as argument, but I passed an EventFiringWebDriver object; mismatching type. Therefore failed.

If DriverFactory.changeWebDriver() method were designed to accept a org.openqa.selenium.WebDriver object, then it would have been fine. But it is not designed so.

Conclusion

I think we can not use EventFiringWebDriver object in Katalon Studio.

Beta builds will be available next week.

2 Likes

That’s gold for some people, Alex. Deserves its own Topic.

1 Like