Экземпляр драйвера не сохраняется при использовании с WebDriverEventListener

У меня есть мой класс EventListner, как показано ниже:

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

public class EventHandlerEx implements WebDriverEventListener 
{
    public void afterChangeValueOf(WebElement arg0, WebDriver arg1) 
    {
        System.out.println("After change value of "+arg0.toString());
    }

    public void beforeChangeValueOf(WebElement arg0, WebDriver arg1) 
    {
        System.out.println("Before change value of "+arg0.toString());
    }

    public void afterClickOn(WebElement arg0, WebDriver arg1) 
    {
        System.out.println("After Click on "+arg0.toString());
    }


    public void beforeClickOn(WebElement arg0, WebDriver arg1) 
    {
        System.out.println("Before Click on "+arg0.toString());
    }

    public void afterFindBy(By arg0, WebElement arg1, WebDriver arg2) 
    {
        System.out.println("After find by  on "+arg1.toString()+" using "+arg0.toString());
    }

    public void beforeFindBy(By arg0, WebElement arg1, WebDriver arg2) 
    {
        System.out.println("Before find by  on "+arg1.toString());
    }

    public void afterNavigateBack(WebDriver arg0) 
    {

    }

    public void afterNavigateForward(WebDriver arg0) 
    {

    }

    public void afterNavigateTo(String arg0, WebDriver arg1) 
    {
      System.out.println("After navigating to "+arg0.toString());
    }

    public void afterScript(String arg0, WebDriver arg1) 
    {

    }

    public void beforeNavigateBack(WebDriver arg0) 
    {

    }

    public void beforeNavigateForward(WebDriver arg0) 
    {

    }

    public void beforeNavigateTo(String arg0, WebDriver arg1) 
    {
         System.out.println("Before navigating to "+arg0.toString()+" with driver "+arg1.toString());
    }

    public void beforeScript(String arg0, WebDriver arg1) 
    {

    }

    public void onException(Throwable arg0, WebDriver arg1) 
    {

    }

    public void afterNavigateRefresh(WebDriver driver)
    {

    }

    public void beforeNavigateRefresh(WebDriver driver)
    {

    }
}

И мой почтовый класс, как ниже:

public class testEventHandler
{
    public static void main(String[] args) 
    {
        System.setProperty("webdriver.chrome.driver", "D:Java Jarschromedriver.exe");
        WebDriver driver=new ChromeDriver();
        EventFiringWebDriver eveDriver=new EventFiringWebDriver(driver);
        EventHandlerEx objHandle=new EventHandlerEx();

        eveDriver.register(objHandle);

        eveDriver.get("http://www.google.com");
        eveDriver.findElement(By.name("q")).sendKeys("Cheese");
        eveDriver.findElement(By.className("sbico")).click();
        System.out.println(eveDriver.getTitle());
        eveDriver.quit();
        eveDriver.unregister(objHandle);
    }
}

Здесь браузер открывает и выполняет первый оператор, но драйвер становится null, когда он пытается выполнить 2-й оператор.

Может ли кто-нибудь сказать мне, чего мне здесь не хватает?

Выход:

Starting ChromeDriver 2.16.333243 (0bfa1d3575fc1044244f21ddb82bf870944ef961) on port 42808
Only local connections are allowed.
Before navigating to http://www.google.com with driver ChromeDriver: chrome on XP (054108d6683078d5d8d3506574935e7e)
After navigating to http://www.google.com
Exception in thread "main" java.lang.NullPointerException
    at EventHandlerEx.beforeFindBy(EventHandlerEx.java:37)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.openqa.selenium.support.events.EventFiringWebDriver

I have my EventListner class like below:

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

public class EventHandlerEx implements WebDriverEventListener 
{
    public void afterChangeValueOf(WebElement arg0, WebDriver arg1) 
    {
        System.out.println("After change value of "+arg0.toString());
    }

    public void beforeChangeValueOf(WebElement arg0, WebDriver arg1) 
    {
        System.out.println("Before change value of "+arg0.toString());
    }

    public void afterClickOn(WebElement arg0, WebDriver arg1) 
    {
        System.out.println("After Click on "+arg0.toString());
    }


    public void beforeClickOn(WebElement arg0, WebDriver arg1) 
    {
        System.out.println("Before Click on "+arg0.toString());
    }

    public void afterFindBy(By arg0, WebElement arg1, WebDriver arg2) 
    {
        System.out.println("After find by  on "+arg1.toString()+" using "+arg0.toString());
    }

    public void beforeFindBy(By arg0, WebElement arg1, WebDriver arg2) 
    {
        System.out.println("Before find by  on "+arg1.toString());
    }

    public void afterNavigateBack(WebDriver arg0) 
    {

    }

    public void afterNavigateForward(WebDriver arg0) 
    {

    }

    public void afterNavigateTo(String arg0, WebDriver arg1) 
    {
      System.out.println("After navigating to "+arg0.toString());
    }

    public void afterScript(String arg0, WebDriver arg1) 
    {

    }

    public void beforeNavigateBack(WebDriver arg0) 
    {

    }

    public void beforeNavigateForward(WebDriver arg0) 
    {

    }

    public void beforeNavigateTo(String arg0, WebDriver arg1) 
    {
         System.out.println("Before navigating to "+arg0.toString()+" with driver "+arg1.toString());
    }

    public void beforeScript(String arg0, WebDriver arg1) 
    {

    }

    public void onException(Throwable arg0, WebDriver arg1) 
    {

    }

    public void afterNavigateRefresh(WebDriver driver)
    {

    }

    public void beforeNavigateRefresh(WebDriver driver)
    {

    }
}

And my mail class is like below:

public class testEventHandler
{
    public static void main(String[] args) 
    {
        System.setProperty("webdriver.chrome.driver", "D:\Java Jars\chromedriver.exe");
        WebDriver driver=new ChromeDriver();
        EventFiringWebDriver eveDriver=new EventFiringWebDriver(driver);
        EventHandlerEx objHandle=new EventHandlerEx();

        eveDriver.register(objHandle);

        eveDriver.get("http://www.google.com");
        eveDriver.findElement(By.name("q")).sendKeys("Cheese");
        eveDriver.findElement(By.className("sbico")).click();
        System.out.println(eveDriver.getTitle());
        eveDriver.quit();
        eveDriver.unregister(objHandle);
    }
}

Here the browser opens and executes the first statement, but the driver is becoming null when it tries to execute 2nd statement.

Can someone tell me what i am missing here?

Output:

Starting ChromeDriver 2.16.333243 (0bfa1d3575fc1044244f21ddb82bf870944ef961) on port 42808
Only local connections are allowed.
Before navigating to http://www.google.com with driver ChromeDriver: chrome on XP (054108d6683078d5d8d3506574935e7e)
After navigating to http://www.google.com
Exception in thread "main" java.lang.NullPointerException
    at EventHandlerEx.beforeFindBy(EventHandlerEx.java:37)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.openqa.selenium.support.events.EventFiringWebDriver$1.invoke(EventFiringWebDriver.java:80)
    at com.sun.proxy.$Proxy0.beforeFindBy(Unknown Source)
    at org.openqa.selenium.support.events.EventFiringWebDriver.findElement(EventFiringWebDriver.java:187)
    at testEventHandler.main(testEventHandler.java:20)

.invoke(EventFiringWebDriver.java:80)
at com.sun.proxy.$Proxy0.beforeFindBy(Unknown Source)
at org.openqa.selenium.support.events.EventFiringWebDriver.findElement(EventFiringWebDriver.java:187)
at testEventHandler.main(testEventHandler.java:20)

1 ответ

  1. I too got the same issue, in EventHandlerEx class , for any method, in which you are using WebElement variable, WebElement became null and that’s why we are getting NULLPOINTEREXCEPTION.

     public void beforeFindBy(By arg0, WebElement arg1, WebDriver arg2) 
        {
            System.out.println("Before find by  on "+arg1.toString());
        }
    

    для решения проблемы я использовал переменную вместо WebElement.