public class HtmlUnitDriver extends java.lang.Object implements WebDriver, JavascriptExecutor, FindsById, FindsByLinkText, FindsByXPath, FindsByName, FindsByCssSelector, FindsByTagName, FindsByClassName, HasCapabilities, HasInputDevices
Modifier and Type | Class and Description |
---|---|
class |
HtmlUnitDriver.HtmlUnitWindow |
protected static interface |
HtmlUnitDriver.JavaScriptResultsCollection |
WebDriver.ImeHandler, WebDriver.Navigation, WebDriver.Options, WebDriver.TargetLocator, WebDriver.Timeouts, WebDriver.Window
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
INVALIDSELECTIONERROR |
static java.lang.String |
INVALIDXPATHERROR |
Constructor and Description |
---|
HtmlUnitDriver()
Constructs a new instance with JavaScript disabled,
and the
default BrowserVersion. |
HtmlUnitDriver(boolean enableJavascript)
Constructs a new instance, specify JavaScript support
and using the
default BrowserVersion. |
HtmlUnitDriver(com.gargoylesoftware.htmlunit.BrowserVersion version)
Constructs a new instance with the specified
BrowserVersion . |
HtmlUnitDriver(com.gargoylesoftware.htmlunit.BrowserVersion version,
boolean enableJavascript)
Constructs a new instance with the specified
BrowserVersion and the JavaScript support. |
HtmlUnitDriver(Capabilities capabilities)
Note: There are two configuration modes for the HtmlUnitDriver using this constructor.
|
HtmlUnitDriver(Capabilities desiredCapabilities,
Capabilities requiredCapabilities) |
Modifier and Type | Method and Description |
---|---|
protected void |
assertElementNotStale(com.gargoylesoftware.htmlunit.html.DomElement element) |
void |
close()
Close the current window, quitting the browser if it's the last window currently open.
|
java.lang.Object |
executeAsyncScript(java.lang.String script,
java.lang.Object... args)
Execute an asynchronous piece of JavaScript in the context of the currently selected frame or
window.
|
java.lang.Object |
executeScript(java.lang.String script,
java.lang.Object... args)
Executes JavaScript in the context of the currently selected frame or window.
|
WebElement |
findElement(By by)
Find the first
WebElement using the given method. |
WebElement |
findElementByClassName(java.lang.String className) |
WebElement |
findElementByCssSelector(java.lang.String using) |
WebElement |
findElementById(java.lang.String id) |
WebElement |
findElementByLinkText(java.lang.String selector) |
WebElement |
findElementByName(java.lang.String name) |
WebElement |
findElementByPartialLinkText(java.lang.String using) |
WebElement |
findElementByTagName(java.lang.String name) |
WebElement |
findElementByXPath(java.lang.String selector) |
java.util.List<WebElement> |
findElements(By by)
Find all elements within the current page using the given mechanism.
|
java.util.List<WebElement> |
findElementsByClassName(java.lang.String className) |
java.util.List<WebElement> |
findElementsByCssSelector(java.lang.String using) |
java.util.List<WebElement> |
findElementsById(java.lang.String id) |
java.util.List<WebElement> |
findElementsByLinkText(java.lang.String selector) |
java.util.List<WebElement> |
findElementsByName(java.lang.String using) |
java.util.List<WebElement> |
findElementsByPartialLinkText(java.lang.String using) |
java.util.List<WebElement> |
findElementsByTagName(java.lang.String using) |
java.util.List<WebElement> |
findElementsByXPath(java.lang.String selector) |
void |
get(java.lang.String url)
Load a new web page in the current browser window.
|
protected void |
get(java.net.URL fullUrl)
Allows HtmlUnit's about:blank to be loaded in the constructor, and may be useful for other
tests?
|
com.gargoylesoftware.htmlunit.BrowserVersion |
getBrowserVersion()
Get the simulated
BrowserVersion . |
Capabilities |
getCapabilities() |
java.lang.String |
getCurrentUrl()
Get a string representing the current URL that the browser is looking at.
|
protected com.gargoylesoftware.htmlunit.WebWindow |
getCurrentWindow() |
Keyboard |
getKeyboard() |
Mouse |
getMouse() |
java.lang.String |
getPageSource()
Get the source of the last loaded page.
|
java.lang.String |
getTitle()
The title of the current page.
|
protected com.gargoylesoftware.htmlunit.WebClient |
getWebClient() |
java.lang.String |
getWindowHandle()
Return an opaque handle to this window that uniquely identifies it within this driver instance.
|
java.util.Set<java.lang.String> |
getWindowHandles()
Return a set of window handles which can be used to iterate over all open windows of this
WebDriver instance by passing them to
WebDriver.switchTo() .WebDriver.Options.window() |
protected <X> X |
implicitlyWaitFor(java.util.concurrent.Callable<X> condition) |
boolean |
isJavascriptEnabled() |
protected com.gargoylesoftware.htmlunit.Page |
lastPage() |
WebDriver.Options |
manage()
Gets the Option interface
|
protected com.gargoylesoftware.htmlunit.WebClient |
modifyWebClient(com.gargoylesoftware.htmlunit.WebClient client)
Child classes can override this method to customize the WebClient that the HtmlUnit driver
uses.
|
WebDriver.Navigation |
navigate()
An abstraction allowing the driver to access the browser's history and to navigate to a given
URL.
|
protected WebElement |
newHtmlUnitWebElement(com.gargoylesoftware.htmlunit.html.DomElement element) |
protected com.gargoylesoftware.htmlunit.WebClient |
newWebClient(com.gargoylesoftware.htmlunit.BrowserVersion version)
Create the underlying WebClient, but don't set any fields on it.
|
protected void |
pickWindow() |
void |
quit()
Quits this driver, closing every associated window.
|
void |
setAutoProxy(java.lang.String autoProxyUrl)
Sets Proxy Autoconfiguration URL for WebClient
|
void |
setHTTPProxy(java.lang.String host,
int port,
java.util.ArrayList<java.lang.String> noProxyHosts)
Sets HTTP proxy for WebClient with bypass proxy hosts
|
void |
setJavascriptEnabled(boolean enableJavascript) |
void |
setProxy(java.lang.String host,
int port)
Sets HTTP proxy for WebClient
|
void |
setProxySettings(Proxy proxy)
Set proxy for WebClient using Proxy.
|
void |
setSocksProxy(java.lang.String host,
int port)
Sets SOCKS proxy for WebClient
|
void |
setSocksProxy(java.lang.String host,
int port,
java.util.ArrayList<java.lang.String> noProxyHosts)
Sets SOCKS proxy for WebClient with bypass proxy hosts
|
WebDriver.TargetLocator |
switchTo()
Send future commands to a different frame or window.
|
public static final java.lang.String INVALIDXPATHERROR
public static final java.lang.String INVALIDSELECTIONERROR
public HtmlUnitDriver()
default
BrowserVersion.public HtmlUnitDriver(boolean enableJavascript)
default
BrowserVersion.enableJavascript
- whether to enable JavaScript support or notpublic HtmlUnitDriver(com.gargoylesoftware.htmlunit.BrowserVersion version, boolean enableJavascript)
BrowserVersion
and the JavaScript support.version
- the browser version to useenableJavascript
- whether to enable JavaScript support or notpublic HtmlUnitDriver(com.gargoylesoftware.htmlunit.BrowserVersion version)
BrowserVersion
.version
- the browser version to usepublic HtmlUnitDriver(Capabilities capabilities)
The Remote WebDriver uses the second mode - the first mode is deprecated and should not be used.
capabilities
- desired capabilities requested for the htmlunit driver sessionpublic HtmlUnitDriver(Capabilities desiredCapabilities, Capabilities requiredCapabilities)
public com.gargoylesoftware.htmlunit.BrowserVersion getBrowserVersion()
BrowserVersion
.BrowserVersion
protected com.gargoylesoftware.htmlunit.WebClient newWebClient(com.gargoylesoftware.htmlunit.BrowserVersion version)
version
- Which browser to emulateprotected com.gargoylesoftware.htmlunit.WebClient modifyWebClient(com.gargoylesoftware.htmlunit.WebClient client)
client
- The client to modifypublic void setProxySettings(Proxy proxy)
proxy
- The proxy preferences.public void setProxy(java.lang.String host, int port)
host
- The hostname of HTTP proxyport
- The port of HTTP proxy, 0 means HTTP proxy w/o portpublic void setHTTPProxy(java.lang.String host, int port, java.util.ArrayList<java.lang.String> noProxyHosts)
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 proxypublic void setSocksProxy(java.lang.String host, int port)
host
- The hostname of SOCKS proxyport
- The port of SOCKS proxy, 0 means HTTP proxy w/o portpublic void setSocksProxy(java.lang.String host, int port, java.util.ArrayList<java.lang.String> noProxyHosts)
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 proxypublic void setAutoProxy(java.lang.String autoProxyUrl)
autoProxyUrl
- The Proxy Autoconfiguration URLpublic Capabilities getCapabilities()
getCapabilities
in interface HasCapabilities
public void get(java.lang.String url)
WebDriver
WebDriver.Navigation.to(String)
.protected void get(java.net.URL fullUrl)
fullUrl
- The URL to visitprotected void pickWindow()
public java.lang.String getCurrentUrl()
WebDriver
getCurrentUrl
in interface WebDriver
public java.lang.String getTitle()
WebDriver
public WebElement findElement(By by)
WebDriver
WebElement
using the given method.
This method is affected by the 'implicit wait' times in force at the time of execution.
The findElement(..) invocation will return a matching row, or try again repeatedly until
the configured timeout is reached.
findElement should not be used to look for non-present elements, use WebDriver.findElements(By)
and assert zero length response instead.findElement
in interface SearchContext
findElement
in interface WebDriver
by
- The locating mechanismBy
,
WebDriver.Timeouts
public java.util.List<WebElement> findElements(By by)
WebDriver
findElements
in interface SearchContext
findElements
in interface WebDriver
by
- The locating mechanism to useWebElement
s, or an empty list if nothing matchesBy
,
WebDriver.Timeouts
public java.lang.String getPageSource()
WebDriver
getPageSource
in interface WebDriver
public void close()
WebDriver
public void quit()
WebDriver
public java.util.Set<java.lang.String> getWindowHandles()
WebDriver
WebDriver.switchTo()
.WebDriver.Options.window()
getWindowHandles
in interface WebDriver
public java.lang.String getWindowHandle()
WebDriver
getWindowHandle
in interface WebDriver
public java.lang.Object executeScript(java.lang.String script, java.lang.Object... args)
JavascriptExecutor
Within the script, use document
to refer to the current document. Note that local
variables will not be available once the script has finished executing, though global variables
will persist.
If the script has a return value (i.e. if the script contains a return
statement),
then the following steps will be taken:
Arguments must be a number, a boolean, a String, WebElement, or a List of any combination of the above. An exception will be thrown if the arguments do not meet these criteria. The arguments will be made available to the JavaScript via the "arguments" magic variable, as if the function were called via "Function.apply"
executeScript
in interface JavascriptExecutor
script
- The JavaScript to executeargs
- The arguments to the script. May be emptypublic java.lang.Object executeAsyncScript(java.lang.String script, java.lang.Object... args)
JavascriptExecutor
synchronous JavaScript
,
scripts executed with this method must explicitly signal they are finished by invoking the
provided callback. This callback is always injected into the executed function as the last
argument.
The first argument passed to the callback function will be used as the script's result. This value will be handled as follows:
The default timeout for a script to be executed is 0ms. In most cases, including the examples
below, one must set the script timeout
WebDriver.Timeouts.setScriptTimeout(long, java.util.concurrent.TimeUnit)
beforehand
to a value sufficiently large enough.
Example #1: Performing a sleep in the browser under test.
long start = System.currentTimeMillis();
((JavascriptExecutor) driver).executeAsyncScript(
"window.setTimeout(arguments[arguments.length - 1], 500);");
System.out.println(
"Elapsed time: " + System.currentTimeMillis() - start);
Example #2: Synchronizing a test with an AJAX application:
WebElement composeButton = driver.findElement(By.id("compose-button"));
composeButton.click();
((JavascriptExecutor) driver).executeAsyncScript(
"var callback = arguments[arguments.length - 1];" +
"mailClient.getComposeWindowWidget().onload(callback);");
driver.switchTo().frame("composeWidget");
driver.findElement(By.id("to")).sendKeys("[email protected]");
Example #3: Injecting a XMLHttpRequest and waiting for the result:
Object response = ((JavascriptExecutor) driver).executeAsyncScript(
"var callback = arguments[arguments.length - 1];" +
"var xhr = new XMLHttpRequest();" +
"xhr.open('GET', '/resource/data.json', true);" +
"xhr.onreadystatechange = function() {" +
" if (xhr.readyState == 4) {" +
" callback(xhr.responseText);" +
" }" +
"};" +
"xhr.send();");
JsonObject json = new JsonParser().parse((String) response);
assertEquals("cheese", json.get("food").getAsString());
Script arguments must be a number, a boolean, a String, WebElement, or a List of any combination of the above. An exception will be thrown if the arguments do not meet these criteria. The arguments will be made available to the JavaScript via the "arguments" variable.
executeAsyncScript
in interface JavascriptExecutor
script
- The JavaScript to execute.args
- The arguments to the script. May be empty.WebDriver.Timeouts.setScriptTimeout(long, java.util.concurrent.TimeUnit)
protected void assertElementNotStale(com.gargoylesoftware.htmlunit.html.DomElement element)
public Keyboard getKeyboard()
getKeyboard
in interface HasInputDevices
public Mouse getMouse()
getMouse
in interface HasInputDevices
public WebDriver.TargetLocator switchTo()
WebDriver
switchTo
in interface WebDriver
WebDriver.TargetLocator
public WebDriver.Navigation navigate()
WebDriver
navigate
in interface WebDriver
WebDriver.Navigation
that allows the selection of what to
do nextprotected com.gargoylesoftware.htmlunit.Page lastPage()
public WebElement findElementByLinkText(java.lang.String selector)
findElementByLinkText
in interface FindsByLinkText
protected WebElement newHtmlUnitWebElement(com.gargoylesoftware.htmlunit.html.DomElement element)
public java.util.List<WebElement> findElementsByLinkText(java.lang.String selector)
findElementsByLinkText
in interface FindsByLinkText
public WebElement findElementById(java.lang.String id)
findElementById
in interface FindsById
public java.util.List<WebElement> findElementsById(java.lang.String id)
findElementsById
in interface FindsById
public WebElement findElementByClassName(java.lang.String className)
findElementByClassName
in interface FindsByClassName
public java.util.List<WebElement> findElementsByClassName(java.lang.String className)
findElementsByClassName
in interface FindsByClassName
public WebElement findElementByCssSelector(java.lang.String using)
findElementByCssSelector
in interface FindsByCssSelector
public java.util.List<WebElement> findElementsByCssSelector(java.lang.String using)
findElementsByCssSelector
in interface FindsByCssSelector
public WebElement findElementByName(java.lang.String name)
findElementByName
in interface FindsByName
public java.util.List<WebElement> findElementsByName(java.lang.String using)
findElementsByName
in interface FindsByName
public WebElement findElementByTagName(java.lang.String name)
findElementByTagName
in interface FindsByTagName
public java.util.List<WebElement> findElementsByTagName(java.lang.String using)
findElementsByTagName
in interface FindsByTagName
public WebElement findElementByXPath(java.lang.String selector)
findElementByXPath
in interface FindsByXPath
public java.util.List<WebElement> findElementsByXPath(java.lang.String selector)
findElementsByXPath
in interface FindsByXPath
public boolean isJavascriptEnabled()
public void setJavascriptEnabled(boolean enableJavascript)
protected <X> X implicitlyWaitFor(java.util.concurrent.Callable<X> condition)
protected com.gargoylesoftware.htmlunit.WebClient getWebClient()
protected com.gargoylesoftware.htmlunit.WebWindow getCurrentWindow()
public WebDriver.Options manage()
WebDriver
manage
in interface WebDriver
WebDriver.Options
public WebElement findElementByPartialLinkText(java.lang.String using)
findElementByPartialLinkText
in interface FindsByLinkText
public java.util.List<WebElement> findElementsByPartialLinkText(java.lang.String using)
findElementsByPartialLinkText
in interface FindsByLinkText