Events

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

Even are present across all programming languages, 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 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.

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

Read QR Code using Selenium

EventFiringWebDriver

EventFiringWebDriver is a wrapper on the webdriver, 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 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

Take Page Screenshots and Compare in selenium

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


        import org.openqa.selenium.By;
        import org.openqa.selenium.WebDriver;
        import org.openqa.selenium.WebElement;
        import org.openqa.selenium.support.events.WebDriverEventListener;
        
        public class ListenerHelper implements WebDriverEventListener{
        
            @Override
            public void afterAlertAccept(WebDriver arg0) {
                System.out.println("Alert Accepted");
            }
        
            @Override
            public void afterAlertDismiss(WebDriver arg0) {
                System.out.println("Alert Dismissed");
            }
        
            @Override
            public void afterChangeValueOf(WebElement arg0, WebDriver arg1, CharSequence[] arg2) {
                System.out.println("The value in the element after changing : "+arg2[0]);
            }
        
            @Override
            public void afterClickOn(WebElement arg0, WebDriver arg1) {
                System.out.println("Element clicked");
            }
        
            @Override
            public void afterFindBy(By arg0, WebElement arg1, WebDriver arg2) {
                System.out.println("Element found");
            }
        
            @Override
            public void afterNavigateBack(WebDriver arg0) {
                System.out.println("Navigating back to : "+arg0.getCurrentUrl());
            }
        
            @Override
            public void afterNavigateForward(WebDriver arg0) {
                System.out.println("Navigating forward to : "+arg0.getCurrentUrl());
            }
        
            @Override
            public void afterNavigateRefresh(WebDriver arg0) {
                System.out.println("Url after refreshing : "+arg0.getCurrentUrl());
            }
        
            @Override
            public void afterNavigateTo(String arg0, WebDriver arg1) {
                System.out.println("navigating to url : "+arg0);
            }
        
            @Override
            public void afterScript(String arg0, WebDriver arg1) {
                System.out.println("Executed javascript : "+arg0);
            }
        
            @Override
            public void beforeAlertAccept(WebDriver arg0) {
                System.out.println("Before accepting alert");
            }
        
            @Override
            public void beforeAlertDismiss(WebDriver arg0) {
                System.out.println("Before dismissing alert");
            }
        
            @Override
            public void beforeChangeValueOf(WebElement arg0, WebDriver arg1, CharSequence[] arg2) {
                System.out.println("Before changing the value : "+arg2[0]);
            }
        
            @Override
            public void beforeClickOn(WebElement arg0, WebDriver arg1) {
                System.out.println("Trying to click on the element");
            }
        
            @Override
            public void beforeFindBy(By arg0, WebElement arg1, WebDriver arg2) {
                System.out.println("Trying to find the element");
            }
        
            @Override
            public void beforeNavigateBack(WebDriver arg0) {
                System.out.println("Url before navigating back : "+arg0.getCurrentUrl());
                
            }
        
            @Override
            public void beforeNavigateForward(WebDriver arg0) {
                System.out.println("Url before navigating forard");
            }
        
            @Override
            public void beforeNavigateRefresh(WebDriver arg0) {
                System.out.println("Url before refreshing page : "+arg0.getCurrentUrl());
            }
        
            @Override
            public void beforeNavigateTo(String arg0, WebDriver arg1) {
                System.out.println("Current Ur : "+arg1.getCurrentUrl());
            }
        
            @Override
            public void beforeScript(String arg0, WebDriver arg1) {
                System.out.println("Executing javascript : " +arg0);
            }
        
            @Override
            public void onException(Throwable arg0, WebDriver arg1) {
                System.out.println("Exception throwed : "+arg0.getMessage());
            }
        
        }


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


            System.setProperty("webdriver.gecko.driver", "C:/~/geckodriver.exe");
                    WebDriver driver = new FirefoxDriver();
            


4. Create object for ListenerHelper inside ActualDriver class


            ListenerHelper listen = new ListenerHelper();
            


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


            EventFiringWebDriver browser = new 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:

            import org.openqa.selenium.By;
            import org.openqa.selenium.WebDriver;
            import org.openqa.selenium.firefox.FirefoxDriver;
            import org.openqa.selenium.support.events.EventFiringWebDriver;
            
            public class ActualDriver {
                public static void main(String[] args) {
                    System.setProperty("webdriver.gecko.driver", "C:/~/geckodriver.exe");
                    WebDriver driver = new FirefoxDriver();
                    ListenerHelper listen = new ListenerHelper();
                    EventFiringWebDriver browser = new 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

Single Inheritance in Selenium and java

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
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions

Recent Addition

new tutorial Registrations for Selenium Online Training is Over.

Below are the training details:
Meeting link : https://zoom.us/j/737840591
Starting Time : 9:00PM 18th DEC 2018
 
Join My Facebook Group
Join Group