Events

What is Event ?
Event is any action performed on webpage elements using selenium kotlin, actions like finding an element, clicking an element, navigating to url, raising an exception, changing value in a field, navigating back, navigating forth, excuting an javascript.

Even are present across all programming langauages, in some languages they call it as event sink.

Event Listeners

What is Event Listener?
An event listeners are set of functions in a selenium that waits for an event to occur; that event may be a user clicking or finding, changing value, or an Exception. The listeners are programmed to react to an input or signal.

There is a function to react when an even about to happen, also there is a function to react after event.

In Simple way : If person A tries to slap perform B, person B tries to escape the hit (before event), unfortunately if A slaps person B. Then B reacts to it either by slapping or by keeping mouth shut (after event).

Even though selenium kotlin provides listeners , it doesn't provide details what to do when an event occurs. User must provide all the details what should happened when an event occurs.

For Example: User should implement method for what should happen before and after a click.

Benefits of Event Listeners

  • It allows do something before or after an action. For example, if you want to wait for an ajax element before every click, or log that action into logger, it is possible to do with the event listener.
  • Take screenshots when exception occurred.
  • Log the action of the selenium webdriver
  • To take screenshots on on every action like when you navigate go a page, when an value changes so on
  • To capture values before changing the values
  • Track the urls
  • To check state of the element

EventFiringWebDriver

EventFiringWebDriver is a wrapper on the webdriver Interface, it provides all the methods which webdriver provides.

EventFiringWebDriver provide two more methods to register and unregister the Listeners implementing class

Event Listeners present in Selenium Webdriver

Webdriver provides pre and post even listeners.
beforeAlertAccept(WebDriver arg0) :
This methods will be invoked before accepting an alert, this methods accepts driver as parameter

afterAlertAccept(WebDriver arg0)
this method will be invoked once user accepts the alert with script, this methods accepts driver as parameter

beforeAlertDismiss(WebDriver arg0)
beforeAlertDismiss method will be invoked before an alert is dismissed, this methods accepts driver as parameter

afterAlertDismiss(WebDriver arg0)
afterAlertDismiss method will be invoked after dismissing an alert, this methods accepts driver as parameter

beforeChangeValueOf(WebElement arg0, WebDriver arg1, CharSequence[] arg2) :
beforeChangeValueOf method will be invoked when we try to change the value of the element, this methods accepts the target element, driver, and the text

afterChangeValueOf(WebElement arg0, WebDriver arg1, CharSequence[] arg2) :
afterChangeValueOf method will be invoked when the target element's values changes, this method accepts target element, driver, and text.

beforeClickOn(WebElement arg0, WebDriver arg1) :
beforeClickOn method will be invoked before clicking and element with click() method in selenium, this method accepts webelement and webdriver as parameters

afterClickOn(WebElement arg0, WebDriver arg1) :
This method will be invoked after the click() method's operation.

beforeFindBy(By arg0, WebElement arg1, WebDriver arg2) :
beforeFindBy method will be invoked before finding the element with findElement method, this method accepts By class parameter and two webelements

afterFindBy(By arg0, WebElement arg1, WebDriver arg2) :
afterFindBy method will be invoked after the operation of findElement

beforeNavigateBack(WebDriver arg0) :
beforeNavigateBack this method will be invoked before executing back() method from Navigation class, this method accepts driver as parameter

afterNavigateBack(WebDriver arg0) :
afterNavigateBack method will be executed after executing back() method

beforeNavigateForward(WebDriver arg0)
beforeNavigateForward will be invoked before executing forward() method from Navigation class, this method accepts driver as parameter

afterNavigateForward(WebDriver arg0) :
afterNavigateForward method will be invoked after executing forward() method from Navigation class.

beforeNavigateRefresh(WebDriver arg0) :
beforeNavigateRefresh method will be executed before executing refresh() method from Navigation class, this method accepts driver as parameter

afterNavigateRefresh(WebDriver arg0) :
afterNavigateRefresh method will be executed after refreshing the page with refresh() method from Navigation class

beforeNavigateTo(String arg0, WebDriver arg1)
beforeNavigateTo method will be executed before navigating to nay webpage using to() method from Navigation class, this method accepts String webaddress and driver as parameters

afterNavigateTo(String arg0, WebDriver arg1) :
afterNavigateTo method will be executed after executing to() method

beforeScript(String arg0, WebDriver arg1)
beforeScript method will be executed before executing any javascript code with JavaScriptExecutor

afterScript(String arg0, WebDriver arg1):
afterScript method will be executed after executing javascript code.

onException(Throwable arg0, WebDriver arg1)
onException method will be executed whenever there is an exception occurs in selenium, irrespective of whether user handles the exception or not, this method will be executed

Implement Webdriver Listeners


Steps to implement listeners in webbdriver :

1. We have to implement the Listener Interface in event handling class
2. Register the event handling class

Steps in detail :
1. Create a java Class called ListenerHelper


     class ListenerHelper : WebDriverEventListener{

    override fun beforeAlertAccept(var1: WebDriver){
        println("before Alert Accept")
    }

    override fun afterAlertAccept(var1: WebDriver){
        println("After Alert accept")
    }

    override fun afterAlertDismiss(var1: WebDriver){
        println("After Alert Dismiss")
    }

    override fun beforeAlertDismiss(var1: WebDriver){
        println("Before Alert dismiss")
    }

    override fun beforeNavigateTo(var1: String, var2: WebDriver){
        println("Before navigate to")
    }

    override fun afterNavigateTo(var1: String, var2: WebDriver){
        println("after navigate to")
    }

    override fun beforeNavigateBack(var1: WebDriver){
        println("before navigate back")
    }

    override fun afterNavigateBack(var1: WebDriver){
        println("after navigate back")
    }

    override fun beforeNavigateForward(var1: WebDriver){
        println("bafore navigate forward")
    }

    override fun afterNavigateForward(var1: WebDriver){
        println("after navigate forward")
    }

    override fun beforeNavigateRefresh(var1: WebDriver){
        println("before navigate refresh")
    }

    override fun afterNavigateRefresh(var1: WebDriver){
        println("after navigate refresh")
    }

    override fun beforeFindBy(var1: By, var2: WebElement, var3: WebDriver){
        println("before find by")
    }

    override fun afterFindBy(var1: By, var2: WebElement, var3: WebDriver){
        println("after find by")
    }

    override fun beforeClickOn(var1: WebElement, var2: WebDriver){
        println("Before click on")
    }

    override fun afterClickOn(var1: WebElement, var2: WebDriver){
        println("before click on")
    }

    override fun beforeChangeValueOf(var1: WebElement, var2: WebDriver, var3: Array){
        println("before change of value")
    }

    override fun afterChangeValueOf(var1: WebElement, var2: WebDriver, var3: Array){
        println("after change of value")
    }

    override fun beforeScript(var1: String, var2: WebDriver){
        println("before script")
    }

    override fun afterScript(var1: String, var2: WebDriver){
        println("after script")
    }

    override fun beforeSwitchToWindow(var1: String, var2: WebDriver){
        println("before switch to window")
    }

    override fun afterSwitchToWindow(var1: String, var2: WebDriver){
        println("after switch to window")
    }

    override fun onException(var1: Throwable, var2: WebDriver){
        println("on exception")
    }
}


2. Create class called ActualDriver
3. Create object for Browser


            System.setProperty("webdriver.gecko.driver", "D:\\Eclipse progs\\driverserver\\geckodriver.exe")
    val driver = FirefoxDriver()
            


4. Create object for ListenerHelper inside ActualDriver class


            val listen = ListenerHelper()
            


5. Create Object for EventFiringWebDriver
, it accepts the driver as parameter


            val browser = EventFiringWebDriver(driver)
            


6. Register the Listener onto EventFiringWebDriver object


            browser.register(listen)
            


7. Write the scripts using EventFiringWebDriver object


            browser.get("https://google.com");
            

Complete ActualDriver code:

            fun main(args: Array) {
    System.setProperty("webdriver.gecko.driver", "D:\\Eclipse progs\\driverserver\\geckodriver.exe")
    val driver = FirefoxDriver()
    val listen = ListenerHelper()
    val browser = EventFiringWebDriver(driver)
    browser.register(listen)

    browser.get("https://google.com")
    browser.findElement(By.name("q")).sendKeys("Sendkeys with listener")
    browser.findElement(By.xpath("//input[contains(@value,'Search')]")).click()
}
            


Output : listeners-selenium-webdriver

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
You can also share knowledge by Adding a topic here


Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions

Recent Addition

new tutorial Selenium Online Training : Our next online training course for Selenium with Java starts from 17th December 2018.

You can attend first 3 classes for free, the total course fee is INR 10,000

The course time would be 8.00 PM(IST) for the first three classes

If you are interested to learn, then you can join the course by sending email to chercher.tech@gmail.com

or Register below


 
Join My Facebook Group
Join Group