Package org.openqa.selenium.htmlunit
Class HtmlUnitDriver
- java.lang.Object
-
- org.openqa.selenium.htmlunit.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 ofWebDriverthat 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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classHtmlUnitDriver.ElementsMapprotected classHtmlUnitDriver.HtmlUnitWebDriverOptionsprotected static interfaceHtmlUnitDriver.JavaScriptResultsCollection
-
Field Summary
Fields Modifier and Type Field Description static StringBROWSER_LANGUAGE_CAPABILITYBROWSER_LANGUAGE_CAPABILITY = "browserLanguage".static StringDOWNLOAD_IMAGES_CAPABILITYDOWNLOAD_IMAGES_CAPABILITY = "downloadImages".static StringJAVASCRIPT_ENABLEDJAVASCRIPT_ENABLED = "javascriptEnabled".
-
Constructor Summary
Constructors Constructor Description HtmlUnitDriver()Constructs a new instance with JavaScript disabled, and thedefaultBrowserVersion.HtmlUnitDriver(boolean enableJavascript)Constructs a new instance, specify JavaScript support and using thedefaultBrowserVersion.HtmlUnitDriver(BrowserVersion version)Constructs a new instance with the specifiedBrowserVersion.HtmlUnitDriver(BrowserVersion version, boolean enableJavascript)Constructs a new instance with the specifiedBrowserVersionand the JavaScript support.HtmlUnitDriver(org.openqa.selenium.Capabilities capabilities)The browserName isBrowser.HTMLUNIT"htmlunit" and the browserVersion denotes the required browser AND its version.HtmlUnitDriver(org.openqa.selenium.Capabilities desiredCapabilities, org.openqa.selenium.Capabilities requiredCapabilities)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidassertElementNotStale(DomElement element)voidclick(DomElement element, boolean directClick)voidclose()voiddoubleClick(DomElement element)ObjectexecuteAsyncScript(String script, Object... args)ObjectexecuteScript(String script, Object... args)org.openqa.selenium.WebElementfindElement(org.openqa.selenium.By by)org.openqa.selenium.WebElementfindElement(HtmlUnitWebElement element, org.openqa.selenium.By by)List<org.openqa.selenium.WebElement>findElements(org.openqa.selenium.By by)List<org.openqa.selenium.WebElement>findElements(HtmlUnitWebElement element, org.openqa.selenium.By by)voidget(String url)protected voidget(URL fullUrl)Allows HtmlUnit's about:blank to be loaded in the constructor, and may be useful for other tests?HtmlUnitAlertgetAlert()BrowserVersiongetBrowserVersion()Get the simulatedBrowserVersion.org.openqa.selenium.CapabilitiesgetCapabilities()StringgetCurrentUrl()HtmlUnitWindowgetCurrentWindow()HtmlUnitDriver.ElementsMapgetElementsMap()HtmlUnitKeyboardgetKeyboard()HtmlUnitMousegetMouse()StringgetPageSource()StringgetTitle()WebClientgetWebClient()StringgetWindowHandle()Set<String>getWindowHandles()protected <X> XimplicitlyWaitFor(Callable<X> condition)booleanisAcceptInsecureCerts()booleanisDownloadImages()booleanisJavascriptEnabled()org.openqa.selenium.WebDriver.Optionsmanage()protected WebClientmodifyWebClient(WebClient client)Child classes can override this method to customize the WebClient that the HtmlUnit driver uses.voidmouseDown(DomElement element)voidmouseMove(DomElement element)voidmouseUp(DomElement element)org.openqa.selenium.WebDriver.Navigationnavigate()protected WebClientnewWebClient(BrowserVersion version)Create the underlying WebClient, but don't set any fields on it.voidopenNewWindow()voidperform(Collection<org.openqa.selenium.interactions.Sequence> sequences)voidquit()voidresetInputState()protected voidrunAsync(Runnable r)voidsendKeys(HtmlUnitWebElement element, CharSequence... value)voidsetAcceptInsecureCerts(boolean accept)voidsetAutoProxy(String autoProxyUrl)Sets Proxy Autoconfiguration URL for WebClient.voidsetCurrentWindow(WebWindow window)voidsetDownloadImages(boolean downloadImages)voidsetExecutor(Executor executor)Sets theExecutorto be used for submitting async tasks to.voidsetHTTPProxy(String host, int port, List<String> noProxyHosts)Sets HTTP proxy for WebClient with bypass proxy hosts.voidsetJavascriptEnabled(boolean enableJavascript)voidsetProxy(String host, int port)Sets HTTP proxy for WebClient.voidsetProxySettings(org.openqa.selenium.Proxy proxy)Set proxy for WebClient using Proxy.voidsetSocksProxy(String host, int port)Sets SOCKS proxy for WebClient.voidsetSocksProxy(String host, int port, List<String> noProxyHosts)Sets SOCKS proxy for WebClient with bypass proxy hosts.voidsubmit(HtmlUnitWebElement element)org.openqa.selenium.WebDriver.TargetLocatorswitchTo()protected voidswitchToDefaultContentOfWindow(WebWindow window)HtmlUnitWebElementtoWebElement(String elementId)protected HtmlUnitWebElementtoWebElement(DomElement element)
-
-
-
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 thedefaultBrowserVersion.
-
HtmlUnitDriver
public HtmlUnitDriver(BrowserVersion version)
Constructs a new instance with the specifiedBrowserVersion.- Parameters:
version- the browser version to use
-
HtmlUnitDriver
public HtmlUnitDriver(boolean enableJavascript)
Constructs a new instance, specify JavaScript support and using thedefaultBrowserVersion.- Parameters:
enableJavascript- whether to enable JavaScript support or not
-
HtmlUnitDriver
public HtmlUnitDriver(BrowserVersion version, boolean enableJavascript)
Constructs a new instance with the specifiedBrowserVersionand the JavaScript support.- Parameters:
version- the browser version to useenableJavascript- 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 isBrowser.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)
-
submit
public void submit(HtmlUnitWebElement element)
-
sendKeys
public void sendKeys(HtmlUnitWebElement element, CharSequence... value)
-
getBrowserVersion
public BrowserVersion getBrowserVersion()
Get the simulatedBrowserVersion.- 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
-
getAlert
public HtmlUnitAlert getAlert()
-
getElementsMap
public HtmlUnitDriver.ElementsMap getElementsMap()
-
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 proxyport- 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 proxyport- The port of HTTP proxy, 0 means HTTP proxy w/o portnoProxyHosts- 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 proxyport- 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 proxyport- The port of SOCKS proxy, 0 means HTTP proxy w/o portnoProxyHosts- The list of hosts which need to bypass SOCKS proxy
-
setExecutor
public void setExecutor(Executor executor)
Sets theExecutorto be used for submitting async tasks to. You have to close this manually onquit()- Parameters:
executor- theExecutorto 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:
getCapabilitiesin interfaceorg.openqa.selenium.HasCapabilities
-
get
public void get(String url)
- Specified by:
getin interfaceorg.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:
getCurrentUrlin interfaceorg.openqa.selenium.WebDriver
-
getTitle
public String getTitle()
- Specified by:
getTitlein interfaceorg.openqa.selenium.WebDriver
-
findElement
public org.openqa.selenium.WebElement findElement(org.openqa.selenium.By by)
- Specified by:
findElementin interfaceorg.openqa.selenium.SearchContext- Specified by:
findElementin interfaceorg.openqa.selenium.WebDriver
-
findElements
public List<org.openqa.selenium.WebElement> findElements(org.openqa.selenium.By by)
- Specified by:
findElementsin interfaceorg.openqa.selenium.SearchContext- Specified by:
findElementsin interfaceorg.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:
getPageSourcein interfaceorg.openqa.selenium.WebDriver
-
close
public void close()
- Specified by:
closein interfaceorg.openqa.selenium.WebDriver
-
quit
public void quit()
- Specified by:
quitin interfaceorg.openqa.selenium.WebDriver
-
getWindowHandles
public Set<String> getWindowHandles()
- Specified by:
getWindowHandlesin interfaceorg.openqa.selenium.WebDriver
-
getWindowHandle
public String getWindowHandle()
- Specified by:
getWindowHandlein interfaceorg.openqa.selenium.WebDriver
-
executeScript
public Object executeScript(String script, Object... args)
- Specified by:
executeScriptin interfaceorg.openqa.selenium.JavascriptExecutor
-
executeAsyncScript
public Object executeAsyncScript(String script, Object... args)
- Specified by:
executeAsyncScriptin interfaceorg.openqa.selenium.JavascriptExecutor
-
assertElementNotStale
protected void assertElementNotStale(DomElement element)
-
getKeyboard
public HtmlUnitKeyboard getKeyboard()
-
getMouse
public HtmlUnitMouse getMouse()
-
switchTo
public org.openqa.selenium.WebDriver.TargetLocator switchTo()
- Specified by:
switchToin interfaceorg.openqa.selenium.WebDriver
-
navigate
public org.openqa.selenium.WebDriver.Navigation navigate()
- Specified by:
navigatein interfaceorg.openqa.selenium.WebDriver
-
toWebElement
protected HtmlUnitWebElement toWebElement(DomElement element)
-
toWebElement
public HtmlUnitWebElement toWebElement(String elementId)
-
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()
-
getCurrentWindow
public HtmlUnitWindow getCurrentWindow()
-
manage
public org.openqa.selenium.WebDriver.Options manage()
- Specified by:
managein interfaceorg.openqa.selenium.WebDriver
-
perform
public void perform(Collection<org.openqa.selenium.interactions.Sequence> sequences)
- Specified by:
performin interfaceorg.openqa.selenium.interactions.Interactive
-
resetInputState
public void resetInputState()
- Specified by:
resetInputStatein interfaceorg.openqa.selenium.interactions.Interactive
-
switchToDefaultContentOfWindow
protected void switchToDefaultContentOfWindow(WebWindow window)
-
openNewWindow
public void openNewWindow()
-
-