Class WebClient
java.lang.Object
com.meterware.httpunit.WebClient
- Direct Known Subclasses:
ServletUnitClient, WebConversation
The context for a series of web requests. This class manages cookies used to maintain
session context, computes relative URLs, and generally emulates the browser behavior
needed to build an automated test of a web site.
- Author:
- Russell Gold, Jan Ohrstrom, Seth Ladd, Oliver Imbusch
-
Nested Class Summary
Nested Classes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddClientListener(WebClientListener listener) Adds a listener to watch for requests and responses.voidDeprecated.as of 1.6, use #putCookie instead.voidaddWindowListener(WebWindowListener listener) Adds a listener to watch for window openings and closings.voidResets the state of this client, removing all cookies, frames, and per-client headers.voidClears the proxy server settings.Returns the properties associated with this client.getCookieDetails(String name) Returns an object containing the details of the named cookieDeprecated.- use with care - was not public in the pastString[]Returns the name of all the active cookies which will be sent to the server.getCookieValue(String name) Returns the value of the specified cookie.Returns the response representing the current top page in the main window.booleanReturns true if an exception will be thrown when an error status (4xx or 5xx) is detected on a response.getFrameContents(FrameSelector targetFrame) Returns the response associated with the specified frame name in the main window.getFrameContents(String frameName) Returns the response associated with the specified frame name in the main window.String[]Returns the name of the currently active frames in the main window.getHeaderField(String fieldName) Returns the value for the header field with the specified name.protected DictionarygetHeaderFields(URL targetURL) Returns the value of all current header fields.Returns the next javascript alert without removing it from the queue.getOpenWindow(String name) Returns the name of the active proxy server.intReturns the number of the active proxy port, or 0 is none is specified.getResource(WebRequest request) Returns the resource specified by the request.getResponse(WebRequest request) Submits a web request and returns a response, using all state developed so far as stored in cookies as requested by the server.getResponse(String urlString) Submits a GET method request and returns a response.Deprecated.as of 1.4.6.protected abstract WebResponsenewResponse(WebRequest request, FrameSelector targetFrame) Creates a web response object which represents the response to the specified web request.Returns the next javascript alert and removes it from the queue.voidDefines a cookie to be sent to the server on every request.voidremoveClientListener(WebClientListener listener) Removes a listener to watch for requests and responses.voidremoveWindowListener(WebWindowListener listener) Removes a listener to watch for window openings and closings.sendRequest(WebRequest request) Submits a web request and returns a response.voidsetAuthentication(String realm, String username, String password) Specifies a username and password for on-demand authentication.voidsetAuthorization(String userName, String password) Deprecated.as of 1.7.voidsetDialogResponder(DialogResponder responder) Specifies the object which will respond to all dialogs.voidsetExceptionsThrownOnErrorStatus(boolean throwExceptions) Specifies whether an exception will be thrown when an error status (4xx or 5xx) is detected on a response.voidsetHeaderField(String fieldName, String fieldValue) Sets the value for a header field to be sent with all requests.voidsetMainWindow(WebWindow mainWindow) abstract voidsetProxyServer(String proxyHost, int proxyPort) Specifies a proxy server to use for requests from this client.voidsetProxyServer(String proxyHost, int proxyPort, String userName, String password) Specifies a proxy server to use, along with a user and password for authentication.voidsetUserAgent(String userAgent) Deprecated.as of 1.4.6.protected final voidupdateMainWindow(FrameSelector frame, WebResponse response) Updates this web client based on a received response.protected final voidwriteMessageBody(WebRequest request, OutputStream stream) Writes the message body for the request.
-
Constructor Details
-
WebClient
protected WebClient()
-
-
Method Details
-
getMainWindow
-
setMainWindow
-
getOpenWindows
-
getOpenWindow
-
getResponse
Submits a GET method request and returns a response.- Throws:
SAXException- thrown if there is an error parsing the retrieved pageIOException
-
sendRequest
Submits a web request and returns a response. This is an alternate name for the getResponse method.- Throws:
IOExceptionSAXException
-
getCurrentPage
Returns the response representing the current top page in the main window. -
getResponse
Submits a web request and returns a response, using all state developed so far as stored in cookies as requested by the server.- Throws:
SAXException- thrown if there is an error parsing the retrieved pageIOException
-
getFrameNames
Returns the name of the currently active frames in the main window. -
getFrameContents
Returns the response associated with the specified frame name in the main window. Throws a runtime exception if no matching frame is defined. -
getFrameContents
Returns the response associated with the specified frame name in the main window. Throws a runtime exception if no matching frame is defined.- Since:
- 1.6
-
getResource
Returns the resource specified by the request. Does not update the client or load included framesets or scripts. May return null if the resource is a JavaScript URL which would normally leave the client unchanged.- Throws:
IOException
-
clearContents
public void clearContents()Resets the state of this client, removing all cookies, frames, and per-client headers. This does not affect any listeners or preferences which may have been set. -
addCookie
-
putCookie
-
getCookieNames
Returns the name of all the active cookies which will be sent to the server. -
getCookieDetails
-
getCookieValue
-
getClientProperties
Returns the properties associated with this client. -
setUserAgent
Deprecated.as of 1.4.6. Use ClientProperties#setUserAgent instead.Specifies the user agent identification. Used to trigger browser-specific server behavior. -
getUserAgent
Deprecated.as of 1.4.6. Use ClientProperties#getUserAgent instead.Returns the current user agent setting. -
setAuthorization
-
setAuthentication
Specifies a username and password for on-demand authentication. Will only send the authorization header when challenged for the specified realm.- Parameters:
realm- the realm for which the credentials apply.username- the user to authenticatepassword- the credentials for the user
-
setProxyServer
Specifies a proxy server to use for requests from this client. -
setProxyServer
-
clearProxyServer
public void clearProxyServer()Clears the proxy server settings. -
getProxyHost
Returns the name of the active proxy server. -
getProxyPort
public int getProxyPort()Returns the number of the active proxy port, or 0 is none is specified. -
setHeaderField
-
getHeaderField
-
setExceptionsThrownOnErrorStatus
public void setExceptionsThrownOnErrorStatus(boolean throwExceptions) Specifies whether an exception will be thrown when an error status (4xx or 5xx) is detected on a response. Defaults to the value returned by HttpUnitOptions.getExceptionsThrownOnErrorStatus. -
getExceptionsThrownOnErrorStatus
public boolean getExceptionsThrownOnErrorStatus()Returns true if an exception will be thrown when an error status (4xx or 5xx) is detected on a response. -
addClientListener
Adds a listener to watch for requests and responses. -
removeClientListener
Removes a listener to watch for requests and responses. -
addWindowListener
Adds a listener to watch for window openings and closings. -
removeWindowListener
Removes a listener to watch for window openings and closings. -
getNextAlert
Returns the next javascript alert without removing it from the queue. -
popNextAlert
Returns the next javascript alert and removes it from the queue. If the queue is empty, will return an empty string. -
setDialogResponder
Specifies the object which will respond to all dialogs. -
newResponse
protected abstract WebResponse newResponse(WebRequest request, FrameSelector targetFrame) throws IOException Creates a web response object which represents the response to the specified web request.- Parameters:
request- the request to which the response should be generatedtargetFrame- the frame in which the response should be stored- Throws:
IOException
-
writeMessageBody
Writes the message body for the request.- Throws:
IOException
-
getHeaderFields
Returns the value of all current header fields. -
updateMainWindow
protected final void updateMainWindow(FrameSelector frame, WebResponse response) throws IOException, SAXException Updates this web client based on a received response. This includes updating cookies and frames. This method is required by ServletUnit, which cannot call the updateWindow method directly.- Throws:
IOExceptionSAXException
-
getCookieJar
Deprecated.- use with care - was not public in the pastSupport Request [ 1288796 ] getCookieJar() in WebClient- Returns:
- the cookie jar
-