Class HtmlUnitDriverOptions

  • All Implemented Interfaces:
    Serializable, org.openqa.selenium.Capabilities

    public class HtmlUnitDriverOptions
    extends org.openqa.selenium.remote.AbstractDriverOptions<HtmlUnitDriverOptions>
    Class to manage options specific to HtmlUnitDriver.

    Example usage:

    
     HtmlUnitDriverOptions options = new HtmlUnitDriverOptions()
         .setWebClientVersion(BrowserVersion.FIREFOX_ESR)
         .setJavaScriptEnabled(true);
    
     // For use with HtmlUnitDriver:
     HtmlUnitDriver driver = new HtmlUnitDriver(options);
    
     // For use with RemoteWebDriver:
     RemoteWebDriver driver = new RemoteWebDriver(
         new URL("http://localhost:4444/"),
         new HtmlUnitDriverOptions());
     

    Getting/setting HtmlUnitDriver options:

    In addition to methods for reading and writing specific HtmlUnitDriver options, you can use the standard MutableCapabilities API:

    Example usage:

    
     HtmlUnitDriverOptions options = new HtmlUnitDriverOptions();
     boolean popupBlockerEnabled = options.is(HtmlUnitOption.optPopupBlockerEnabled);
     // NOTE: See "Getting individual browser version traits" below
     String  browserLanguage = (String) options.getCapability(BrowserVersionTrait.optBrowserLanguage);
     options.setCapability(HtmlUnitOption.optGeolocationEnabled, true);
     

    Getting individual browser version traits:

    HtmlUnitDriverOption contains a BrowserVersion which can be read and written directly:

    The individual traits of the BrowserVersion object can be read directly as well via the standard Capabilities API. For example:
    
     HtmlUnitDriverOptions options = new HtmlUnitDriverOptions(BrowserVersion.EDGE);
     // System time zone accessed via BrowserVersion API
     TimeZone viaBrowserVersion = options.getWebClientVersion.getSystemTimezone();
     // System time zone accessed via standard Capabilities API
     TimeZone viaCapabilityName = (TimeZone) options.getCapability(BrowserVersionTrait.optSystemTimezone);
     
    NOTE: Although HtmlUnitDriverOptions objects are mutable (their properties can be altered), the individual traits of the BrowserVersion object within these objects cannot be altered:
    
     HtmlUnitDriverOptions options = new HtmlUnitDriverOptions(BrowserVersion.CHROME);
     options.setCapability(BrowserVersionTrait.optUserAgent, "HtmlUnitDriver emulating Google Chrome");
     // => UnsupporterOperationException: Individual browser version traits are immutable; 'optUserAgent' cannot be set
     
    Since:
    HtmlUnitDriver v4.22.0
    Author:
    Scott Babcock, Ronald Brill
    See Also:
    HtmlUnitOption, BrowserVersionTrait, Serialized Form
    • Field Detail

      • HTMLUNIT_OPTIONS

        public static final String HTMLUNIT_OPTIONS
        Key used to store a set of HtmlUnitDriverOptions in a Capabilities object.
        See Also:
        Constant Field Values
      • BROWSER_VERSION

        public static final String BROWSER_VERSION
        Key used to store the browser version in a Capabilities object.

        This key includes the "garg:" vendor prefix so that the Grid Distributor will ignore it. Using the standard "browserVersion" key instead results in slot match failures, because the distributor requires the version specified in the requested capabilities to match the version specified by the slot stereotype.

        See Also:
        Constant Field Values
    • Constructor Detail

      • HtmlUnitDriverOptions

        public HtmlUnitDriverOptions()
        Default constructor.

        Initializes HtmlUnitDriverOptions with default capabilities:

        • Browser name set to "htmlunit"
        • Home page set to "about:blank"
        • Exceptions on failing status codes disabled
        • Print content on failing status code disabled
        • Use insecure SSL enabled
      • HtmlUnitDriverOptions

        public HtmlUnitDriverOptions​(BrowserVersion version)
        Constructs HtmlUnitDriverOptions with a specific BrowserVersion.
        Parameters:
        version - the browser version to use
      • HtmlUnitDriverOptions

        public HtmlUnitDriverOptions​(BrowserVersion version,
                                     boolean enableJavascript)
        Constructs HtmlUnitDriverOptions with a specific BrowserVersion and JavaScript support enabled/disabled.
        Parameters:
        version - the browser version to use
        enableJavascript - true to enable JavaScript support, false to disable
      • HtmlUnitDriverOptions

        public HtmlUnitDriverOptions​(org.openqa.selenium.Capabilities source)
        Constructs HtmlUnitDriverOptions from an existing Capabilities object.

        Transfers mapped capabilities and legacy HtmlUnit options. If the source is another HtmlUnitDriverOptions instance, copies the WebClientOptions and BrowserVersion from it.

        Parameters:
        source - source capabilities to copy
      • HtmlUnitDriverOptions

        public HtmlUnitDriverOptions​(Map<String,​Object> optionsMap)
        Constructs HtmlUnitDriverOptions from a map of options.
        Parameters:
        optionsMap - map containing option key-value pairs
    • Method Detail

      • getBrowserVersion

        public String getBrowserVersion()
      • getCapability

        public Object getCapability​(String capabilityName)
        Specified by:
        getCapability in interface org.openqa.selenium.Capabilities
        Overrides:
        getCapability in class org.openqa.selenium.remote.AbstractDriverOptions<HtmlUnitDriverOptions>
      • setCapability

        public void setCapability​(String capabilityName,
                                  Object value)
        Overrides:
        setCapability in class org.openqa.selenium.MutableCapabilities
      • getExtraCapabilityNames

        protected Set<String> getExtraCapabilityNames()
        Specified by:
        getExtraCapabilityNames in class org.openqa.selenium.remote.AbstractDriverOptions<HtmlUnitDriverOptions>
      • getExtraCapability

        protected Object getExtraCapability​(String capabilityName)
        Specified by:
        getExtraCapability in class org.openqa.selenium.remote.AbstractDriverOptions<HtmlUnitDriverOptions>
      • applyOptions

        public void applyOptions​(WebClientOptions target)
        Apply values from this HtmlUnitDriver options object to the specifies target.
        Parameters:
        target - target WebClientOptions object
      • isJavaScriptEnabled

        public boolean isJavaScriptEnabled()
        Returns whether JavaScript execution is enabled.
        Returns:
        true if JavaScript is enabled; false otherwise
      • setJavaScriptEnabled

        public HtmlUnitDriverOptions setJavaScriptEnabled​(boolean enableJavascript)
        Enables or disables JavaScript execution.
        Parameters:
        enableJavascript - true to enable JavaScript; false to disable it
        Returns:
        this options instance
      • isDownloadImages

        public boolean isDownloadImages()
        Returns whether images are downloaded automatically.
        Returns:
        true if image downloading is enabled; false otherwise
      • setDownloadImages

        public HtmlUnitDriverOptions setDownloadImages​(boolean downloadImages)
        Enables or disables automatic image downloading.
        Parameters:
        downloadImages - true to download images; false otherwise
        Returns:
        this options instance
      • getWebClientVersion

        public BrowserVersion getWebClientVersion()
        Returns the configured BrowserVersion used by the underlying WebClient.
        Returns:
        the current web client browser version
      • setSSLClientCertificateKeyStore

        public HtmlUnitDriverOptions setSSLClientCertificateKeyStore​(KeyStore keyStore,
                                                                     char[] keyStorePassword)
        Sets the SSL client certificate key store from an existing KeyStore.
        Parameters:
        keyStore - the key store containing the client certificate
        keyStorePassword - the password for the key store
        Returns:
        this options instance
      • setSSLClientCertificateKeyStore

        public HtmlUnitDriverOptions setSSLClientCertificateKeyStore​(URL keyStoreUrl,
                                                                     String keyStorePassword,
                                                                     String keyStoreType)
        Sets the SSL client certificate key store using a URL.
        Parameters:
        keyStoreUrl - the URL to the key store
        keyStorePassword - the key store password
        keyStoreType - the key store type (e.g., "PKCS12")
        Returns:
        this options instance
        Throws:
        NullPointerException - if keyStoreUrl is null
      • setSSLClientCertificateKeyStore

        public HtmlUnitDriverOptions setSSLClientCertificateKeyStore​(InputStream keyStoreInputStream,
                                                                     String keyStorePassword,
                                                                     String keyStoreType)
        Sets the SSL client certificate key store from an InputStream.
        Parameters:
        keyStoreInputStream - the input stream providing the key store
        keyStorePassword - the key store password
        keyStoreType - the key store type (e.g., "PKCS12")
        Returns:
        this options instance
      • setSSLTrustStore

        public HtmlUnitDriverOptions setSSLTrustStore​(URL sslTrustStoreUrl,
                                                      String sslTrustStorePassword,
                                                      String sslTrustStoreType)
        Sets the SSL trust store using a URL.
        Parameters:
        sslTrustStoreUrl - the URL to the trust store
        sslTrustStorePassword - the trust store password
        sslTrustStoreType - the trust store type (e.g., "JKS")
        Returns:
        this options instance
        Throws:
        NullPointerException - if sslTrustStoreUrl is null