Search
 
 

Display results as :
 


Rechercher Advanced Search

Latest topics
Navigation
 Portal
 Index
 Memberlist
 Profile
 FAQ
 Search

How to use WebDriverEventListener in Page Object Model design Pattern with Page Factory

View previous topic View next topic Go down

How to use WebDriverEventListener in Page Object Model design Pattern with Page Factory

Post by saradaakurathi on Wed Aug 17, 2016 1:55 pm

Hi All,
I tried to implement WebDriverEventListener in Page Object Model with Page Factory.
But i am getting the error  as below


Code:
java.lang.NullPointerException
at com.tecnotree.CLM.Commons.EventHandler.beforeFindBy(EventHandler.java:68)
at com.tecnotree.CLM.ObjectPage.LoginPage.clickSignInButton(LoginPage.java:28)


Here below are the implementation for EventHandler class which implements WebDriverEventListener

Code:
package com.tecnotree.CLM.Commons;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.events.EventFiringWebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.events.WebDriverEventListener;
 
public class EventHandler implements WebDriverEventListener{

    private Log log = LogFactory.getLog(this.getClass());
 
    public void afterChangeValueOf(WebElement arg0, WebDriver arg1) {
        // TODO Auto-generated method stub
        log.info("inside method afterChangeValueOf on " + arg0.toString());
        log.info("inside method afterChangeValueOf on " + arg0.toString());
    }
 
    public void afterClickOn(WebElement arg0, WebDriver arg1) {
        // TODO Auto-generated method stub
        log.info("inside method afterClickOn on " + arg0.toString());
    }
 
    public void afterFindBy(By arg0, WebElement arg1, WebDriver arg2) {
        // TODO Auto-generated method stub
        log.info("Find happened on " + arg1.toString()
                + " Using method " + arg0.toString());
    }
 
    public void afterNavigateBack(WebDriver arg0) {
        // TODO Auto-generated method stub
 
        log.info("Inside the after navigateback to " + arg0.getCurrentUrl());
    }
 
    public void afterNavigateForward(WebDriver arg0) {
        // TODO Auto-generated method stub
        log.info("Inside the afterNavigateForward to " + arg0.getCurrentUrl());
    }
 
    public void afterNavigateTo(String arg0, WebDriver arg1) {
        // TODO Auto-generated method stub
        log.info("Inside the afterNavigateTo to " + arg0);
    }
 
    public void afterScript(String arg0, WebDriver arg1) {
        // TODO Auto-generated method stub
        log.info("Inside the afterScript to, Script is " + arg0);
    }
 
    public void beforeChangeValueOf(WebElement arg0, WebDriver arg1) {
        // TODO Auto-generated method stub
 
        log.info("Inside the beforeChangeValueOf method");
    }
 
    public void beforeClickOn(WebElement arg0, WebDriver arg1) {
        // TODO Auto-generated method stub
        log.info("About to click on the " + arg0.toString());
 
    }
 
    public void beforeFindBy(By arg0, WebElement arg1, WebDriver arg2) {
        // TODO Auto-generated method stub
        log.info("Just before finding element " + arg1.toString());
 
    }
 
    public void beforeNavigateBack(WebDriver arg0) {
        // TODO Auto-generated method stub
        log.info("Just before beforeNavigateBack " + arg0.getCurrentUrl());
 
    }
 
    public void beforeNavigateForward(WebDriver arg0) {
        // TODO Auto-generated method stub
        log.info("Just before beforeNavigateForward " + arg0.getCurrentUrl());
 
    }
 
    public void beforeNavigateTo(String arg0, WebDriver arg1) {
        // TODO Auto-generated method stub
        log.info("Just before beforeNavigateTo " + arg0);
    }
 
    public void beforeScript(String arg0, WebDriver arg1) {
        // TODO Auto-generated method stub
        log.info("Just before beforeScript " + arg0);
    }
 
    public void onException(Throwable arg0, WebDriver arg1) {
        log.info("Exception occured at " + arg0.getMessage());
 
    }

    public void afterNavigateRefresh(WebDriver arg0) {
        // TODO Auto-generated method stub
       
    }

    public void beforeNavigateRefresh(WebDriver arg0) {
        // TODO Auto-generated method stub
       
    }
 
}


LoginPage Class implementation as:

Code:
package com.tecnotree.CLM.ObjectPage;

import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.events.EventFiringWebDriver;
import org.openqa.selenium.support.events.WebDriverEventListener;

public class LoginPage {
   
    private WebDriverEventListener eventListener;
   
    WebDriver driver;
           
    @FindBy(css = "button.btn.btn-lg.btn-primary.ng-binding.ng-scope")
    private WebElement signIn;

   public LoginPage(WebDriver driver) {
       
        this.driver = driver;
        PageFactory.initElements(driver, this);
    }
  
   // Click on Sign In Button.
   
    public void clickSignInButton() {
        signIn.click();
    }

  
}

Actual code which calling above are as follows:


Code:
package com.tecnotree.CLM.Scripts;

import java.io.File;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.events.EventFiringWebDriver;

import org.testng.annotations.*;


import com.tecnotree.CLM.Commons.*;
import com.tecnotree.CLM.ObjectPage.LoginPage;
import com.tecnotree.CLM.ObjectPage.SignInPage;

public class SuperTest implements CLMConstants {
    WebDriver driver;
    EventFiringWebDriver eventDriver ;
    Logger log;
    static String browserType;
    EventHandler handler ;
   
    @BeforeClass()
    public void signIn() throws Exception
        {
       
        BasicConfigurator.configure();
        PropertyConfigurator.configure(PROPERTIES_FILEPATH);
       
        log = Logger.getLogger("SuperTest");
               
          
        //Get the Browser Type and initiate the Driver
        driver = GenericUtilLibrary.initializeBrowser(driver, BROWSER_TYPE);
        driver.manage().deleteAllCookies();
        driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
        driver.manage().window().maximize();
       
        // Create object for EventFiringWebDriver
        eventDriver = new EventFiringWebDriver(driver);
        
       // Create object for class which implements WebDriverEventListener
        handler = new EventHandler();

        //Register the class object which implements WebDriverEventListener with EventFiringWebDriver ojbject
        eventDriver.register(handler);

        // using eventDriver get the URL
        eventDriver.get(URL);
           
       
        //CLICK ON SIGNIN BUTTON IN LOGIN PAGE by sending eventDriver
        LoginPage lpage = new LoginPage(eventDriver);
        lpage.clickSignInButton();

    }
   
    @AfterClass()
    public void closeDriver()
    {
        http://driver.quit();
       
    }
   

}


Please anyone help me, where wrong in my coding.

in LoginPage() constructor instead of WebDriver object , if i give eventDriver also , it didn't work.

saradaakurathi

Posts : 1
Join date : 2012-06-12

Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum