Sarada Akurathi Sarada Akurathi - 3 months ago 20
Java Question

How to use WebDriverEvent Listener in Page Factory Pattern

I got the code how to use WebDriverEventListener in normal way in selenium.
But don't know how to use the same for PageFactory pattern.

The Code is as follows, first need to implement WebDriverEventListner interface

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

}

}


Next we need to use this in script as below:

@BeforeClass()
public void signIn() throws Exception
{

BasicConfigurator.configure();
PropertyConfigurator.configure(PROPERTIES_FILEPATH);

log = Logger.getLogger("SuperTest");

//Delete the test-output directory
try
{
FileUtils.deleteDirectory(new File("./test-output"));
}
catch(Exception e)
{

}

//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 a object for EventFiringWebDriver Class
eventDriver = new EventFiringWebDriver(driver);

//create object of the class (EventHandler) which implemented WebDriverEventListener Interface.
handler = new EventHandler();

//register handler object with EventFiringWebDriver
eventDriver.register(handler);


eventDriver.get(URL);


//CLICK ON SIGNIN BUTTON IN LOGIN PAGE.
LoginPage lpage = new LoginPage(driver);
lpage.clickSignInButton();


//ENTER USER CREDENTIALS AND CLICK ON SIGNON BUTTON IN SIGNIN PAGE.
SignInPage spage = new SignInPage(driver);
spage.clickSignInButton(USERNAME, PASSWORD);
}


Once registered, eventDriver will handle the logging the actions of WebDriver, when we use eventDriver for all the WebElements.

Till eventDriver.get(URL); i have no problem but when it comes to next step, i am calling Object Page Classes and invoking their methods. To Send eventDriver instead of driver object, i am getting NULLPOINTEREXCEPTION when it called the first method itself.

I have no clue how to use WebDriverEventListener.

Please any one help me on this.

Answer

In your code you have a simple error.

Instead of:

 //CLICK ON SIGNIN BUTTON IN LOGIN PAGE.
        LoginPage lpage = new LoginPage(driver);
        lpage.clickSignInButton();

You should use:

 //CLICK ON SIGNIN BUTTON IN LOGIN PAGE.
        LoginPage lpage = new LoginPage(handler);
        lpage.clickSignInButton();