Module org.htmlunit
Package org.htmlunit

Class BrowserVersion

java.lang.Object
org.htmlunit.BrowserVersion
All Implemented Interfaces:
Serializable

public final class BrowserVersion extends Object implements Serializable
Objects of this class represent one specific version of a given browser. Predefined constants are provided for common browser versions.

You can create a different browser setup by using the BrowserVersionFactory.

         final String applicationName = "APPNAME";
         final String applicationVersion = "APPVERSION";
         final String userAgent = "USERAGENT";
 
         final BrowserVersion browser =
                 new BrowserVersion.BrowserVersionBuilder(BrowserVersion.FIREFOX)
                     .setApplicationName(applicationName)
                     .setApplicationVersion(applicationVersion)
                     .setUserAgent(userAgent)
                     .build();
 

But keep in mind this new one still behaves like an FF, only the stuff reported to the outside is changed. This is more or less the same you can do with real browsers installing plugins like UserAgentSwitcher.

Author:
Mike Bowler, Daniel Gredler, Marc Guillemot, Chris Erskine, Ahmed Ashour, Frank Danek, Ronald Brill
See Also:
  • Field Details

    • FIREFOX

      public static final BrowserVersion FIREFOX
      Latest Firefox.
    • FIREFOX_ESR

      public static final BrowserVersion FIREFOX_ESR
      Firefox ESR.
    • CHROME

      public static final BrowserVersion CHROME
      Latest Chrome.
    • EDGE

      public static final BrowserVersion EDGE
      Latest Edge.
    • ALL_SUPPORTED_BROWSERS

      public static final BrowserVersion[] ALL_SUPPORTED_BROWSERS
      Array with all supported browsers.
    • BEST_SUPPORTED

      public static final BrowserVersion BEST_SUPPORTED
      The best supported browser version at the moment.
  • Method Details

    • isSameBrowser

      public boolean isSameBrowser(BrowserVersion other)
      Returns whether the same browser.
      Parameters:
      other - the BrowserVersion to compare with
      Returns:
      true if the nickname and the numeric version are the same
    • getDefault

      public static BrowserVersion getDefault()
      Returns the default browser version that is used whenever a specific version isn't specified. Defaults to BEST_SUPPORTED.
      Returns:
      the default browser version
    • setDefault

      public static void setDefault(BrowserVersion newBrowserVersion)
      Sets the default browser version that is used whenever a specific version isn't specified.
      Parameters:
      newBrowserVersion - the new default browser version
    • isChrome

      public boolean isChrome()
      Returns whether the chrome. Returns true if this BrowserVersion instance represents some version of Google Chrome. Note that Google Chrome does not return 'Chrome' in the application name, we have to look in the nickname.
      Returns:
      whether this version is a version of a Chrome browser
    • isEdge

      public boolean isEdge()
      Returns whether the edge. Returns true if this BrowserVersion instance represents some version of Microsoft Edge.
      Returns:
      whether this version is a version of the Edge browser
    • isFirefox

      public boolean isFirefox()
      Returns whether the firefox. Returns true if this BrowserVersion instance represents some version of Firefox.
      Returns:
      whether this version is a version of a Firefox browser
    • isFirefoxESR

      public boolean isFirefoxESR()
      Returns whether the firefox esr. INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
      Returns:
      whether this represents the Firefox Extended Support Release (ESR) version
    • getNickname

      public String getNickname()
      Returns the short name of the browser like FF, CHROME, etc.
      Returns:
      the short name (if any)
    • getBrowserVersionNumeric

      public int getBrowserVersionNumeric()
      Returns the browser version numeric.
      Returns:
      the browserVersionNumeric
    • getApplicationCodeName

      public String getApplicationCodeName()
      Returns the application code name, for example "Mozilla". Default value is "Mozilla" if not explicitly configured.
      Returns:
      the application code name
      See Also:
    • getApplicationMinorVersion

      public String getApplicationMinorVersion()
      Returns the application minor version, for example "0". Default value is "0" if not explicitly configured. This is a legacy, non-standard property that was only ever implemented by Internet Explorer (as navigator.appMinorVersion).
      Returns:
      the application minor version
    • getApplicationName

      public String getApplicationName()
      Returns the application name, for example "Netscape".
      Returns:
      the application name
      See Also:
    • getApplicationVersion

      public String getApplicationVersion()
      Returns the application version, for example "4.0 (compatible; MSIE 6.0b; Windows 98)".
      Returns:
      the application version
      See Also:
    • getVendor

      public String getVendor()
      Returns the vendor.
      Returns:
      the vendor
    • getBrowserLocale

      public Locale getBrowserLocale()
      Returns the browser locale. Default value is Locale.forLanguageTag("en-US") if not explicitly configured.
      Returns:
      the system locale
    • getBrowserLanguage

      public String getBrowserLanguage()
      Returns the browser application language, for example "en-us". Default value is "en-US" if not explicitly configured.
      Returns:
      the browser application language
      See Also:
    • isOnLine

      public boolean isOnLine()
      Returns true if the browser is currently online. Default value is true if not explicitly configured.
      Returns:
      true if the browser is currently online
      See Also:
    • getPlatform

      public String getPlatform()
      Returns the platform on which the application is running, for example "Win32". Default value is 'Win32' if not explicitly configured.
      Returns:
      the platform on which the application is running
      See Also:
    • getSystemTimezone

      public TimeZone getSystemTimezone()
      Returns the system TimeZone. Default value is America/New_York if not explicitly configured.
      Returns:
      the system TimeZone
    • getUserAgent

      public String getUserAgent()
      Returns the user agent string, for example "Mozilla/4.0 (compatible; MSIE 6.0b; Windows 98)".
      Returns:
      the user agent string
    • getAcceptEncodingHeader

      public String getAcceptEncodingHeader()
      Returns the value used by the browser for the Accept_Encoding header.
      Returns:
      the accept encoding header string
    • getAcceptLanguageHeader

      public String getAcceptLanguageHeader()
      Returns the value used by the browser for the Accept_Language header.
      Returns:
      the accept language header string
    • getHtmlAcceptHeader

      public String getHtmlAcceptHeader()
      Returns the value used by the browser for the Accept header if requesting a page.
      Returns:
      the accept header string
    • getScriptAcceptHeader

      public String getScriptAcceptHeader()
      Returns the script accept header. Returns the value used by the browser for the Accept header if requesting a script.
      Returns:
      the accept header string
    • getXmlHttpRequestAcceptHeader

      public String getXmlHttpRequestAcceptHeader()
      Returns the xml http request accept header. Returns the value used by the browser for the Accept header if performing an XMLHttpRequest.
      Returns:
      the accept header string
    • getImgAcceptHeader

      public String getImgAcceptHeader()
      Returns the img accept header. Returns the value used by the browser for the Accept header if requesting an image.
      Returns:
      the accept header string
    • getCssAcceptHeader

      public String getCssAcceptHeader()
      Returns the css accept header. Returns the value used by the browser for the Accept header if requesting a CSS declaration.
      Returns:
      the accept header string
    • getSecClientHintUserAgentHeader

      public String getSecClientHintUserAgentHeader()
      Returns the value used by the browser for the sec-ch-ua header.
      Returns:
      the sec-ch-ua header string
    • getSecClientHintUserAgentPlatformHeader

      public String getSecClientHintUserAgentPlatformHeader()
      Returns the value used by the browser for the sec-ch-ua-platform header.
      Returns:
      the sec-ch-ua-platform header string
    • hasFeature

      public boolean hasFeature(BrowserVersionFeatures property)
      Indicates if this instance has the given feature. Used for HtmlUnit internal processing.
      Parameters:
      property - the property name
      Returns:
      false if this browser doesn't have this feature
    • getBuildId

      public String getBuildId()
      Returns the buildId.
      Returns:
      the buildId
    • getProductSub

      public String getProductSub()
      Returns the productSub.
      Returns:
      the productSub
    • getHeaderNamesOrdered

      public String[] getHeaderNamesOrdered()
      Gets the header names, so they are sent in the given order (if included in the request).
      Returns:
      the header names, in the order they should be sent
    • registerUploadMimeType

      public void registerUploadMimeType(String fileExtension, String mimeType)
      Registers a new mime type for the provided file extension.
      Parameters:
      fileExtension - the file extension used to determine the mime type
      mimeType - the mime type to be used when uploading files with this extension
    • getUploadMimeType

      public String getUploadMimeType(File file)
      Determines the content type for the given file.
      Parameters:
      file - the file
      Returns:
      a content type or an empty string if unknown
    • getFontHeight

      public int getFontHeight(String fontSize)
      Returns the corresponding height of the specified fontSize.
      Parameters:
      fontSize - the font size
      Returns:
      the corresponding height
    • getPixesPerChar

      @Deprecated(since="5.3.0", forRemoval=true) public int getPixesPerChar()
      Deprecated, for removal: This API element is subject to removal in a future version.
      as of version 5.3.0; use getPixelsPerChar() instead.
      Performs this operation.
      Returns:
      the pixesPerChar
    • getPixelsPerChar

      public int getPixelsPerChar()
      Returns the pixels per char.
      Returns:
      the pixelsPerChar (currently hard coded 10)
    • canPlayType

      public String canPlayType(String type)
      Performs this operation. Determines whether this browser thinks it can play the given media resource type, mirroring the result of the DOM method HTMLMediaElement.canPlayType().

      The type is parsed into a MIME type and an optional codecs parameter (see BrowserVersion.MediaResourceType.parse(String) for the exact parsing rules). The parsed value is then looked up first against this browser's "probably" playable resources (MIME type plus a known-good codec list) and, failing that, against its "maybe" playable resources (MIME type alone, with no codec guarantee).

      Parameters:
      type - the media type string to test, e.g. "video/mp4" or "video/mp4; codecs=\"avc1.42E01E, mp4a.40.2\""
      Returns:
      "probably" if this exact MIME type and codec combination is known to be supported, "maybe" if the MIME type alone (with no codec specified) is known to be supported, or "" if type is blank, or if a codec was specified but that exact MIME/codec pairing isn't a known "probably" match
      See Also:
    • toString

      public String toString()
      Overrides:
      toString in class Object