|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.gargoylesoftware.htmlunit.WebClient
public class WebClient
An object that represents a web browser.
Field Summary | |
---|---|
static String |
HTMLUNIT_COOKIE_POLICY
HtmlUnit's cookie policy is to be browser-compatible. |
static URL |
URL_ABOUT_BLANK
URL for "about:blank" |
Constructor Summary | |
---|---|
WebClient()
Creates a web client instance using the browser version returned by BrowserVersion.getDefault() . |
|
WebClient(BrowserVersion browserVersion)
Creates a web client instance using the specified BrowserVersion . |
|
WebClient(BrowserVersion browserVersion,
String proxyHost,
int proxyPort)
Create an instance that will use the specified BrowserVersion and proxy server |
Method Summary | |
---|---|
void |
addHostsToProxyBypass(String pattern)
Any hosts matched by the specified regular expression pattern will bypass the configured proxy. |
void |
addRequestHeader(String name,
String value)
Set a header which will be sent up on EVERY request from this client. |
void |
addWebWindowListener(WebWindowListener listener)
Add a listener for WebWindowEvent's. |
void |
assertionFailed(String message)
Deprecated. |
void |
deregisterWebWindow(WebWindow webWindow)
INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK. Remove a web window from the list of available windows. |
protected URL |
encodeUrl(URL url)
Encodes illegal parameter in path or query string (if any) as done by browsers. |
static URL |
expandUrl(URL baseUrl,
String relativeUrl)
Expands a relative url relative to the specified base. |
Map |
getActiveXObjectMap()
Return the active X object map for this webclient. |
AjaxController |
getAjaxController()
Gets the current Ajax controller |
AlertHandler |
getAlertHandler()
Return the alert handler for this webclient. |
BrowserVersion |
getBrowserVersion()
Return the current browser version |
Cache |
getCache()
Gets the cache currently used |
ConfirmHandler |
getConfirmHandler()
Return the confirm handler. |
CredentialsProvider |
getCredentialsProvider()
Returns the credentials provider for this client instance. |
WebWindow |
getCurrentWindow()
Return the "current" window for this client. |
String |
getHomePage()
Returns the client's current homepage. |
HTMLParserListener |
getHTMLParserListener()
Gets the configured listener for messages generated by the html parser. |
static boolean |
getIgnoreOutsideContent()
Get the state of the flag to ignore content outside the BODY and HTML tags |
IncorrectnessListener |
getIncorrectnessListener()
Gets the current listener for encountered incorrectness (except HTML parsing messages that are handled by the HTML parser listener). |
com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine |
getJavaScriptEngine()
This method is intended for testing only - use at your own risk. |
protected Log |
getLog()
Return the log object for this web client |
OnbeforeunloadHandler |
getOnbeforeunloadHandler()
Return the onbeforeunload handler for this webclient. |
Page |
getPage(String url)
Convenient method to build an URL and load it into the current WebWindow |
Page |
getPage(URL url)
Convenient method to load a URL into the current WebWindow |
Page |
getPage(WebRequestSettings request)
Convenient method to load a web request into the current WebWindow |
Page |
getPage(WebWindow opener,
String target,
WebRequestSettings params)
INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK. |
Page |
getPage(WebWindow webWindow,
WebRequestSettings parameters)
Send a request to a server and return a Page that represents the response from the server. |
PageCreator |
getPageCreator()
Return the current page creator. |
boolean |
getPrintContentOnFailingStatusCode()
Return true if the content of the resulting document will be printed to the console in the event of a failing response code. |
PromptHandler |
getPromptHandler()
Return the prompt handler. |
RefreshHandler |
getRefreshHandler()
Return the current refresh handler or null if one has not been set. |
ScriptPreProcessor |
getScriptPreProcessor()
Return the script pre processor for this webclient. |
StatusHandler |
getStatusHandler()
Return the status handler for this webclient. |
int |
getTimeout()
Gets the timeout value for the WebConnection |
WebConnection |
getWebConnection()
INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK. |
WebWindow |
getWebWindowByName(String name)
Return the first WebWindow that matches the specified name. |
List |
getWebWindows()
Return an immutable list of open web windows (top windows or not). |
String |
guessContentType(File file)
Tries to guess the content type of the file. This utility could be located in an helper class but we can compare this functionality for instance with the "Helper Applications" settings of Mozilla and therefore see it as a property of the "browser". |
void |
initialize(Page newPage)
INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK. Initialize a new page for JavaScript. |
void |
initialize(WebWindow webWindow)
INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK. Initialize a new web window for JavaScript. |
void |
initializeEmptyWindow(WebWindow webWindow)
INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK. Initialize a new empty web window for JavaScript. |
boolean |
isCookiesEnabled()
Returns true if cookies are enabled. |
boolean |
isJavaScriptEnabled()
Returns true if JavaScript is enabled and the script engine was loaded successfully. |
boolean |
isPopupBlockerEnabled()
Returns true if the popup window blocker is enabled. |
boolean |
isRedirectEnabled()
Return whether or not redirections will be followed automatically on receipt of a redirect status code from the server. |
boolean |
isThrowExceptionOnFailingStatusCode()
Return true if an exception will be thrown in the event of a failing response code. |
boolean |
isThrowExceptionOnScriptError()
Indicates if an exception should be thrown when a script execution fails (the default) or if it should be caught and just logged to allow page execution to continue. |
WebResponse |
loadWebResponse(WebRequestSettings webRequestSettings)
Loads a WebResponse from the server |
Page |
loadWebResponseInto(WebResponse webResponse,
WebWindow webWindow)
Use the specified WebResponse to create a Page object which will then get inserted into the WebWindow. |
WebWindow |
openWindow(URL url,
String windowName)
Open a new window with the specified name. |
WebWindow |
openWindow(URL url,
String windowName,
WebWindow opener)
Open a new window with the specified name. |
WebWindow |
popFirstWindow()
Return the "first" window for this client. |
void |
printContentIfNecessary(WebResponse webResponse)
INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK. |
void |
pushClearFirstWindow()
Clear the first window for this client. |
void |
registerWebWindow(WebWindow webWindow)
INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK. Add a new web window to the list of available windows. |
void |
removeHostsFromProxyBypass(String pattern)
Any hosts matched by the specified regular expression pattern will no longer bypass the configured proxy. |
void |
removeRequestHeader(String name)
Remove a header |
void |
removeWebWindowListener(WebWindowListener listener)
Remove a listener for WebWindowEvent's. |
void |
setActiveXObjectMap(Map activeXObjectMap)
Set the active X object map for this webclient. |
void |
setAjaxController(AjaxController newValue)
Sets the current Ajax controller EXPERIMENTAL - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK. |
void |
setAlertHandler(AlertHandler alertHandler)
Set the alert handler for this webclient. |
void |
setCache(Cache cache)
Sets the cache to use |
void |
setConfirmHandler(ConfirmHandler handler)
Set the handler that will be executed when the javascript method Window.confirm() is called. |
void |
setCookiesEnabled(boolean enabled)
Enable/disable cookie support. |
void |
setCredentialsProvider(CredentialsProvider credentialsProvider)
Sets the credentials provider that will provide authentication information when trying to access protected information on a web server. |
void |
setCurrentWindow(WebWindow window)
Set the current window for this client. |
void |
setHomePage(String homePage)
Sets the client's homepage. |
void |
setHTMLParserListener(HTMLParserListener listener)
Defines a listener for messages generated by the html parser. Note: If getIgnoreOutsideContent() returns false , the parser
will ignore closing <body> and <html> tags to be able to handle html content
incorrectly located after the end of the html file. |
static void |
setIgnoreOutsideContent(boolean ignoreOutsideContent)
Set the flag on the HtmlParse to ignore the content that is outside of the BODY and HTML tags. |
void |
setIncorrectnessListener(IncorrectnessListener listener)
Returns the current HTML incorrectness listener. |
void |
setJavaScriptEnabled(boolean enabled)
Enable/disable JavaScript support. |
void |
setJavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine engine)
This method is intended for testing only - use at your own risk. |
void |
setOnbeforeunloadHandler(OnbeforeunloadHandler onbeforeunloadHandler)
Set the onbeforeunload handler for this webclient. |
void |
setPageCreator(PageCreator pageCreator)
Set the object that will be used to create pages. |
void |
setPopupBlockerEnabled(boolean enabled)
Enable/disable the popup window blocker. |
void |
setPrintContentOnFailingStatusCode(boolean enabled)
Specify whether or not the content of the resulting document will be printed to the console in the event of a failing response code. |
void |
setPromptHandler(PromptHandler handler)
Set the handler that will be executed when the javascript method Window.prompt() is called. |
void |
setRedirectEnabled(boolean enabled)
Set whether or not redirections will be followed automatically on receipt of a redirect status code from the server. |
void |
setRefreshHandler(RefreshHandler handler)
Set the handler to be used whenever a refresh is triggered. |
void |
setScriptPreProcessor(ScriptPreProcessor scriptPreProcessor)
Set the script pre processor for this webclient. |
void |
setStatusHandler(StatusHandler statusHandler)
Set the status handler for this webclient. |
void |
setThrowExceptionOnFailingStatusCode(boolean enabled)
Specify whether or not an exception will be thrown in the event of a failing status code. |
void |
setThrowExceptionOnScriptError(boolean newValue)
Changes the behavior of this webclient when a script error occurs. |
void |
setTimeout(int timeout)
Sets the timeout of the WebConnection. |
void |
setUseInsecureSSL(boolean useInsecureSSL)
If set to true, the client will accept connections to any host, regardless of whether they have valid certificates or not. |
void |
setWebConnection(WebConnection webConnection)
Set the object that will resolve all url requests This method is intended for unit testing HtmlUnit itself. |
void |
throwFailingHttpStatusCodeExceptionIfNecessary(WebResponse webResponse)
INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final String HTMLUNIT_COOKIE_POLICY
CookiePolicy
constants directly.
public static final URL URL_ABOUT_BLANK
Constructor Detail |
---|
public WebClient()
BrowserVersion.getDefault()
.
public WebClient(BrowserVersion browserVersion)
BrowserVersion
.
browserVersion
- The browser version to simulate.public WebClient(BrowserVersion browserVersion, String proxyHost, int proxyPort)
BrowserVersion
and proxy server
browserVersion
- The browser version to simulateproxyHost
- The server that will act as proxyproxyPort
- The port to use on the proxy serverMethod Detail |
---|
public WebConnection getWebConnection()
Return the object that will resolve all url requests
public void setWebConnection(WebConnection webConnection)
webConnection
- The new web connectionpublic Page getPage(WebWindow webWindow, WebRequestSettings parameters) throws IOException, FailingHttpStatusCodeException
The type of Page will depend on the content type of the http response.
Content type | Type of page |
---|---|
"text/html" | HtmlPage |
"text/xhtml" | HtmlPage |
"application/xhtml+xml" | HtmlPage for now, in the
future it will be XML validated as well
|
"text/*" | TextPage |
Anything else | UnexpectedPage |
webWindow
- The WebWindow to load this request intoparameters
- Parameter object for the web request
IOException
- If an IO error occurs
FailingHttpStatusCodeException
- If the server returns a failing status code AND the property
setThrowExceptionOnFailingStatusCode(boolean)
is set to trueWebRequestSettings
public Page getPage(WebWindow opener, String target, WebRequestSettings params) throws FailingHttpStatusCodeException, IOException
Open a new web window and populate it with a page loaded by
getPage(WebWindow,WebRequestSettings)
opener
- The web window that initiated the request.target
- The name of the window to be opened. This is the name that would
be passed into the javascript open() method.params
- Any parameters
FailingHttpStatusCodeException
- If the server returns a failing status code AND the property
setThrowExceptionOnFailingStatusCode(boolean)
is set to true.
IOException
- If an IO problem occurs.public Page getPage(String url) throws IOException, FailingHttpStatusCodeException, MalformedURLException
url
- The url of the new content.
FailingHttpStatusCodeException
- If the server returns a failing status code AND the property
setThrowExceptionOnFailingStatusCode(boolean)
is set to true.
IOException
- If an IO problem occurs.
MalformedURLException
- if no url can be created from the provided stringpublic Page getPage(URL url) throws IOException, FailingHttpStatusCodeException
url
- The url of the new content.
FailingHttpStatusCodeException
- If the server returns a failing status code AND the property
setThrowExceptionOnFailingStatusCode(boolean)
is set to true.
IOException
- If an IO problem occurs.public Page getPage(WebRequestSettings request) throws IOException, FailingHttpStatusCodeException
request
- The request parameters
FailingHttpStatusCodeException
- If the server returns a failing status code AND the property
setThrowExceptionOnFailingStatusCode(boolean)
is set to true.
IOException
- If an IO problem occurs.getPage(WebWindow,WebRequestSettings)
public Page loadWebResponseInto(WebResponse webResponse, WebWindow webWindow) throws IOException, FailingHttpStatusCodeException
webResponse
- The response that will be used to create the new page.webWindow
- The window that the new page will be placed within.
IOException
- If an IO error occurs.
FailingHttpStatusCodeException
- If the server returns a failing status code AND the property
setThrowExceptionOnFailingStatusCode(boolean)
is set to truepublic void setPrintContentOnFailingStatusCode(boolean enabled)
enabled
- True to enable this featurepublic boolean getPrintContentOnFailingStatusCode()
setPrintContentOnFailingStatusCode(boolean)
public void printContentIfNecessary(WebResponse webResponse)
Logs the response's content if its status code indicates a request failure and
getPrintContentOnFailingStatusCode()
returns true.
webResponse
- the response whose content may be loggedpublic void setThrowExceptionOnFailingStatusCode(boolean enabled)
enabled
- True to enable this featurepublic boolean isThrowExceptionOnFailingStatusCode()
setThrowExceptionOnFailingStatusCode(boolean)
public void throwFailingHttpStatusCodeExceptionIfNecessary(WebResponse webResponse)
Throws a FailingHttpStatusCodeException
if the request's status code indicates a request
failure and isThrowExceptionOnFailingStatusCode()
returns true.
webResponse
- the response which may trigger a FailingHttpStatusCodeException
public void addRequestHeader(String name, String value)
name
- The name of the headervalue
- The value of the headerpublic void removeRequestHeader(String name)
name
- Name of the headeraddRequestHeader(java.lang.String, java.lang.String)
public void setCredentialsProvider(CredentialsProvider credentialsProvider)
credentialsProvider
- The new credentials provider to use to authenticate.public CredentialsProvider getCredentialsProvider()
DefaultCredentialsProvider
.
public void assertionFailed(String message)
Override this to provide custom behavior.
message
- The failure messageWebAssert
public com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine getJavaScriptEngine()
null
).public void setJavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine engine)
engine
- The new script engine to use.public void setJavaScriptEnabled(boolean enabled)
enabled
- true to enable JavaScript supportpublic boolean isJavaScriptEnabled()
public void setCookiesEnabled(boolean enabled)
enabled
- true to enable cookie supportpublic boolean isCookiesEnabled()
public void setPopupBlockerEnabled(boolean enabled)
enabled
- true to enable the popup window blockerpublic boolean isPopupBlockerEnabled()
public String getHomePage()
public void setHomePage(String homePage)
homePage
- the new homepage URLpublic void addHostsToProxyBypass(String pattern)
pattern
- A regular expression pattern that matches the hostnames of the hosts which should
bypass the configured proxy.Pattern
public void removeHostsFromProxyBypass(String pattern)
pattern
- The previously added regular expression pattern.Pattern
public void setAlertHandler(AlertHandler alertHandler)
alertHandler
- The new alerthandler or null if none is specified.public AlertHandler getAlertHandler()
public void setConfirmHandler(ConfirmHandler handler)
handler
- The new handler or null if no handler is to be used.public ConfirmHandler getConfirmHandler()
public void setPromptHandler(PromptHandler handler)
handler
- The new handler or null if no handler is to be used.public PromptHandler getPromptHandler()
public void setStatusHandler(StatusHandler statusHandler)
statusHandler
- The new alerthandler or null if none is specified.public StatusHandler getStatusHandler()
public BrowserVersion getBrowserVersion()
public WebWindow getCurrentWindow()
public void setCurrentWindow(WebWindow window)
window
- The new window.public WebWindow popFirstWindow()
public void pushClearFirstWindow()
public void addWebWindowListener(WebWindowListener listener)
listener
- A listener.public void removeWebWindowListener(WebWindowListener listener)
listener
- A listener.public WebWindow openWindow(URL url, String windowName)
url
- The url to load content from or null if no content is to be loaded.windowName
- The name of the new window
public WebWindow openWindow(URL url, String windowName, WebWindow opener)
url
- The url to load content from or null if no content is to be loaded.windowName
- The name of the new windowopener
- The web window that is calling openWindow
public void setRedirectEnabled(boolean enabled)
enabled
- true to enable automatic redirection.public boolean isRedirectEnabled()
public void setUseInsecureSSL(boolean useInsecureSSL) throws GeneralSecurityException
useInsecureSSL
- whether or not to use insecure SSL
GeneralSecurityException
- if a security error occurspublic void setPageCreator(PageCreator pageCreator)
pageCreator
- The new page creatorpublic PageCreator getPageCreator()
public WebWindow getWebWindowByName(String name) throws WebWindowNotFoundException
WebWindow
that matches the specified name.
name
- The name to search for.
WebWindow
with the specified name
WebWindowNotFoundException
- If the WebWindow
can't be found.public void initialize(WebWindow webWindow)
webWindow
- The new WebWindowpublic void initialize(Page newPage)
newPage
- The new page.public void initializeEmptyWindow(WebWindow webWindow)
webWindow
- The new WebWindowpublic void registerWebWindow(WebWindow webWindow)
webWindow
- The new WebWindowpublic void deregisterWebWindow(WebWindow webWindow)
webWindow
- The WebWindow to removeprotected final Log getLog()
public static URL expandUrl(URL baseUrl, String relativeUrl) throws MalformedURLException
new URL(baseUrl, relativeUrl)
but
there are some cases that URL doesn't handle correctly. See
RFC1808
regarding Relative Uniform Resource Locators for more information.
baseUrl
- The base urlrelativeUrl
- The relative url
MalformedURLException
- If an error occurred when creating a URL objectpublic String guessContentType(File file)
file
- the file
public WebResponse loadWebResponse(WebRequestSettings webRequestSettings) throws IOException
WebResponse
from the server
webRequestSettings
- settings to use when making the request
IOException
- if an IO problem occursprotected URL encodeUrl(URL url) throws MalformedURLException, URIException
url
- the url to encode
MalformedURLException
- if the new URL could note be instantiated
URIException
- if the default protocol charset is not supportedpublic List getWebWindows()
public void setRefreshHandler(RefreshHandler handler)
RefreshHandler
for more details.
handler
- The new handlerpublic RefreshHandler getRefreshHandler()
public void setScriptPreProcessor(ScriptPreProcessor scriptPreProcessor)
scriptPreProcessor
- The new preprocessor or null if none is specifiedpublic ScriptPreProcessor getScriptPreProcessor()
public void setActiveXObjectMap(Map activeXObjectMap)
Map
is used to map the
string passed into the ActiveXObject
constructor to a java class name. Therefore
you can emulate ActiveXObject
s in a web page's javascript by mapping the object
name to a java class to emulate the active X object.
activeXObjectMap
- The new preprocessor or null if none is specifiedpublic Map getActiveXObjectMap()
public void setHTMLParserListener(HTMLParserListener listener)
getIgnoreOutsideContent()
returns false
, the parser
will ignore closing <body> and <html> tags to be able to handle html content
incorrectly located after the end of the html file. As a consequence it will finally
notify as errors that <body> and <html> are not closed properly even if
they were correctly present.
listener
- the new listener, null
if messages should be totally ignored.public HTMLParserListener getHTMLParserListener()
null
if no listener is defined (default value).public static void setIgnoreOutsideContent(boolean ignoreOutsideContent)
ignoreOutsideContent
- The boolean flag to enable or disable the support of
content outside of the HTML and BODY tagspublic static boolean getIgnoreOutsideContent()
public int getTimeout()
setTimeout(int)
public void setTimeout(int timeout)
timeout
- The value of the timeout in millisecondspublic boolean isThrowExceptionOnScriptError()
true
if an exception is thrown on script error (the default)public void setThrowExceptionOnScriptError(boolean newValue)
newValue
- indicates if exception should be thrown or notpublic IncorrectnessListener getIncorrectnessListener()
IncorrectnessListenerImpl
.
null
).public void setIncorrectnessListener(IncorrectnessListener listener)
listener
- the new value (not null
)public AjaxController getAjaxController()
public void setAjaxController(AjaxController newValue)
newValue
- the controllerpublic void setOnbeforeunloadHandler(OnbeforeunloadHandler onbeforeunloadHandler)
onbeforeunloadHandler
- The new onbeforeunloadHandler or null if none is specified.public OnbeforeunloadHandler getOnbeforeunloadHandler()
public Cache getCache()
public void setCache(Cache cache)
cache
- the new cache (must not be null
)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |