Wednesday, 22 June 2016

Listeners in Selenium

When Selenium performs any of the operations like click, navigate etc, events are fired.
We can write custom code when these events are fired.

We will need to create a class that implements the methods to be called after event is fired.

Below class implements the WebDriverEventListener.

package chrometest;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.events.WebDriverEventListener;

/**
 * Created by Sagar on 22-06-2016.
 */
public class ListenerImplementation  implements WebDriverEventListener {
    @Override
    public void beforeNavigateTo(String s, WebDriver webDriver) {
        System.out.println("Before navigating to " + s);
    }

    @Override
    public void afterNavigateTo(String s, WebDriver webDriver) {

    }

    @Override
    public void beforeNavigateBack(WebDriver webDriver) {

    }

    @Override
    public void afterNavigateBack(WebDriver webDriver) {

    }

    @Override
    public void beforeNavigateForward(WebDriver webDriver) {

    }

    @Override
    public void afterNavigateForward(WebDriver webDriver) {

    }

    @Override
    public void beforeFindBy(By by, WebElement webElement, WebDriver webDriver) {
        System.out.println("Before find by");
    }

    @Override
    public void afterFindBy(By by, WebElement webElement, WebDriver webDriver) {

    }

    @Override
    public void beforeClickOn(WebElement webElement, WebDriver webDriver) {

    }

    @Override
    public void afterClickOn(WebElement webElement, WebDriver webDriver) {

    }

    @Override
    public void beforeChangeValueOf(WebElement webElement, WebDriver webDriver) {

    }

    @Override
    public void afterChangeValueOf(WebElement webElement, WebDriver webDriver) {

    }

    @Override
    public void beforeScript(String s, WebDriver webDriver) {

    }

    @Override
    public void afterScript(String s, WebDriver webDriver) {

    }

    @Override
    public void onException(Throwable throwable, WebDriver webDriver) {
        System.out.println("Exception");
    }
}

Next we need to register the instance of this class with driver as shown in highlighted code below. beforeNavigateTo method is called when execute below code. In fact, all the methods for corresponding events are called automatically.

package chrometest;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.events.EventFiringWebDriver;
import java.util.concurrent.TimeUnit;
/** * Created by Sagar on 17-02-2016. */
public class LaunchEventFiringChrome {
    static WebDriver driver = null;

    @Test    public void launchChromeTest() throws Exception{
        System.setProperty("webdriver.chrome.driver",
 "C:\\Users\\Sagar\\Videos\\chromedriver_win32\\chromedriver.exe");
        driver = new ChromeDriver();
        EventFiringWebDriver eventFiringWebDriverDriver  =
 new EventFiringWebDriver(driver);
        ListenerImplementation listenerImplementation = 
new ListenerImplementation();
        eventFiringWebDriverDriver.register(listenerImplementation);

        eventFiringWebDriverDriver.manage().timeouts().implicitlyWait
(10, TimeUnit.SECONDS);
        eventFiringWebDriverDriver.manage().window().maximize();
        String domain = "http://www.softpost.org";
        eventFiringWebDriverDriver.get(domain);
        System.out.println(eventFiringWebDriverDriver.getTitle());
        eventFiringWebDriverDriver.close();
        eventFiringWebDriverDriver.quit();

    }
}

What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com

2 comments:

Buy Best Selenium Books

About Me

My photo

Self-motivated software professional