Event Listeners

What is Event Listener?
An event listeners are set of functions in a selenium python bindings 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 webdriver 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.

EventFiringWebDriver in python

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

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

Event Listeners present in Selenium Python

AbstractEventListener provides pre and post even listeners, so to use these listeners we have to implement those listeners by inheriting AbstractEventListener
before_change_value_of :
before_change_value_of method will be invoked when we try to change the value of the element, this methods accepts the target element, driver, and the text

after_change_value_of :
after_change_value_of method will be invoked when the target element's values changes, this method accepts target element, driver, and text.

before_click :
before_click method will be invoked before clicking and element with click() method in selenium, this method accepts webelement and webdriver as parameters

after_click :
This method will be invoked after the click() method's operation.

before_find :
before_find method will be invoked before finding the element with findElement method, this method accepts By class parameter and two webelements

after_find :
after_find method will be invoked after the operation of findElement

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

after_navigate_back :
after_navigate_back method will be executed after executing back() method

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

after_navigate_forward :
after_navigate_forward method will be invoked after executing forward() method from Navigation class.

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

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

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

after_navigate_to :
after_navigate_to method will be executed after executing to() method

before_execute_script
before_execute_script method will be executed before executing any javascript code with JavaScriptExecutor

after_execute_script:
after_execute_script method will be executed after executing javascript code.

on_exception
on_exception 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

before_close :
after_close :
before_quit :
after_quit :

Let's cerate a an example program for Listeners in python.

import unittest
import logging
from selenium import webdriver
from selenium.webdriver import Firefox
from selenium.webdriver.support.events import EventFiringWebDriver, AbstractEventListener

class MyListener(AbstractEventListener):
    def before_navigate_to(self, url, driver):
        print("Before navigate to %s" % url)
    def after_navigate_to(self, url, driver):
        print("After navigate to %s" % url)
        
        
class Test(unittest.TestCase):
    def test_logging_file(self):
        
        driver_plain = webdriver.Chrome(executable_path=r'D:\PATH\chromedriver.exe');
        edriver = EventFiringWebDriver(driver_plain, MyListener())
        edriver.get("https://google.com")
                    
if __name__ == "__main__":
    unittest.main()

Complete code for python selenium Listeners


import unittest
import logging
from selenium import webdriver
from selenium.webdriver import Firefox
from selenium.webdriver.support.events import EventFiringWebDriver, AbstractEventListener

class MyListener(AbstractEventListener):
    def before_navigate_to(self, url, driver):  
        print("Before navigating to ", url)
        
    def after_navigate_to(self, url, driver):   
        print("After navigating to ", url)
        
    def before_navigate_back(self, driver): 
        print("before navigating back ", driver.current_url)
    
    def after_navigate_back(self, driver):  
        print("After navigating back ", driver.current_url)
    
    def before_navigate_forward(self, driver):  
        print("before navigating forward ", driver.current_url)
    
    def after_navigate_forward(self, driver):   
        print("After navigating forward ", driver.current_url)
    
    def before_find(self, by, value, driver):   
        print("before find")
    
    def after_find(self, by, value, driver):    
        print("after_find")
    
    def before_click(self, element, driver):    
        print("before_click")
    
    def after_click(self, element, driver): 
        print("after_click")
    
    def before_change_value_of(self, element, driver):  
        print("before_change_value_of")
    
    def after_change_value_of(self, element, driver):   
        print("after_change_value_of")
    
    def before_execute_script(self, script, driver):    
        print("before_execute_script")
    
    def after_execute_script(self, script, driver): 
        print("after_execute_script")
    
    def before_close(self, driver): 
        print("tttt")
    
    def after_close(self, driver):  
        print("before_close")
    
    def before_quit(self, driver):  
        print("before_quit")
        
    def after_quit(self, driver):   
        print("after_quit")
    
    def on_exception(self, exception, driver):  
        print("on_exception")
        
        
class Test(unittest.TestCase):
    def test_logging_file(self):
        
        driver_plain = webdriver.Chrome(executable_path=r'D:\PATH\chromedriver.exe');
        edriver = EventFiringWebDriver(driver_plain, MyListener())
        edriver.get("https://google.com")
        edriver.find_element_by_name("q").sendKeys("Sendkeys with listener");
        edriver.find_element_by_xpath("//input[contains(@value,'Search')]").click();
        edriver.close()
                    
if __name__ == "__main__":
    unittest.main()

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 Protractor Online Training : We have closed registration for training

Please do email to chercher.tech@gmail.com for any queries

If you already registered then please do check your email for the Webex link for the training starting from 15th Nov 2018
 
Join My Facebook Group
Join Group