Class WebResponse

java.lang.Object
com.meterware.httpunit.WebResponse
All Implemented Interfaces:
CookieSource, com.meterware.httpunit.dom.DomWindowProxy, HTMLSegment

public abstract class WebResponse extends Object implements HTMLSegment, CookieSource, com.meterware.httpunit.dom.DomWindowProxy
A response to a web request from a web server.
Author:
Russell Gold, Drew Varner, Dave Glowacki, Benoit Xhenseval, Wolfgang Fahl
  • Constructor Details

    • WebResponse

      protected WebResponse(WebClient client, FrameSelector frame, URL url)
      Constructs a response object. see [ 1159858 ] patch for RFE 1159844 (parsing intercepted pages)
      Parameters:
      frame - the frame to hold the response
      url - the url from which the response was received
    • WebResponse

      protected WebResponse(WebClient client, FrameSelector frame, URL url, String text)
      Constructs a response object.
      Parameters:
      frame - the frame to hold the response
      url - the url from which the response was received
  • Method Details

    • newResponse

      public static WebResponse newResponse(URLConnection connection) throws IOException
      Returns a web response built from a URL connection. Provided to allow access to WebResponse parsing without using a WebClient.
      Throws:
      IOException
    • isHTML

      public boolean isHTML()
      Returns true if the response is HTML.
      Returns:
      true if the contenType fits
    • getURL

      public URL getURL()
      Returns the URL which invoked this response.
      Specified by:
      getURL in interface CookieSource
      Specified by:
      getURL in interface com.meterware.httpunit.dom.DomWindowProxy
      Returns:
      the URL associated with the window.
    • getTitle

      public String getTitle() throws SAXException
      Returns the title of the page.
      Throws:
      SAXException - thrown if there is an error parsing this response
    • getExternalStyleSheet

      public String getExternalStyleSheet() throws SAXException
      Returns the stylesheet linked in the head of the page. will return "/mystyle.css".
      Throws:
      SAXException - thrown if there is an error parsing this response
    • getMetaTagContent

      public String[] getMetaTagContent(String attribute, String attributeValue) throws SAXException
      Retrieves the "content" of the meta tags for a key pair attribute-attributeValue. this can be used like this getMetaTagContent("name","robots") will return { "index","follow" } getMetaTagContent("http-equiv","Expires") will return { "now" }
      Throws:
      SAXException - thrown if there is an error parsing this response
    • getFrameName

      public String getFrameName()
      Returns the name of the frame containing this page.
    • getRefreshRequest

      public WebRequest getRefreshRequest()
      Returns a request to refresh this page, if any. This request will be defined by a tag in the header. If no tag exists, will return null.
    • getRefreshDelay

      public int getRefreshDelay()
      Returns the delay before normally following the request to refresh this page, if any. This request will be defined by a tag in the header. If no tag exists, will return zero.
    • getResponseCode

      public abstract int getResponseCode()
      Returns the response code associated with this response.
    • getResponseMessage

      public abstract String getResponseMessage()
      Returns the response message associated with this response.
    • getContentLength

      public int getContentLength()
      Returns the content length of this response.
      Returns:
      the content length, if known, or -1.
    • getContentType

      public String getContentType()
      Returns the content type of this response.
    • getCharacterSet

      public String getCharacterSet()
      Returns the character set used in this response.
    • getNewCookieNames

      public String[] getNewCookieNames()
      Returns a list of new cookie names defined as part of this response.
    • getNewCookieValue

      public String getNewCookieValue(String name)
      Returns the new cookie value defined as part of this response.
    • getHeaderFieldNames

      public abstract String[] getHeaderFieldNames()
      Returns the names of the header fields found in the response.
    • getHeaderField

      public abstract String getHeaderField(String fieldName)
      Returns the value for the specified header field. If no such field is defined, will return null. If more than one header is defined for the specified name, returns only the first found.
    • getText

      public String getText() throws IOException
      Returns the text of the response (excluding headers) as a string. Use this method in preference to 'toString' which may be used to represent internal state of this object.
      Throws:
      IOException
    • getInputStream

      public InputStream getInputStream() throws IOException
      Returns a buffered input stream for reading the contents of this reply.
      Throws:
      IOException
    • getFrameNames

      public String[] getFrameNames() throws SAXException
      Returns the names of the frames found in the page in the order in which they appear.
      Throws:
      SAXException - thrown if there is an error parsing this response
    • getSubframeContents

      public WebResponse getSubframeContents(String subFrameName)
      Returns the contents of the specified subframe of this frameset response.
      Parameters:
      subFrameName - the name of the desired frame as defined in the frameset.
    • getElementWithID

      public HTMLElement getElementWithID(String id) throws SAXException
      Returns the HTMLElement with the specified ID.
      Specified by:
      getElementWithID in interface HTMLSegment
      Throws:
      SAXException - thrown if there is an error parsing the response.
    • getElementsByTagName

      public HTMLElement[] getElementsByTagName(String tagName) throws SAXException
      return the HTMLElements with the specified tag name
      Parameters:
      tagName - e.g. "div" or "table"
      Returns:
      a list of all HTMLElements with that tag name
      Throws:
      SAXException
      Since:
      1.7
    • getElementNames

      public String[] getElementNames() throws SAXException
      Returns a list of HTML element names contained in this HTML section.
      Specified by:
      getElementNames in interface HTMLSegment
      Throws:
      SAXException
    • getElementsWithName

      public HTMLElement[] getElementsWithName(String name) throws SAXException
      Returns the HTMLElements found in this segment with the specified name.
      Specified by:
      getElementsWithName in interface HTMLSegment
      Throws:
      SAXException
    • getElementsWithAttribute

      public HTMLElement[] getElementsWithAttribute(String name, String value) throws SAXException
      Returns the HTMLElements found with the specified attribute value.
      Specified by:
      getElementsWithAttribute in interface HTMLSegment
      Throws:
      SAXException
      Since:
      1.6
    • getForms

      public WebForm[] getForms() throws SAXException
      Returns the forms found in the page in the order in which they appear.
      Specified by:
      getForms in interface HTMLSegment
      Throws:
      SAXException - thrown if there is an error parsing the response.
    • getFormWithName

      public WebForm getFormWithName(String name) throws SAXException
      Returns the form found in the page with the specified name.
      Specified by:
      getFormWithName in interface HTMLSegment
      Throws:
      SAXException - thrown if there is an error parsing the response.
    • getFormWithID

      public WebForm getFormWithID(String ID) throws SAXException
      Returns the form found in the page with the specified ID.
      Specified by:
      getFormWithID in interface HTMLSegment
      Throws:
      SAXException - thrown if there is an error parsing the response.
    • getFirstMatchingForm

      public WebForm getFirstMatchingForm(HTMLElementPredicate predicate, Object criteria) throws SAXException
      Returns the first form found in the page matching the specified criteria.
      Specified by:
      getFirstMatchingForm in interface HTMLSegment
      Throws:
      SAXException - thrown if there is an error parsing the response.
    • getMatchingForms

      public WebForm[] getMatchingForms(HTMLElementPredicate predicate, Object criteria) throws SAXException
      Returns all forms found in the page matching the specified criteria.
      Specified by:
      getMatchingForms in interface HTMLSegment
      Throws:
      SAXException - thrown if there is an error parsing the response.
    • getLinks

      public WebLink[] getLinks() throws SAXException
      Returns the links found in the page in the order in which they appear.
      Specified by:
      getLinks in interface HTMLSegment
      Throws:
      SAXException - thrown if there is an error parsing the response.
    • getLinkWith

      public WebLink getLinkWith(String text) throws SAXException
      Returns the first link which contains the specified text.
      Specified by:
      getLinkWith in interface HTMLSegment
      Throws:
      SAXException - thrown if there is an error parsing the response.
    • getLinkWithImageText

      public WebLink getLinkWithImageText(String text) throws SAXException
      Returns the first link which contains an image with the specified text as its 'alt' attribute.
      Specified by:
      getLinkWithImageText in interface HTMLSegment
      Throws:
      SAXException - thrown if there is an error parsing the response.
    • getLinkWithName

      public WebLink getLinkWithName(String name) throws SAXException
      Returns the link found in the page with the specified name.
      Throws:
      SAXException - thrown if there is an error parsing the response.
    • getLinkWithID

      public WebLink getLinkWithID(String ID) throws SAXException
      Returns the link found in the page with the specified ID.
      Throws:
      SAXException - thrown if there is an error parsing the response.
    • getFirstMatchingLink

      public WebLink getFirstMatchingLink(HTMLElementPredicate predicate, Object criteria) throws SAXException
      Returns the first link found in the page matching the specified criteria.
      Specified by:
      getFirstMatchingLink in interface HTMLSegment
      Throws:
      SAXException - thrown if there is an error parsing the response.
    • getMatchingLinks

      public WebLink[] getMatchingLinks(HTMLElementPredicate predicate, Object criteria) throws SAXException
      Returns all links found in the page matching the specified criteria.
      Specified by:
      getMatchingLinks in interface HTMLSegment
      Throws:
      SAXException - thrown if there is an error parsing the response.
    • getImages

      public WebImage[] getImages() throws SAXException
      Returns the images found in the page in the order in which they appear.
      Specified by:
      getImages in interface HTMLSegment
      Throws:
      SAXException - thrown if there is an error parsing the response.
    • getImageWithName

      public WebImage getImageWithName(String source) throws SAXException
      Returns the image found in the page with the specified name attribute.
      Specified by:
      getImageWithName in interface HTMLSegment
      Throws:
      SAXException - thrown if there is an error parsing the response.
    • getImageWithSource

      public WebImage getImageWithSource(String source) throws SAXException
      Returns the first image found in the page with the specified src attribute.
      Specified by:
      getImageWithSource in interface HTMLSegment
      Throws:
      SAXException - thrown if there is an error parsing the response.
    • getImageWithAltText

      public WebImage getImageWithAltText(String altText) throws SAXException
      Returns the first image found in the page with the specified alt attribute.
      Specified by:
      getImageWithAltText in interface HTMLSegment
      Throws:
      SAXException - thrown if there is an error parsing the segment.
    • getApplets

      public WebApplet[] getApplets() throws SAXException
      Description copied from interface: HTMLSegment
      Returns the applets found in the page in the order in which they appear.
      Specified by:
      getApplets in interface HTMLSegment
      Throws:
      SAXException - thrown if there is an error parsing the segment.
    • getTextBlocks

      public TextBlock[] getTextBlocks() throws SAXException
      Returns an array of text blocks found in the page.
      Specified by:
      getTextBlocks in interface HTMLSegment
      Throws:
      SAXException - thrown if there is an error parsing the segment.
      Since:
      1.6
    • getNextTextBlock

      public TextBlock getNextTextBlock(TextBlock block) throws SAXException
      Returns the text block after the specified block, if any.
      Throws:
      SAXException
      Since:
      1.6
    • getFirstMatchingTextBlock

      public TextBlock getFirstMatchingTextBlock(HTMLElementPredicate predicate, Object criteria) throws SAXException
      Returns the first link found in the page matching the specified criteria.
      Throws:
      SAXException - thrown if there is an error parsing the response.
      Since:
      1.6
    • getDOM

      public Document getDOM() throws SAXException
      Returns a copy of the domain object model tree associated with this response. If the response is HTML, it will use a special parser which can transform HTML into an XML DOM.
      Throws:
      SAXException - thrown if there is an error parsing the response.
    • getTables

      public WebTable[] getTables() throws SAXException
      Returns the top-level tables found in this page in the order in which they appear.
      Specified by:
      getTables in interface HTMLSegment
      Throws:
      SAXException - thrown if there is an error parsing the response.
    • getFirstMatchingTable

      public WebTable getFirstMatchingTable(HTMLElementPredicate predicate, Object criteria) throws SAXException
      Returns the first table in the response which matches the specified predicate and value. Will recurse into any nested tables, as needed.
      Specified by:
      getFirstMatchingTable in interface HTMLSegment
      Returns:
      the selected table, or null if none is found
      Throws:
      SAXException
    • getMatchingTables

      public WebTable[] getMatchingTables(HTMLElementPredicate predicate, Object criteria) throws SAXException
      Returns all tables found in the page matching the specified criteria.
      Specified by:
      getMatchingTables in interface HTMLSegment
      Throws:
      SAXException - thrown if there is an error parsing the response.
    • getTableStartingWith

      public WebTable getTableStartingWith(String text) throws SAXException
      Returns the first table in the response which has the specified text as the full text of its first non-blank row and non-blank column. Will recurse into any nested tables, as needed. Case is ignored.
      Specified by:
      getTableStartingWith in interface HTMLSegment
      Returns:
      the selected table, or null if none is found
      Throws:
      SAXException - thrown if there is an error parsing the response.
    • getTableStartingWithPrefix

      public WebTable getTableStartingWithPrefix(String text) throws SAXException
      Returns the first table in the response which has the specified text as a prefix of the text of its first non-blank row and non-blank column. Will recurse into any nested tables, as needed. Case is ignored.
      Specified by:
      getTableStartingWithPrefix in interface HTMLSegment
      Returns:
      the selected table, or null if none is found
      Throws:
      SAXException - thrown if there is an error parsing the response.
    • getTableWithSummary

      public WebTable getTableWithSummary(String text) throws SAXException
      Returns the first table in the response which has the specified text as its summary attribute. Will recurse into any nested tables, as needed. Case is ignored.
      Specified by:
      getTableWithSummary in interface HTMLSegment
      Returns:
      the selected table, or null if none is found
      Throws:
      SAXException - thrown if there is an error parsing the response.
    • getTableWithID

      public WebTable getTableWithID(String text) throws SAXException
      Returns the first table in the response which has the specified text as its ID attribute. Will recurse into any nested tables, as needed. Case is ignored.
      Specified by:
      getTableWithID in interface HTMLSegment
      Returns:
      the selected table, or null if none is found
      Throws:
      SAXException - thrown if there is an error parsing the response.
    • getScriptableObject

      public WebResponse.Scriptable getScriptableObject()
      get the scriptable object for this WebResponse
    • setScriptingHandler

      public void setScriptingHandler(ScriptingHandler scriptingHandler)
    • getScriptingHandler

      public ScriptingHandler getScriptingHandler()
      Specified by:
      getScriptingHandler in interface com.meterware.httpunit.dom.DomWindowProxy
    • createJavascriptScriptingHandler

      public ScriptingHandler createJavascriptScriptingHandler()
    • createDomScriptingHandler

      public ScriptingHandler createDomScriptingHandler()
      create a DOMScriptingHandler
      Returns:
      the DOM scripting handler (the window)
    • newDelegate

      public static ScriptableDelegate newDelegate(String delegateClassName)
    • openNewWindow

      public com.meterware.httpunit.dom.DomWindowProxy openNewWindow(String name, String relativeUrl) throws IOException, SAXException
      open a a new Window with the given name and relative URL
      Specified by:
      openNewWindow in interface com.meterware.httpunit.dom.DomWindowProxy
      Parameters:
      name - - the name of the window
      relativeUrl - - the relative URL to be used
      Returns:
      the WebResponse as a DomWindowProxy
      Throws:
      IOException
      SAXException
    • submitRequest

      public com.meterware.httpunit.dom.DomWindowProxy submitRequest(com.meterware.httpunit.dom.HTMLElementImpl sourceElement, String method, String location, String target, com.meterware.httpunit.protocol.MessageBody requestBody) throws IOException, SAXException
      Specified by:
      submitRequest in interface com.meterware.httpunit.dom.DomWindowProxy
      Throws:
      IOException
      SAXException
    • close

      public void close()
      Specified by:
      close in interface com.meterware.httpunit.dom.DomWindowProxy
    • alert

      public void alert(String message)
      Specified by:
      alert in interface com.meterware.httpunit.dom.DomWindowProxy
    • confirm

      public boolean confirm(String message)
      Specified by:
      confirm in interface com.meterware.httpunit.dom.DomWindowProxy
    • prompt

      public String prompt(String prompt, String defaultResponse)
      Specified by:
      prompt in interface com.meterware.httpunit.dom.DomWindowProxy
    • toString

      public abstract String toString()
      Overrides:
      toString in class Object
    • defineRawInputStream

      protected final void defineRawInputStream(InputStream inputStream) throws IOException
      Throws:
      IOException
    • setContentTypeHeader

      protected void setContentTypeHeader(String value)
      Overwrites the current value (if any) of the content type header.
    • replaceText

      public boolean replaceText(String text, String contentType)
      Description copied from interface: com.meterware.httpunit.dom.DomWindowProxy
      Replaces the text in the window with the specified text and content type. Returns false if unable to do the replacement.
      Specified by:
      replaceText in interface com.meterware.httpunit.dom.DomWindowProxy
    • getClient

      public WebClient getClient()
      getter for the WebClient
      Returns:
      the web client for this WebResponse (if any)
      Since:
      1.7
    • loadResponseText

      protected void loadResponseText() throws IOException
      Throws:
      IOException
    • getValidContentTypes

      public static String[] getValidContentTypes()
      allow access to the valid content Types
      Returns:
      the validContentTypes
      Since:
      1.7
    • setValidContentTypes

      protected static void setValidContentTypes(String[] validContentTypes)
      allow modification of the valid content Types use with care
      Parameters:
      validContentTypes - the validContentTypes to set
      Since:
      1.7