Vincent
Vincent

Reputation: 480

ui4j NullPointerException when calling javascript window.scrollTo

I'm testing a website using ui4j. When I execute

page.executeScript("window.scrollTo(0,document.body.scrollHeight)");

to scroll to the end of the page I get the following NullPointerException:

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
    at com.sun.javafx.webkit.theme.ScrollBarThemeImpl.getThumb(ScrollBarThemeImpl.java:400)
    at com.sun.javafx.webkit.theme.ScrollBarThemeImpl.thumbPosition(ScrollBarThemeImpl.java:284)
    at com.sun.javafx.webkit.theme.ScrollBarThemeImpl.getThumbPosition(ScrollBarThemeImpl.java:380)
    at com.sun.webkit.WebPage.twkExecuteScript(Native Method)
    at com.sun.webkit.WebPage.executeScript(WebPage.java:1427)
    at javafx.scene.web.WebEngine.executeScript(WebEngine.java:948)
    at com.ui4j.webkit.spi.WebKitJavaScriptEngine.executeScript(WebKitJavaScriptEngine.java:26)
    at com.ui4j.webkit.browser.WebKitPage.executeScript(WebKitPage.java:231)
    at com.ui4j.webkit.browser.WebKitPage$ByteBuddy$sjifLXwz.executeScript$accessor$6vHNXtoG(Unknown Source)
    at com.ui4j.webkit.browser.WebKitPage$ByteBuddy$sjifLXwz$auxiliary$0UdQY8PH.call(Unknown Source)
    at com.ui4j.webkit.proxy.WebKitProxy$CallableExecutor.run(WebKitProxy.java:46)
    at com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295)
    at com.sun.javafx.application.PlatformImpl$$Lambda$51/1744951114.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(PlatformImpl.java:294)
    at com.sun.javafx.application.PlatformImpl$$Lambda$50/929850074.run(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95) 

I tried it with ui4j 2.0 and 1.2.0 on a mac. JDK version 1.8.0_45 .

Is there some configuration on ui4j missing to be able to execute 'window.scrollTo'?

I load the webpage the following way:

BrowserEngine engine = BrowserFactory.getWebKit();
Page page = engine.navigate( url );

Am I missing something or is there a workaround for 'window.scrollTo' under ui4j to avoid the NullPointerException?

Upvotes: 0

Views: 826

Answers (1)

user452425
user452425

Reputation:

Before the page.executeScript() call page.show()

import static com.ui4j.api.browser.BrowserFactory.getWebKit;

import com.ui4j.api.browser.Page;

public class Sample {

    public static void main(String[] args) {
        Page page = getWebKit().navigate("http://stackoverflow.com");
        page.show();
        page.executeScript("window.scrollTo(0,document.body.scrollHeight)");
    }
}

Upvotes: 1

Related Questions