Class HtmlUnitDriver

  • All Implemented Interfaces:
    org.openqa.selenium.HasCapabilities, org.openqa.selenium.interactions.Interactive, org.openqa.selenium.JavascriptExecutor, org.openqa.selenium.SearchContext, org.openqa.selenium.WebDriver

    public class HtmlUnitDriver
    extends Object
    implements org.openqa.selenium.WebDriver, org.openqa.selenium.JavascriptExecutor, org.openqa.selenium.HasCapabilities, org.openqa.selenium.interactions.Interactive
    An implementation of WebDriver that drives HtmlUnit, which is a headless (GUI-less) browser simulator.

    The main supported browsers are Chrome, Edge, Firefox and Internet Explorer.

    Author:
    Alexei Barantsev, Ahmed Ashour, Rafael Jimenez, Luke Inman-Semerau, Kay McCormick, Simon Stewart, Javier Neira, Ronald Brill, Rob Winch, Andrei Solntsev, Martin Bartoš, Scott Babcock
    • Field Detail

      • BROWSER_LANGUAGE_CAPABILITY

        public static final String BROWSER_LANGUAGE_CAPABILITY
        BROWSER_LANGUAGE_CAPABILITY = "browserLanguage".
        See Also:
        Constant Field Values
      • DOWNLOAD_IMAGES_CAPABILITY

        public static final String DOWNLOAD_IMAGES_CAPABILITY
        DOWNLOAD_IMAGES_CAPABILITY = "downloadImages".
        See Also:
        Constant Field Values
      • JAVASCRIPT_ENABLED

        public static final String JAVASCRIPT_ENABLED
        JAVASCRIPT_ENABLED = "javascriptEnabled".
        See Also:
        Constant Field Values
    • Constructor Detail

      • HtmlUnitDriver

        public HtmlUnitDriver()
        Constructs a new instance with JavaScript disabled, and the default BrowserVersion.
      • HtmlUnitDriver

        public HtmlUnitDriver​(BrowserVersion version)
        Constructs a new instance with the specified BrowserVersion.
        Parameters:
        version - the browser version to use
      • HtmlUnitDriver

        public HtmlUnitDriver​(boolean enableJavascript)
        Constructs a new instance, specify JavaScript support and using the default BrowserVersion.
        Parameters:
        enableJavascript - whether to enable JavaScript support or not
      • HtmlUnitDriver

        public HtmlUnitDriver​(BrowserVersion version,
                              boolean enableJavascript)
        Constructs a new instance with the specified BrowserVersion and the JavaScript support.
        Parameters:
        version - the browser version to use
        enableJavascript - whether to enable JavaScript support or not
      • HtmlUnitDriver

        public HtmlUnitDriver​(org.openqa.selenium.Capabilities desiredCapabilities,
                              org.openqa.selenium.Capabilities requiredCapabilities)
      • HtmlUnitDriver

        public HtmlUnitDriver​(org.openqa.selenium.Capabilities capabilities)
        The browserName is Browser.HTMLUNIT "htmlunit" and the browserVersion denotes the required browser AND its version. For example "chrome" for Chrome, "firefox-100" for Firefox 100.
        Parameters:
        capabilities - desired capabilities requested for the htmlunit driver session
    • Method Detail

      • runAsync

        protected void runAsync​(Runnable r)
      • click

        public void click​(DomElement element,
                          boolean directClick)
      • doubleClick

        public void doubleClick​(DomElement element)
      • mouseUp

        public void mouseUp​(DomElement element)
      • mouseMove

        public void mouseMove​(DomElement element)
      • mouseDown

        public void mouseDown​(DomElement element)
      • getBrowserVersion

        public BrowserVersion getBrowserVersion()
        Get the simulated BrowserVersion.
        Returns:
        the used BrowserVersion
      • newWebClient

        protected WebClient newWebClient​(BrowserVersion version)
        Create the underlying WebClient, but don't set any fields on it.
        Parameters:
        version - Which browser to emulate
        Returns:
        a new instance of WebClient.
      • modifyWebClient

        protected WebClient modifyWebClient​(WebClient client)
        Child classes can override this method to customize the WebClient that the HtmlUnit driver uses.
        Parameters:
        client - The client to modify
        Returns:
        The modified client
      • setCurrentWindow

        public void setCurrentWindow​(WebWindow window)
      • setProxySettings

        public void setProxySettings​(org.openqa.selenium.Proxy proxy)
        Set proxy for WebClient using Proxy.
        Parameters:
        proxy - The proxy preferences.
      • setProxy

        public void setProxy​(String host,
                             int port)
        Sets HTTP proxy for WebClient.
        Parameters:
        host - The hostname of HTTP proxy
        port - The port of HTTP proxy, 0 means HTTP proxy w/o port
      • setHTTPProxy

        public void setHTTPProxy​(String host,
                                 int port,
                                 List<String> noProxyHosts)
        Sets HTTP proxy for WebClient with bypass proxy hosts.
        Parameters:
        host - The hostname of HTTP proxy
        port - The port of HTTP proxy, 0 means HTTP proxy w/o port
        noProxyHosts - The list of hosts which need to bypass HTTP proxy
      • setSocksProxy

        public void setSocksProxy​(String host,
                                  int port)
        Sets SOCKS proxy for WebClient.
        Parameters:
        host - The hostname of SOCKS proxy
        port - The port of SOCKS proxy, 0 means HTTP proxy w/o port
      • setSocksProxy

        public void setSocksProxy​(String host,
                                  int port,
                                  List<String> noProxyHosts)
        Sets SOCKS proxy for WebClient with bypass proxy hosts.
        Parameters:
        host - The hostname of SOCKS proxy
        port - The port of SOCKS proxy, 0 means HTTP proxy w/o port
        noProxyHosts - The list of hosts which need to bypass SOCKS proxy
      • setExecutor

        public void setExecutor​(Executor executor)
        Sets the Executor to be used for submitting async tasks to. You have to close this manually on quit()
        Parameters:
        executor - the Executor to use
      • setAutoProxy

        public void setAutoProxy​(String autoProxyUrl)
        Sets Proxy Autoconfiguration URL for WebClient.
        Parameters:
        autoProxyUrl - The Proxy Autoconfiguration URL
      • getCapabilities

        public org.openqa.selenium.Capabilities getCapabilities()
        Specified by:
        getCapabilities in interface org.openqa.selenium.HasCapabilities
      • get

        public void get​(String url)
        Specified by:
        get in interface org.openqa.selenium.WebDriver
      • get

        protected void get​(URL fullUrl)
        Allows HtmlUnit's about:blank to be loaded in the constructor, and may be useful for other tests?
        Parameters:
        fullUrl - The URL to visit
      • getCurrentUrl

        public String getCurrentUrl()
        Specified by:
        getCurrentUrl in interface org.openqa.selenium.WebDriver
      • getTitle

        public String getTitle()
        Specified by:
        getTitle in interface org.openqa.selenium.WebDriver
      • findElement

        public org.openqa.selenium.WebElement findElement​(org.openqa.selenium.By by)
        Specified by:
        findElement in interface org.openqa.selenium.SearchContext
        Specified by:
        findElement in interface org.openqa.selenium.WebDriver
      • findElements

        public List<org.openqa.selenium.WebElement> findElements​(org.openqa.selenium.By by)
        Specified by:
        findElements in interface org.openqa.selenium.SearchContext
        Specified by:
        findElements in interface org.openqa.selenium.WebDriver
      • findElement

        public org.openqa.selenium.WebElement findElement​(HtmlUnitWebElement element,
                                                          org.openqa.selenium.By by)
      • findElements

        public List<org.openqa.selenium.WebElement> findElements​(HtmlUnitWebElement element,
                                                                 org.openqa.selenium.By by)
      • getPageSource

        public String getPageSource()
        Specified by:
        getPageSource in interface org.openqa.selenium.WebDriver
      • close

        public void close()
        Specified by:
        close in interface org.openqa.selenium.WebDriver
      • quit

        public void quit()
        Specified by:
        quit in interface org.openqa.selenium.WebDriver
      • getWindowHandles

        public Set<String> getWindowHandles()
        Specified by:
        getWindowHandles in interface org.openqa.selenium.WebDriver
      • getWindowHandle

        public String getWindowHandle()
        Specified by:
        getWindowHandle in interface org.openqa.selenium.WebDriver
      • executeScript

        public Object executeScript​(String script,
                                    Object... args)
        Specified by:
        executeScript in interface org.openqa.selenium.JavascriptExecutor
      • executeAsyncScript

        public Object executeAsyncScript​(String script,
                                         Object... args)
        Specified by:
        executeAsyncScript in interface org.openqa.selenium.JavascriptExecutor
      • assertElementNotStale

        protected void assertElementNotStale​(DomElement element)
      • switchTo

        public org.openqa.selenium.WebDriver.TargetLocator switchTo()
        Specified by:
        switchTo in interface org.openqa.selenium.WebDriver
      • navigate

        public org.openqa.selenium.WebDriver.Navigation navigate()
        Specified by:
        navigate in interface org.openqa.selenium.WebDriver
      • isJavascriptEnabled

        public boolean isJavascriptEnabled()
      • setJavascriptEnabled

        public void setJavascriptEnabled​(boolean enableJavascript)
      • isDownloadImages

        public boolean isDownloadImages()
      • setDownloadImages

        public void setDownloadImages​(boolean downloadImages)
      • setAcceptInsecureCerts

        public void setAcceptInsecureCerts​(boolean accept)
      • isAcceptInsecureCerts

        public boolean isAcceptInsecureCerts()
      • implicitlyWaitFor

        protected <X> X implicitlyWaitFor​(Callable<X> condition)
      • getWebClient

        public WebClient getWebClient()
      • manage

        public org.openqa.selenium.WebDriver.Options manage()
        Specified by:
        manage in interface org.openqa.selenium.WebDriver
      • perform

        public void perform​(Collection<org.openqa.selenium.interactions.Sequence> sequences)
        Specified by:
        perform in interface org.openqa.selenium.interactions.Interactive
      • resetInputState

        public void resetInputState()
        Specified by:
        resetInputState in interface org.openqa.selenium.interactions.Interactive
      • switchToDefaultContentOfWindow

        protected void switchToDefaultContentOfWindow​(WebWindow window)
      • openNewWindow

        public void openNewWindow()