简体   繁体   中英

Selenium DoubleClick WebElement causing Error

I am using the code below to stimulate a double mouse click on a button, however, I am getting a compile error which is pointing towards the actions but I don't no how to fix it.

Actions act = new Actions(driver);
    act.doubleClick(driver.findElement(By.id("dijit_form_Button_0_label"))).build().perform();
    logger1.info("Logout Successful");

org.testng.TestNGException: 
Cannot instantiate class testNG.RAD3398LogoutTwiceTest
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:29)
    ... 21 more
Caused by: java.lang.NullPointerException
    at org.openqa.selenium.interactions.Actions.<init>(Actions.java:44)
    at testNG.RAD3398LogoutTwiceTest.<init>(RAD3398LogoutTwiceTest.java:25)
    ... 26 more

Any ideas?

you could try to fire the doubleclick event using javascript. This is how I do for triggering the click event from selenium, I guess that you can adapt it for triggering a doubleclick

    public static void phantomClick (WebDriver driver, WebElement element){
        final String script = "function ghostclick(el){var ev = document.createEvent(\"MouseEvent\");ev.initMouseEvent(\"click\",true ,true,window,null,0,0,0,0,false,false,false,false,0,null);el.dispatchEvent(ev);} return ghostclick(arguments[0])";
        ((JavascriptExecutor) driver).executeScript(script, element);
    }   

A TestNGException isn't a compile error. It is a RuntimeException thrown by TestNG. In this case TestNG threw the exception because it could not "instantiate class testNG.RAD3398LogoutTwiceTest".

Here is the reason why:

Caused by: java.lang.NullPointerException
    at org.openqa.selenium.interactions.Actions.<init>(Actions.java:44)
    at testNG.RAD3398LogoutTwiceTest.<init>(RAD3398LogoutTwiceTest.java:25)

This means that when initializing/constructing an instance of RAD3398LogoutTwiceTest that a constructor for Actions was invoked which then threw a NullPointerException on line 44.

If you take a look at Actions.java:44 you'll see this.mouse = ((HasInputDevices) driver).getMouse(); . ie The initialization failed because driver is null .

Make sure that any statements like new Actions(driver); occur after driver is initialized or your test class will fail to initialize and TestNG will throw an exception.

Once you fix this issue your statement to double-click should execute.

Alternative workaround Source

Simplified to this:

((JavascriptExecutor) driver).executeScript("document.getElementById('map_container').dispatchEvent(new Event('dblclick'));"); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM