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 ofWebDriver
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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static class
HtmlUnitDriver.ElementsMap
protected class
HtmlUnitDriver.HtmlUnitWebDriverOptions
protected static interface
HtmlUnitDriver.JavaScriptResultsCollection
-
Field Summary
Fields Modifier and Type Field Description static String
BROWSER_LANGUAGE_CAPABILITY
BROWSER_LANGUAGE_CAPABILITY = "browserLanguage".static String
DOWNLOAD_IMAGES_CAPABILITY
DOWNLOAD_IMAGES_CAPABILITY = "downloadImages".static String
JAVASCRIPT_ENABLED
JAVASCRIPT_ENABLED = "javascriptEnabled".
-
Constructor Summary
Constructors Constructor Description HtmlUnitDriver()
Constructs a new instance with JavaScript disabled, and thedefault
BrowserVersion.HtmlUnitDriver(boolean enableJavascript)
Constructs a new instance, specify JavaScript support and using thedefault
BrowserVersion.HtmlUnitDriver(BrowserVersion version)
Constructs a new instance with the specifiedBrowserVersion
.HtmlUnitDriver(BrowserVersion version, boolean enableJavascript)
Constructs a new instance with the specifiedBrowserVersion
and 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 void
assertElementNotStale(DomElement element)
void
click(DomElement element, boolean directClick)
void
close()
void
doubleClick(DomElement element)
Object
executeAsyncScript(String script, Object... args)
Object
executeScript(String script, Object... args)
org.openqa.selenium.WebElement
findElement(org.openqa.selenium.By by)
org.openqa.selenium.WebElement
findElement(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)
void
get(String url)
protected void
get(URL fullUrl)
Allows HtmlUnit's about:blank to be loaded in the constructor, and may be useful for other tests?HtmlUnitAlert
getAlert()
BrowserVersion
getBrowserVersion()
Get the simulatedBrowserVersion
.org.openqa.selenium.Capabilities
getCapabilities()
String
getCurrentUrl()
HtmlUnitWindow
getCurrentWindow()
HtmlUnitDriver.ElementsMap
getElementsMap()
HtmlUnitKeyboard
getKeyboard()
HtmlUnitMouse
getMouse()
String
getPageSource()
String
getTitle()
WebClient
getWebClient()
String
getWindowHandle()
Set<String>
getWindowHandles()
protected <X> X
implicitlyWaitFor(Callable<X> condition)
boolean
isAcceptInsecureCerts()
boolean
isDownloadImages()
boolean
isJavascriptEnabled()
org.openqa.selenium.WebDriver.Options
manage()
protected WebClient
modifyWebClient(WebClient client)
Child classes can override this method to customize the WebClient that the HtmlUnit driver uses.void
mouseDown(DomElement element)
void
mouseMove(DomElement element)
void
mouseUp(DomElement element)
org.openqa.selenium.WebDriver.Navigation
navigate()
protected WebClient
newWebClient(BrowserVersion version)
Create the underlying WebClient, but don't set any fields on it.void
openNewWindow()
void
perform(Collection<org.openqa.selenium.interactions.Sequence> sequences)
void
quit()
void
resetInputState()
protected void
runAsync(Runnable r)
void
sendKeys(HtmlUnitWebElement element, CharSequence... value)
void
setAcceptInsecureCerts(boolean accept)
void
setAutoProxy(String autoProxyUrl)
Sets Proxy Autoconfiguration URL for WebClient.void
setCurrentWindow(WebWindow window)
void
setDownloadImages(boolean downloadImages)
void
setExecutor(Executor executor)
Sets theExecutor
to be used for submitting async tasks to.void
setHTTPProxy(String host, int port, List<String> noProxyHosts)
Sets HTTP proxy for WebClient with bypass proxy hosts.void
setJavascriptEnabled(boolean enableJavascript)
void
setProxy(String host, int port)
Sets HTTP proxy for WebClient.void
setProxySettings(org.openqa.selenium.Proxy proxy)
Set proxy for WebClient using Proxy.void
setSocksProxy(String host, int port)
Sets SOCKS proxy for WebClient.void
setSocksProxy(String host, int port, List<String> noProxyHosts)
Sets SOCKS proxy for WebClient with bypass proxy hosts.void
submit(HtmlUnitWebElement element)
org.openqa.selenium.WebDriver.TargetLocator
switchTo()
protected void
switchToDefaultContentOfWindow(WebWindow window)
HtmlUnitWebElement
toWebElement(String elementId)
protected HtmlUnitWebElement
toWebElement(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 thedefault
BrowserVersion.
-
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 thedefault
BrowserVersion.- Parameters:
enableJavascript
- whether to enable JavaScript support or not
-
HtmlUnitDriver
public HtmlUnitDriver(BrowserVersion version, boolean enableJavascript)
Constructs a new instance with the specifiedBrowserVersion
and 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 theExecutor
to be used for submitting async tasks to. You have to close this manually onquit()
- Parameters:
executor
- theExecutor
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 interfaceorg.openqa.selenium.HasCapabilities
-
get
public void get(String url)
- Specified by:
get
in 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:
getCurrentUrl
in interfaceorg.openqa.selenium.WebDriver
-
getTitle
public String getTitle()
- Specified by:
getTitle
in interfaceorg.openqa.selenium.WebDriver
-
findElement
public org.openqa.selenium.WebElement findElement(org.openqa.selenium.By by)
- Specified by:
findElement
in interfaceorg.openqa.selenium.SearchContext
- Specified by:
findElement
in interfaceorg.openqa.selenium.WebDriver
-
findElements
public List<org.openqa.selenium.WebElement> findElements(org.openqa.selenium.By by)
- Specified by:
findElements
in interfaceorg.openqa.selenium.SearchContext
- Specified by:
findElements
in 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:
getPageSource
in interfaceorg.openqa.selenium.WebDriver
-
close
public void close()
- Specified by:
close
in interfaceorg.openqa.selenium.WebDriver
-
quit
public void quit()
- Specified by:
quit
in interfaceorg.openqa.selenium.WebDriver
-
getWindowHandles
public Set<String> getWindowHandles()
- Specified by:
getWindowHandles
in interfaceorg.openqa.selenium.WebDriver
-
getWindowHandle
public String getWindowHandle()
- Specified by:
getWindowHandle
in interfaceorg.openqa.selenium.WebDriver
-
executeScript
public Object executeScript(String script, Object... args)
- Specified by:
executeScript
in interfaceorg.openqa.selenium.JavascriptExecutor
-
executeAsyncScript
public Object executeAsyncScript(String script, Object... args)
- Specified by:
executeAsyncScript
in 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:
switchTo
in interfaceorg.openqa.selenium.WebDriver
-
navigate
public org.openqa.selenium.WebDriver.Navigation navigate()
- Specified by:
navigate
in 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:
manage
in interfaceorg.openqa.selenium.WebDriver
-
perform
public void perform(Collection<org.openqa.selenium.interactions.Sequence> sequences)
- Specified by:
perform
in interfaceorg.openqa.selenium.interactions.Interactive
-
resetInputState
public void resetInputState()
- Specified by:
resetInputState
in interfaceorg.openqa.selenium.interactions.Interactive
-
switchToDefaultContentOfWindow
protected void switchToDefaultContentOfWindow(WebWindow window)
-
openNewWindow
public void openNewWindow()
-
-